Help needed in C Programming

This is a discussion on Help needed in C Programming within the C Programming forums, part of the General Programming Boards category; Hi guys, I am new to this forum and C programming language. Currently, I am doing an application and faced ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    4

    Help needed in C Programming

    Hi guys, I am new to this forum and C programming language.
    Currently, I am doing an application and faced with some problems.
    Hope you guys here can help!!!

    I am doing an application on cryptography.
    Basically what the program does is to read in ciphertexts(encrypted texts).
    The texts will then be decrypt with a key to generate out the plain english texts.

    My program should decrypt letters(a,b,c,...,z) of the ciphertext leaving the others characters(1234567890.,!@#$~%^&<>"space") unaltered. Unaltered characters will not advance the shift to its
    next key.

    E.g. ciphertext: "sgdty &hy, GHYTR?" decrypt with the key "boy" should decrypt using this method

    "boybo &yb, OYBOY?" and
    NOT to "boybo &oy, YBOYB?"

    Currently, my program is able to leave the unaltered characters unchanged. The nummbers, !@#$%^ even "space" seems to work fine. However, all my alphabets(a-z) after going through the algorithmn display out texts that is not plain english texts.

    Below is a portion of my source code that i used to decrypt the ciphertext to english text.

    Code:
    char *vig_dec_str(char *str, char *key)
    {
      int ii, key_len = strlen(key);
      if(key_len < 1) { vig_error(VIG_ELENGTH); }
    
    
    #define VIG_DEC_CHAR(C, K)  (K == '-') ? ' ' : \
                                          ('A' + (26 + (C - 'A') - (K - 'A')) % 26)
      for(ii = 0; str[ii]; ii++)
      {
             if(isalpha(str[ii]))
             {
              str[ii] = VIG_DEC_CHAR(str[ii], key[ii % key_len]);
             }
             else if(isdigit(str[ii]))
             {
              str[ii] = str[ii];
             }
             else if(ispunct(str[ii]))
             {
              str[ii] = str[ii];
             }
             else if(iscntrl(str[ii]))
             {
              str[ii] = str[ii];
             }
             putchar(str[ii]);
      }
      return str;
    }
    I believe the problem is because my source code decrypt the texts using this method "boybo &oy, YBOYB?" which is wrong. I would like my program to decrypt the texts using this method "boybo &yb, OYBOY?".

    One of the methods that i try is removing all numbers, !@#$%^, and "space" from the ciphertexts combine the whole string texts and do the decryption. This method is not good because I am able to
    decrypt the texts correctly but i lose all my spacing, numbers and !@#$%.

    E.g.

    Results using this method: IneedsomehelpfromMrcanyouhelpmePlease
    Actual text message: I need some help from Mr.123 can you help me? Please!

    Hope you guys can help me out!

    Thanks in advance.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ProgC View Post
    Code:
    char *vig_dec_str(char *str, char *key)
    {
      int ii, key_len = strlen(key);
      if(key_len < 1) { vig_error(VIG_ELENGTH); }
    
    
    #define VIG_DEC_CHAR(C, K)  (K == '-') ? ' ' : \
                                          ('A' + (26 + (C - 'A') - (K - 'A')) % 26)
      for(ii = 0; str[ii]; ii++)
      {
             if(isalpha(str[ii]))
             {
              str[ii] = VIG_DEC_CHAR(str[ii], key[ii % key_len]);
             }
             else if(isdigit(str[ii]))
             {
              str[ii] = str[ii];
             }
             else if(ispunct(str[ii]))
             {
              str[ii] = str[ii];
             }
             else if(iscntrl(str[ii]))
             {
              str[ii] = str[ii];
             }
             putchar(str[ii]);
      }
      return str;
    }
    Everything in orange can just be removed because it doesn't actually do anything.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    4
    Thanks Quzah! Yah I try out the codes in orange is actually redunctant.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by ProgC View Post
    Hope you guys can help me out!

    Thanks in advance.
    I'd suggest something like this:


    Code:
    key[] = "boy\0"
    
    for (i = 0; i < stringlength of ciphertext ; i++)  {  /* work thru the ciphertext with i */
        /* if char is a letter, replace it with the next key letter */ 
    
        if (isalpha(ciphertext[i]))
            ciphertext[i] = key[j++] 
    
         if (j >= stringlenth of key - 1)
            j = 0
    You'll need to adjust the "stringlength" references to your own ciphertext char array. Shorten by one if you have a '\0' (end of string char), at the end of your char array.

    Right off hand, I don't remember whether isalpha('&') returns a 1 or not. Pop back with a post if that's a problem you can't surmount.

    I wouldn't use your compression method, because at the end of the program, you want to print out the encoded ciphertext, not a compressed version.


    Adak

  5. #5
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by Adak View Post
    I wouldn't use your compression method, because at the end of the program, you want to print out the encoded ciphertext, not a compressed version.
    It's not really a compressed version, it does the following:

    1. First he converts the ASCII values of C and K to their position within the alphabet.
    2. Then he takes the difference of the two
    3. Then he adds 26 to avoid taking modulo of a negative number
    4. Then he converts back to the ASCII value by adding 'A' again

    This represents a LEFT-shift by the character position within the alphabet of the key. This corresponds to a more advanced version of the Caesar cipher and was first introduced by Blaise de Vigen&#232;re who used a secret key K, splitting the plaintext into blocks of the same length as the key and adding characterwise the key onto every block. Ofc the translations are cyclic.

    There's a very easy way to break this cipher by calculating the index of coincidence for various keylength.

    Edit:
    Apparently I was wrong concerning the historic providence:
    The Vigen&#232;re cipher has been reinvented many times. The method was originally described by Giovan Batista Belaso in his 1553 book La cifra del. Sig. Giovan Batista Belaso; however, the scheme was later misattributed to Blaise de Vigen&#232;re in the 19th century, and is now widely known as the "Vigen&#232;re cipher".
    Last edited by KONI; 03-28-2007 at 05:20 AM.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    4
    Hi Adak,

    I tried your method but i am still not able to assign the key accordingly to each characters that I want.

    Below are the updated codes that i added.
    Please see if there is anything wrong with the logic.

    Code:
    char *vig_dec_str(char *str, char *key)
    {
       int ii, key_len = strlen(key);
       int jj = 0;
       if(key_len < 1) 
      { 
        vig_error(VIG_ELENGTH); 
      }
     /* The check for a key letter being '-' is used in vig_disp_text */
    #define VIG_DEC_CHAR(C, K)  (K == '-') ? ' ' : \
                                          ('A' + (26 + (C - 'A') - (K - 'A')) % 26)
      for(ii = 0; str[ii]; ii++)
      {
             if(isalpha(str[ii]))
             {
              str[ii] = VIG_DEC_CHAR(str[ii], key[ii % key_len]);
              
              if(key[jj] >= key_len - 1)
              {
               key[jj] = 0;
              }
             }
             putchar(str[ii]);
      }
      return str;
    }

  7. #7
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Yes, that logic is wrong. You're actually modifying the value of the key instead of the key index.

    Code:
    for(ii = 0; str[ii]; ii++)
      {
             if(isalpha(str[ii]))
             {
                  str[ii] = VIG_DEC_CHAR(str[ii], key[jj &#37; key_len]);
                  jj++;
             }
             putchar(str[ii]);
      }
    something along the lines of the above should work.

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    4
    Thanks KONI!
    I am able to decipher most of the texts out correctly.

    Million Thanks!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free needed or not?
    By quantt in forum Linux Programming
    Replies: 3
    Last Post: 06-25-2009, 09:32 AM
  2. C Programmers needed for Direct Hire positions
    By canefan in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 09-24-2008, 11:55 AM
  3. lock needed in this scenario?
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-25-2008, 07:22 AM
  4. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  5. semi-colon - where is it needed
    By kes103 in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2003, 05:24 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21