The Caesar cipher, also known as a shift cipher, is one of the classic form of e
ID: 3603097 • Letter: T
Question
The Caesar cipher, also known as a shift cipher, is one of the classic form of encryption that were used historically. Each letter in the original message (plaintext) is replaced with a letter corresponding to a certain number of letters up or down in the alphabet. In this way, a message that initially was quite readable, ends up in a form that cannot be understood at a simple glance. For example, here is the Caesar Cipher using a right rotation of 5 places (equivalent to a left shift of 21 places). The shift parameter is used as the key.
Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
Cipher: FGHIJKLMNOPQRSTUVWXYZABCDEfghijklmnopqrstuvwxyzabcde
Obviously, if a different key is used, the cipher alphabet will be shifted a different amount.
When encrypting, looks up each letter of the message in the “plain” line and writes down the corresponding letter in the “cipher” line. The example below uses the key 5.
Plaintext: Procedural Programming vs Object Oriented Programming
Ciphertext: Uwthjizwfq Uwtlwfrrnsl ax Tgojhy Twnjsyji Uwtlwfrrnsl
The Caesar Cipher can be represented using modular arithmetic as follows.
Enc(x) = (x + n) mod 26
This means that the encryption of a letter x is equal to a right shift of x by n letters. The result of the process is then taken under modulo division, meaning that if a letter is shifted past the end of the alphabet, it wraps around to the beginning.
Now, you are given below a cipher text encrypted using Caesar Cipher.
“Fhewhqccydw qdt shofjewhqfxo qhu vkd. Oek sqd tushofj jxyi cuiiqwu kiydw rhkju vehsu qjjqsa. Sxusai qbb feiiyrbu auoi kdjyb jxu sehhusj edu yi vekdt.”
Where the period (.) and comma (,) symbols are not encrypted. Write a program to decrypt this message by exhaustively search over all possible 26 keys.
Explanation / Answer
For every letter in the message M :
1. Convert the letter into the number that matches its order in the alphabet starting from 0, and call this number X.
( A=0, B=1, C=2, ...,Y=24, Z=25)
2. Calculate: Y = (X + K) mod 26
3. Convert the number Y into a letter that matches its order in the alphabet starting from 0.
(A=0, B=1, C=2, ...,Y=24, Z=25)
For Example: We agree with our friend to use the Shift Cipher with key K=19for our message.
We encrypt the message "KHAN", as follows:
So, after applying the Shift Cipher with key K=19 our message text "KHAN" gave us cipher text "DATG".
We give the message "DATG" to our friend.
How to decrypt:
For every letter in the cipher text C :
1. Convert the letter into the number that matches its order in the alphabet starting from 0, and call this number Y.
(A=0, B=1, C=2, ..., Y=24, Z=25)
2. Calculate: X= (Y - K) mod 26
3. Convert the number X into a letter that matches its order in the alphabet starting from 0.
(A=0, B=1, C=2, ..., Y=24, Z=25)
Our friend now decodes the message using our agreed upon key K=19. As follows:
So, after decrypting the Shift Cipher with key K=19 our friend deciphers the cipher text "DATG" into the message text "KHAN".