Hi everyone.

Recently I have been working on an XOR encryptor/decryptor. I have looked at tutorials to get ideas. Now, I have created my encryptor/decryptor and I have absolutely messed it up i think, because the output is not right. What I am trying to do here is:

  • Creating a string for the user to input the unencrypted text.
  • Create a for loop.
  • Get the length of the string by using string.length()
  • Using Xor operator to "XOR" the string with the encryption key i made.
  • Then create another string for the user to input the encrypted text.
  • Use the same keys to "XOR" with the encrypted text to obtain

I will add a switch statement later.
Here is the code I've made, i do not know what to put in after
"^ encryption_key[]" in the for loop. I know what the [] is for though.

#include <iostream>
#include <string>


using namespace std;

int main ()
{
string decrypt_string;
string input_string;


char encryption_key[8] = "ABCDEFG";

                   
                   
                   cout << "Please enter the text you would like to encrypt below:"<<endl<<endl;

                   getline (cin, input_string);

                    cout<<endl<<endl<<"Below is the encrypted text:"<<endl;
                   
                   

                   for(int i=0; i<=input_string.length(); i++) {
                        input_string[i]=input_string[i] ^ encryption_key[]; 

// I do not know what to put in for the [] after encryption_key.
                        
     
                   
                   cout<<input_string[i];
                       
                   }
                   cout<<endl<<endl;
                   cin.get();
                   
              
              
                   cout << "Please enter the text you would like to decrypt below:"<<endl<<endl;

                   getline (cin, decrypt_string);

                   decrypt_string.length();

                   cout<<endl<<endl<<"Below is the decrypted text:"<<endl;

                   for(int y=0; y<=decrypt_string.length(); y++ ) {
     
                   decrypt_string[y]=decrypt_string[y] ^ encryption_key[];
                   
                   cout<<decrypt_string[y];
    
                   }
                   cout<<endl<<endl;
                   system("PAUSE");    
                      


return 0;
}

Thanks.

> // I do not know what to put in for the [] after encryption_key.
Another index variable - one which loops over the length of your key.

What you want to have in the brackets is another index into the key. I would assume that you want to repeat the key at the beginning after you've XOR'ed to the end of it. So you'd probably do something like this:

#include <stdio.h>

char *xorcrypt(char *cipher, const char *plain, size_t len,
               const char *key, size_t keylen)
{
   size_t i, j = 0;
   for ( i = 0; i < len; ++i )
   {
      cipher[i] = plain[i] ^ key[j];
      if ( ++j > keylen )
      {
         j = 0;
      }
   }
   return cipher;
}

int main(void)
{
   const char plain[] = "All work and no play makes Jack a dull boy.";
   char cipher[sizeof plain];
   char decode[sizeof plain] = {0};
   const char key[] = "ABCDEFG";

   puts(plain);
   xorcrypt(cipher, plain, sizeof plain - 1, key, sizeof key - 1);
   puts(xorcrypt(decode, cipher, sizeof cipher - 1, key, sizeof key - 1));

   return 0;
}

[edit]Whoa! Talk about being slow to post this.

ok thanks for your reply. Before, i have had a lot of pointer trouble, but starting from yesterday, I started learning a lot of pointers. But can I ask something here? What is char *xorcrypt actually going to point to in the code, because thats just declaring it right?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.