M. UMAIR
  • Home
  • Photography
  • Digital Art
  • Blog
  • About Me
  • Contact

Cryptography : Caesar Cipher Program Using C++

March 16, 20176 commentsUmair

In Cryptography, a Caesar cipher, which is said to have been used by Julius Caesar to communicate with his army, is one of the simplest and most widely known encryption techniques.

Caesar is considered to be one of the first persons to have ever employed encryption for the sake of securing messages. Caesar decided that shifting each letter in the message would be his standard algorithm, and so he informed all of his generals of his decision, and was then able to send them secured messages. Using the Caesar Shift (3 to the right), the message,

“RETURN TO ROME”
would be encrypted as,

“UHWXUA WR URPH”
In this example, ‘R’ is shifted to ‘U’, ‘E’ is shifted to ‘H’, and so on. Now, even if the enemy did intercept the message, it would be useless, since only Caesar’s generals could read it.

 

 

By Matt_Crypto - http://en.wikipedia.org/wiki/File:Caesar3.png, Public Domain, https://commons.wikimedia.org/w/index.php?curid=30693472

By Matt_Crypto – http://en.wikipedia.org/wiki/File:Caesar3.png, Public Domain, https://commons.wikimedia.org/w/index.php?curid=30693472

 

 

Following a simple Code in C++ to implement a Caesar Cipher :

 

#include <iostream>
#include <string>
using namespace std;
char caesar( char );
int main()
{
    string input;
    do {
        cout << "Enter ciphertext and press enter to continue." << endl;
        cout << "Enter blank line to quit." << endl;
        getline(cin, input);
        string output = "";
        for(int i = 0; i < input.length(); i++)
        {
            output += caesar(input[i]);
        }
        cout << output << endl;
    } while (input.length() != 0);
}  //end of main function
char caesar( char c )
{
    if( isalpha(c) )
    {
        c = toupper(c); //use upper to keep from having to use two seperate for A..Z a..z
        c = (((c-65)+13) % 26) + 65;
    }
    //if c isn't alpha, just send it back.
    return c;
}

The  isalpha() function checks whether the input is an alphabet or not….in case of a number or special character , the program should return the same number/character.

The above program would Encrypt “ABCDE” as “NOPQR” and amazingly the same code can be used to Decrypt “NOPQR” as “ABCDE”!!!

Try changing this line of code  “c = (((c-65)+13) % 26) + 65;”  to get a different outputs (Ascii of A + 13 = Ascii of N) .

You can also use a variable like “i” or an expression like “2i-5” instead of 13….Make your own KEY !!!

480px-CipherDisk2000

Source: https://commons.wikimedia.org/wiki/File:CipherDisk2000.jpg

 

Next post The Baader-Meinhof Phenomenon

6 comments. Leave new

Raj
March 16, 2017 11:21 pm

Awesome bro !

Reply
Umair
March 16, 2017 11:43 pm

Thanks Raj!!

Reply
mlnd
May 21, 2019 10:40 pm

WOW Umair this will help me in my css exam ty!!!!!!!

Reply
Umair
May 21, 2019 11:50 pm

Welx beta

Reply
Big fan
May 21, 2019 11:47 pm

Sir of Caesar Cipher is named after Julius Caesar then is Brute forcing named after Brutus sir pls answer big fan

Reply
Umair
May 21, 2019 11:50 pm

Yes big fan youre correct

Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

© Muhammad Umair Yaseen Siddiqui. Email:umn2o2co2@gmail.com