Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 623
  • Last Modified:

Caesar Cipher Help

Ok, I have to implement a simple Caesar Cipher in the programming language of my choice for a network security class.  I choose C++ since I've had the most experience with it.  Sadly though, I haven't used it in a while so I don't remember it all.  My program encrypts properly but it will not decrypt.  The character 'a' decrypts to 'b' and the single quote decrypts to 'H' not a single space like it should.  My code is below, please let me know if you see any errors in it.
<edit>
I plan on implementing a file input/output method for getting the plaintext/ciphertext and outputting the ciphertext/plaintext in the future (when I can decrypt properly)
</edit>

#include <iostream.h>
#include <math.h>
#include <fstream.h>
#include <string.h>
#include <stdio.h>
#include <string>

int main()
{
     int k, i, key, choice, temp;
   char plaintext[1000], ciphertext[1000], crap[2], ch;
   for (i=0;i<1000;i++)
   {
        plaintext[i] = ciphertext[i] = '\0';
   }
   do
   {
        cout << endl << "Do you wish to" << endl << "1 - Encode" << endl << "2 - Decode" << endl << "? :";
        cin >> choice;
   }
   while ((choice<1)||(choice>2));
       switch (choice)
   {
        case 1:
           cout << "Please enter a prime integer (k) :";
           cin >> k;
           key = k % 26;
         cin.getline(crap,2);
           cout <<  "Please enter your plain text (can be up to 1000 characters) :";
           cin.getline(plaintext,1000);
         for (i=0;plaintext[i]!='\0';i++)
             {
            if (islower(plaintext[i]))
                  {
                 plaintext[i] = char(int(plaintext[i]) - 32);
            }
            if (int(plaintext[i]) + key <= 90)
            {
                 ciphertext[i] = char(int(plaintext[i]) + key);
            }
            else
            {
                 temp = int(plaintext[i]) + key - 91;
               ciphertext[i] = char(65 + temp);
            }
             }
         cout << endl << "Your encrypted text is" << endl;
         for (i=0;ciphertext[i]!='\0';i++)
         {
              cout << ciphertext[i];
         }
         break;
          case 2:
             cout << "Please enter a prime integer (k) :";
         cin >> k;
         cin.getline(crap,2);
         key = k % 26;
         cout << "Please enter your cipher text (can be up to 1000 characters) :";
         cin.getline(ciphertext,1000);
         for (i=0;ciphertext[i]!='\0';i++)
             {
            if (islower(ciphertext[i]))
                  {
                 ciphertext[i] = char(int(ciphertext[i]) - 32);
            }
            if (int(ciphertext[i]) - key >= 65)
            {
                 plaintext[i] = char(int(ciphertext[i]) - key);
            }
            else
            {
                 temp = 64 - int(ciphertext[i]) - key;
               plaintext[i] = char(90 - temp);
            }
             }
         cout << endl << "Your decrypted text is" << endl;
         for (i=0;plaintext[i]!='\0';i++)
         {
              cout << plaintext[i];
         }
         break;
          default:
           cout << endl << "YOU SHOULD NEVER SEE THIS LINE!!!!" << endl;
         break;
      }
     cout << endl << "Please enter a character to continue :";
   cin >> ch;
     return 0;
}
0
lordrunt
Asked:
lordrunt
  • 3
  • 3
1 Solution
 
ExceterCommented:
Could you post some examples of what it is doing wrong?
0
 
lordruntAuthor Commented:
Examples

C:\Documents and Settings\Shaun\My Documents\programs>implementation

Do you wish to
1 - Encode
2 - Decode
? :1
Please enter a prime integer (k) :7
Please enter your plain text (can be up to 1000 characters) :this is a test

Your encrypted text is
AOPZ'PZ'H'ALZA
Please enter a character to continue :

k

C:\Documents and Settings\Shaun\My Documents\programs>implementation

Do you wish to
1 - Encode
2 - Decode
? :2
Please enter a prime integer (k) :7
Please enter your cipher text (can be up to 1000 characters) :AOPZ'PZ'H'ALZA

Your decrypted text is
bHISHISHAHbESb
Please enter a character to continue :3

C:\Documents and Settings\Shaun\My Documents\programs>implementation

Do you wish to
1 - Encode
2 - Decode
? :1
Please enter a prime integer (k) :t
Please enter your plain text (can be up to 1000 characters) :
Your encrypted text is

Please enter a character to continue :
C:\Documents and Settings\Shaun\My Documents\programs>d
'd' is not recognized as an internal or external command,
operable program or batch file.

C:\Documents and Settings\Shaun\My Documents\programs>implementation

Do you wish to
1 - Encode
2 - Decode
? :1
Please enter a prime integer (k) :7
Please enter your plain text (can be up to 1000 characters) :k

Your encrypted text is
R
Please enter a character to continue :a

C:\Documents and Settings\Shaun\My Documents\programs>implementation

Do you wish to
1 - Encode
2 - Decode
? :1
Please enter a prime integer (k) :7
Please enter your plain text (can be up to 1000 characters) :a

Your encrypted text is
H
Please enter a character to continue :d

C:\Documents and Settings\Shaun\My Documents\programs>implementation

Do you wish to
1 - Encode
2 - Decode
? :2
Please enter a prime integer (k) :7
Please enter your cipher text (can be up to 1000 characters) :a

Your decrypted text is
b
Please enter a character to continue :
0
 
bkrahmerCommented:
The first thing I would do to solve this problem is move your encryption and decryption into functions.  Secondly, I would skip any user interaction, and test the functions with some well-crafted test cases.  Last, I would layer on the user interaction.  Having said that, if you aren't interested in changing the structure of the code, try a debugger.  You wrote the code, so you know the algorithm.  With the debugger, you will be able to more clearly see the problem.
brian
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ExceterCommented:
Sorry about this taking so long, this question kinda slipped through the cracks until bkrahmer posted. :-)

1) This program is converting all text to upper case. Is this what you intend? If not please let me know and I will help you fix this as well.
2) Your major problem with the decrypting method is that you are not adding 26 to plaintext if is is lower than 65. Solution,

>> if (int(ciphertext[i]) - key >= 65)
>> {
>>     plaintext[i] = char(int(ciphertext[i]) - key);
>> }

change this to this,

if ( isupper(ciphertext[i]) )
{
    plaintext[i] = char(int(ciphertext[i]) - key);
    if( plaintext[i] < 65 )
     plaintext[i] += 26;
}

3) Your program does not handle non alphabetic characters properly. Solution,

change this,

>> else
>> {
>>     temp = 64 - int(ciphertext[i]) - key;
>>     plaintext[i] = char(90 - temp);
>> }

to this,

else
{
   plaintext[i] = ciphertext[i] - key;
}

Note: You do not have to cast between char and int because they are synonymous terms.

Exceter
0
 
lordruntAuthor Commented:
I found out that the problem was a missing set of paranthesis.  As for the part about converting the characters to caps, that is done on purpose.  I will work on getting it to handle spaces and other non-alphabetic characters properly soon.
0
 
ExceterCommented:
Why the B? Did my suggestions fail to solve your problem?

>> I found out that the problem was a missing set of paranthesis.

Which set?

>> will work on getting it to handle spaces and other non-alphabetic characters properly soon.

I've already told you how to do that! :-)

Exceter
0
 
lordruntAuthor Commented:
For the part about getting spaces and other non-alphabetic characters, I meant I will use your code soon.  Right now I do not have time to implement it.  For the missing set of paranthesis, it was in the decryption algorithm (can't remember exactally where right now, and again I don't have time to look).
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now