In cryptography, a cipher (or cypher) is an algorithm for performing encryption or decryption—a series of well-defined steps that can be followed as a procedure. An alternative, less common term is encipherment. To encipher or encode is to convert information into cipher or code. In common parlance, “cipher” is synonymous with “code“, as they are both a set of steps that encrypt a message; however, the concepts are distinct in cryptography, especially classical cryptography.
Codes generally substitute different length strings of character in the output, while ciphers generally substitute the same number of characters as are input. There are exceptions and some cipher systems may use slightly more, or fewer, characters when output versus the number that was input.
In this post, we will discuss the Caesar Cipher Algorithm and also write a program for the Caesar Cipher algorithm. Caesar Cipher is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on. The method is named after Julius Caesar, who used it in his private correspondence.
We will use C++ to write this algorithm due to the standard template library support. Hence, we will write the program of Caesar Cipher algorithm in C++, although, it’s very similar to C.
Encryption
INPUT:
line 1: key (key)
line 2: message (s)
OUTPUT:
line 1: Encrypted message (t)
The following is the Caesar Cipher encryption algorithm program in C++.
#include<iostream> #include<string> using namespace std; int main(){ int i,j,k; string s,t; int key; cout<<"Enter the key\n"; cin>>key; cout<<"Enter the message\n"; cin>>s; for(i=0;i<s.size();i++){ t+=(s[i]-'A'+key)%26+'A'; } cout<<"\n\nEncrypted message is "<<t<<'\n'; return 0; }
OUTPUT:
Enter the key 4 Enter the message HELLOWORLD Encrypted message is LIPPSASVPH
Decryption
INPUT:
line 1: key (key)
line 2: message (s)
OUTPUT:
line 1: decrypted message (t)
The following is the Caesar Cipher decryption algorithm program in C++.
#include<iostream> #include<string> using namespace std; int main(){ int i,j,k; string s,t; int key; cout<<"Enter the key\n"; cin>>key; cout<<"Enter the message to decrypt\n"; cin>>s; for(i=0;i<s.size();i++){ t+=(s[i]-'A'-key+26)%26+'A'; } cout<<"\n\nDecrypted message is "<<t<<'\n'; return 0; }
OUTPUT:
Enter the key 4 Enter the message to decrypt LIPPSASVPH Decrypted message is HELLOWORLD
Other cryptography algorithms:
Let us know in the comments if you are having any questions regarding this cryptography cipher Algorithm.
And if you found this post helpful, then please help us by sharing this post with your friends. Thank You
Line 13 in the decryption program is wrong, the +26 added inside the brackets will just get nullified by the mod 26 outside the brackets, the +26 should be after the mod 26 like: t+=(s[i]-‘A’-key)%26+26+’A’;
But great work on the program!
coglione