Saturday 22 February 2014

Caesar Cipher - Implementation in C++ : Program Source code

This is one of my all-time favorite programs. This is the program code for one of the most popular and easiest encryption techniques in Cryptography. Hey... Don't get over-excited, because, this is not what governments are using to encrypt their secrets, but still, it's worth knowing. Let's start by understanding Caesar Cipher is.

Caesar Cipher

Like all ciphers, caesar ciphers are also used to communicate messages from a source to another, without the middleman/medium does not know about the message. Ciphers are exclusively used in wars to communicate military secrets. The field of Cryptography deals with these kind of stuff. In earlier days, encryption and decryption of messages were manual. This proved to be very inefficient, tedious, and time consuming. Then came computers. Computers aided cryptography very much. With the help of computers and computer programming, encryption, transmission, and decryption of messages became very easy.

Caesar cipher is said to be used first by Roman Emperor Julius Caesar, whose name became bound to the method with time. It is also called Caesar shift cipher, because, the method used can be described as shifting. Let's consider an example where number of letters shifted is 3. If the input letter is 'a', the output letter is the alphabet which is 3 letters after the input, which is 'd'. Similarly, all the letters are transformed into some other one, and the resulting string does not make sense at all, unless you decrypt it! Here is a graphical representation of what's happening in a caesar shift.



So, 'Hello, World!' gets transformed into, 'Khoor, zruog!', if the shift number is 3.

Now let's see the code, shall we?

Code
#include<iostream.h>
#include<string>
void main()
{
    cout<<"Enter the input message:\n";
    char inmsg[1000];
    int i, j, length,choice;
    cin.getline(inmsg,100);
    length = strlen(inmsg);
    cout<<"Enter your choice \n1. Encrypt \n2. Decrypt \n";
    cin>>choice;
    if(choice==1)
    {
         for(i=0;i<length;i++)
         {
             if(isalpha(inmsg[i]))
             {
                 inmsg[i]=tolower(inmsg[i]);
                 for(j=0;j<3;j++)
                 {
                    if(inmsg[i]=='z')
                    {
                        inmsg[i]='a';
                    }
                    else
                    {
                        inmsg[i]++;
                    }
                 }
             }
         }
         cout<<"The encrypted message is \""<<inmsg<<"\"\n";
    }
    else if (choice == 2)
    {
        for(i=0;i<length;i++)
        {
             if(isalpha(inmsg[i]))
             {
                 inmsg[i]=tolower(inmsg[i]);
                 for(j=0;j<3;j++)
                 {
                     if(inmsg[i]=='a')
                     {
                        inmsg[i]='z';
                     }
                     else
                     {
                        inmsg[i]--;
                     }
                 }
            }
       }
    cout<<"The decrypted message is \""<<inmsg<<"\"\n";
    }
}

For newer IDEs, visit this page.

Working

NOTE: The header 'string.h' is used in the program, which includes functions like strlen(), isalpha(), tolower()

First, we input the character string from the user. Also, we ask whether he wants to encrypt or decrypt the message given.
If he chooses to encrypt : We loop through the string array, and check whether all the characters are alphabets using the function, 'isalpha()', and also we change all the given upper case characters to lower case using 'tolower()'. Now we loop through it again, but this time only by the number of times to shift the alphabets, which in this case is 3. Now we encounter a problem : What if the user enters the letters 'x','y', or 'z'? There is no letter beyond 'z' right? This can be solved by a little adjustment. If the loop encounters the letter 'z', it's gonna automatically change it to 'a' on the next run. In all other cases, we just increment the letter by 1. So even if the user gives 'x', the loop increments the letter by 1 until it reaches 'z', and it's gonna change it to 'a'. Haha! That's all with encryption! Here is an output, when the user chooses encryption


Notice how the program is retaining those symbols without any change? This is because we put the function isalpha(). This function checks whether if a character is alphabet or not, and if it is not, the loop does not touches that character.
If he chooses to decrypt : Now also everything is same as above, except that the problem changes to the letter 'a', and the adjustment is made the other way around. Also, the letter is decremented by 1 instead of being decremented. Here is a sample decryption output.




That's all for today, guys...
kdssb frglqj!*


*Try on your decryptor! ;-)


3 comments:

  1. Hey Raman ....
    Try to encrypt 'cold'

    ReplyDelete
    Replies
    1. Yeah.. It happens. Just a mere coincidence, dude! If you are that uncomfortable, you can always change the shift number. :P

      Delete
  2. Hi sir can you explain how shifting works

    ReplyDelete

You might also like