?
Solved

Caesar Cipher Help

Posted on 2003-02-21
7
Medium Priority
?
621 Views
Last Modified: 2012-06-27
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
Comment
Question by:lordrunt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 8

Expert Comment

by:Exceter
ID: 7997198
Could you post some examples of what it is doing wrong?
0
 

Author Comment

by:lordrunt
ID: 8005712
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
 
LVL 2

Expert Comment

by:bkrahmer
ID: 8038421
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Accepted Solution

by:
Exceter earned 300 total points
ID: 8043004
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
 

Author Comment

by:lordrunt
ID: 8069351
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
 
LVL 8

Expert Comment

by:Exceter
ID: 8072430
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
 

Author Comment

by:lordrunt
ID: 8072473
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question