Need help with decryption

This is a discussion on Need help with decryption within the C Programming forums, part of the General Programming Boards category; It encrypts the string and then [attempts to] decrypt it, but it fails. Code: #include <stdlib.h> #include <stdio.h> int enCrypt(char ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220

    Question Need help with decryption

    It encrypts the string and then [attempts to] decrypt it, but it fails.

    Code:
    #include <stdlib.h> 
    #include <stdio.h>
    
    int enCrypt(char *string, char *key)
    {
     int y = 0;
     int x;
     for (x = 0; string[x]; x++)
     {
      if(!key[x-y])
       y += x;
      string[x] = string[x]^key[x-y];
      string[x]++;
     }
     return 0;
    }
    
    int deCrypt(char *string, char *key)
    {
     int y = 0;
     int x;
     for (x = 0; string[x]; x++)
     {
      if(!key[x-y])
       y += x;
      string[x] = string[x]^key[x-y];
      string[x]++;
     }
     return 0;
    }
    
    int main(int argc, char *argv[])
    { 
     char string[] = "goodbye";
     char key[] = "hello";
     enCrypt(string,key);
     printf("Encrypted:%s\n",string);
     deCrypt(string,key);
     printf("Decrytped:%s\n",string);
     system("PAUSE");
     return 0;
    }
    What is wrong?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Order is very important in cryptography, when you encrypt the data you add one to the result of an XOR operation by saying string[x]++;
    In your decryption routine the order of the XOR and increment is the same as in your encrypt function. There are two problems with this: First, you are still incrementing when you should be decrementing if you want to get the data you started with. Second, you should decrement first and then XOR:
    Code:
    #include <stdlib.h> 
    #include <stdio.h>
    
    int enCrypt(char *string, char *key)
    {
     int y = 0;
     int x;
     for (x = 0; string[x]; x++)
     {
      if(!key[x-y])
       y += x;
      /* The  ^= operator is easier to read and
      ** more efficient than the way you had it
      */
      string[x] ^= key[x-y];
      string[x]++;
     }
     return 0;
    }
    
    int deCrypt(char *string, char *key)
    {
     int y = 0;
     int x;
     for (x = 0; string[x]; x++)
     {
      if(!key[x-y])
       y += x;
      string[x]--;
      string[x] ^= key[x-y];
     }
     return 0;
    }
    
    int main(int argc, char *argv[])
    { 
     char string[] = "goodbye";
     char key[] = "hello";
     enCrypt(string,key);
     printf("Encrypted:%s\n",string);
     deCrypt(string,key);
     printf("Decrytped:%s\n",string);
     system("PAUSE");
     return 0;
    }
    Also, since the encrypt and decrypt functions are so similar you might as well mesh them into one function for this program. And there's no need to give main arguments if you don't plan on using them.

    -Prelude
    My best code is written with the delete key.

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    The concept of XOR coding is:

    A xor B = C
    C xor B = A

    So you don't need a function for encoding and decoding, just one function that performs the xor-function on both strings is all you need.

    This

    if(!key[x-y])
    y += x;

    goes wrong. The variable x is increasing. What you probably want is that key starts from the beginning. You could introduce two indices, x and y. Where x is the index of string and y the index of key. If x is larger than y, then y should be reset to 0 when the end is reached.

    Also this

    string[x]++;

    goes wrong. You want to step through the string. So you should do

    string [x++];

  4. #4
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Thanks for the help, I knew it was something stupid, just couldn't put my finger on it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. encryption / decryption program
    By epidemic in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2008, 06:40 AM
  2. Problem with decryption (. turn to A's)
    By Dan17 in forum C++ Programming
    Replies: 1
    Last Post: 03-27-2006, 09:52 AM
  3. Decryption...
    By Psycho1312 in forum C++ Programming
    Replies: 4
    Last Post: 09-22-2005, 09:57 AM
  4. Encryption and Decryption Program in C++ using a class
    By goron350 in forum C++ Programming
    Replies: 7
    Last Post: 06-05-2005, 09:29 PM
  5. decryption : reversing
    By Denethor2000 in forum C++ Programming
    Replies: 6
    Last Post: 04-07-2002, 09:52 PM

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