Caesar Cipher
Caesar Cipher
In cryptography, a Caesar cipher, also known as shift cipher, Caesar's cipher, Caesar's code or Caesar
shift, 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 'shifted' a certain number of places down the alphabet. For
example, with a shift of 1, A would be replaced by B, B would become C, and so on. The method is named
after Julius Caesar, who apparently used it to communicate with his generals.
More complex encryption schemes such as the vigenere cipher employ the Caesar cipher as one element
of the encryption process. The widely known ROT13 'encryption' is simply a Caesar cipher with an offset
of 13. As with all single-alphabet substitution ciphers, the Caesar cipher is easily broken and in modern
practice offers essentially no communication security.
The encryption of Caesar cipher can be represented using modular arithmetic by first transforming the
letters into numbers, according to the scheme, A = 0, B = 1,..., Z = 25. Encryption of a letter x by a shift n
can be described mathematically as,
Decryption is performed similarly,
Example:
To pass an encrypted message from one person to another, it is first necessary that both parties have the
'key' for the cipher, so that the sender may encrypt it and the receiver may decrypt it. For the Caesar
cipher, the key is the number of characters to shift the cipher alphabet.
Here is a quick example for the encryption and decryption of Caesar cipher. The text we will encrypt is
'cryptography', with a shift (key) of 3.
y h p a r g o t p y r c Plain Text
24 + 3 7 + 3 15 + 3 0 + 3 17 + 3 6 + 3 14 + 3 19 + 3 15 + 3 24 + 3 17 + 3 2 + 3 Alphabet Number + Key
b k s d u j r w s b u f Cipher Text
Decryption is just as easy, by using an offset of -3.
b k s d u j r w s b u f Cipher Text
1-3 10 - 3 18 - 3 3-3 20 - 3 9-3 17 - 3 22 - 3 18 - 3 1-3 20 - 3 5 - 3 Alphabet Number - Key
y h p a r g o t p y r c Plain Text
Security:
Caesar cipher is not a secure cryptosystem because there are only 26 possible keys to try out; we can
simply try each possibility and see which one results in a piece of readable text. If you happen to know
what a piece of the ciphertext is, or you can guess a piece, then this will allow you to immediately find the
key.
If this is not possible, a more systematic approach is to match up the frequency distribution of the letters.
By graphing the frequencies of letters in the ciphertext, and by knowing the expected distribution of
those letters in the original language of the plaintext, a human can easily spot the value of the shift by
looking at the displacement of particular features of the graph. This is known as frequency analysis. For
example, in the English language the plaintext frequencies of the letters E, T, (usually most frequent), and
Q, Z (typically least frequent) are particularly distinctive.
Encryption and decryption using Caesar Cipher is a simple method of cryptography where each letter in the
plaintext is shifted a certain number of positions down the alphabet. For example, if the shift value is 3, then A
would become D, B would become E, and so on.
Here is a sample code for encryption and decryption using Caesar Cipher in C++:
#include <iostream>
#include <string>
return result;
}
int main()
{
string message = "HELLO WORLD";
int shift = 3;
return 0;
}
In the above code, the encrypt function takes in a message and a shift value, and returns the encrypted
message. The decrypt function takes in an encrypted message and the same shift value, and returns the original
message. The main function demonstrates how to use these functions.
Output: