Thread: XOR Encrypting Program Problem (C++)

  1. #1
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230

    XOR Encrypting Program Problem (C++)

    just wrote a small program to see if I could fully understand the encryption scheme using XOR. I basically have it all, except for a small bug. First I'll give you the code snippet for my encrypting function:
    Code:
    void DCText::cipher()
    {
      int i;   // Array subscript
      
      int strlngth = strlen(plntxt);
      cout << "String length of plaintext is " << strlngth << endl;
    
      for( i = 0; i < strlngth; i++)          // Encrypting 
         cprtxt[i] = plntxt[i] ^ key[i];
    
      int cprlength = strlen(cprtxt);
      cout << "String length of cprtxt is " << cprlength << endl;
    }
    cprtxt[], plntxt[], and key[] are members of the class DCText. Now that is clear, let me explain the problem. As you see, I first print the string length of the plaintext (ie, the non-encrypted text). The stringlength is correct here. Then, I do the actually encryption with the XOR ^ operator and store the encrypted character into the cprtxt[] array. I then print out the stringlength of the ciphertext (ie, the encrypted text). The stringlength it prints out is 0 (zero). So obviously there is something wrong. I have concluded that the ciphered text is not being stored into the cprtxt[] array. Any ideas why?

    Thanks.

  2. #2
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Remember that string are nul terminated and XORing may turn a letter into a '\0'.

  3. #3
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    I figured it out. It was actually 2x problems. I had to ignore newlines in the ciphering function and I forgot to terminate the strings in the input function(s).

    Thanks for your help though

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 11:10 AM
  2. Program Termination Problem
    By dacbo in forum C Programming
    Replies: 3
    Last Post: 01-23-2006, 02:34 AM
  3. Replies: 20
    Last Post: 06-12-2005, 11:53 PM
  4. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM