Caesar Cipher
A Caesar cipher is a simple method of encoding messages. Caesar ciphers use a substitution method where letters in the alphabet are shifted by some fixed number of spaces to yield an encoding alphabet. A Caesar cipher with a shift of \(1\) would encode an A as a B, an M as an N, and a Z as an A, and so on. The method is named after Roman leader Julius Caesar, who used it in his private correspondence.
Contents
Using a Caesar Cipher
Steps for designing and using a Caesar cipher
Choose a value to shift the alphabet by.
Make a table where the top row contains letters in standard alphabetical order, and the bottom row is the new shifted alphabet.
Encode the message by exchanging each letter in the message with the equivalent shifted letter.
Make sure that the message’s intended recipient knows the shifting scheme you used to encode the message so they can decode it.
To decrypt a message encoded with a Caesar cipher, simply take the value of 26 minus the shift value, and apply that new value to shift the encoded message back to its original form.
Using a Caesar cipher described by the table below, encode the following message: “I like chemistry”. (Note: disregard case).
a b c d e f g h i j k l m n o p q r s t u v w x y z k l m n o p q r s t u v w x y z a b c d e f g h i j Note: this is a shift of 10.
A shift of 10 encodes “I like chemistry” to “S vsuo mrowscdbi”.
Using a Caesar cipher with a shift of 14, encode the following message: I send secret messages. (Note: you can disregard case).
a b c d e f g h i j k l m n o p q r s t u v w x y z o p q r s t u v w x y z a b c d e f g h i j k l m n Solution:
Go through the table and match each letter in the phrase "I send secret messages" to the corresponding encoded letter in the table. For example, the letter "s" is encoded as "g" and the letter "c" is encoded as "q".
Using this process, "I send secret messages" can be encoded as "W gsbr gsqfsh asggousg".
Here is a Caesar cipher decrypter/encrypter tool if you would like to generate your own encrypted messages.
Pros and Cons of a Caesar Cipher
A Caesar cipher is very easy to design, but also very easy to decode. To crack a Caesar code, a decoder could simply go through every possible shift of the alphabet (all \(26\) of them) and see if any sensible message appears. This is a relatively small number of combinations to check, for perspective, a message encoded with a Vigenère cipher has over 11 million possible combinations (and that is if the key is only five letters long — the longer the key, the more combinations), and the Enigma code has \(158,962,555,217,826,360,000\) possible combinations.
Crack the following message that has been encoded with a Caesar cipher: pdwk lv ixq. (Hint: the original message has a shift of between 1 and 4.)
If the original message was shifted by 1,2,3, or 4 to result in "pdwk lv ixq", then to get back to the original message, we must shift the decrypted message backward by 1,2,3, or 4 steps. We can think of this as shifting it forward by \(26-1 = 25\), \(26-2 = 24\), \(26-3 = 23\), or \(26-4 = 22\) steps. For example, if we encrypted an "a" and got a "b", to decrypt the "b" to get back the "a", we need to shift "b" by 25 steps.
Here is a table showing the first 4 shifts of the alphabet.
Original a b c d e f g h i j k l m n o p q r s t u v w x y z Shift 25 z a b c d e f g h i j k l m n o p q r s t u v w x y Shift 24 y z a b c d e f g h i j k l m n o p q r s t u v w x Shift 23 x y z a b c d e f g h i j k l m n o p q r s t u v w Shift 22 w x y z a b c d e f g h i j k l m n o p q r s t u v Here is a table showing the message decrypted using each shift
Encrypted message: pdwk lv ixq
Shift Resulting Decrypted Message Shift 25 ocvj ku hwp Shift 24 nbui jt gvo Shift 23 math is fun Shift 22 lzsg hr etm Shift 21 kyrf gq dsl Which of these decodings looks correct? The shift of 23 is the only shift that gives a resulting message in English. If the message "math is fun" is encoded with a shift of three, it is easy to check that it becomes "pdwk lv ixq" and so we use a forward shift of \(26-3\) to take in "pdwk lv ixq" and output "math is fun".
Implementation of a Caesar Cipher
Here is one way to implement a Caesar cipher encoder in Python.[2]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
Here is one way to implement a brute force Caesar cipher solver in Python.[2]Recall that one of the downsides to using a Caesar cipher is that they are fairly easy to crack. This implmentation cycles through every possible shift of the alphabet and applies each shift to the message. In total, there are \(26\) possible shifts, so the program will print out \(26\) different candidate decodings. A user can manually check through this list to see if any of the shifts produce a valid, discernable message.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
|
Run the Ceasar cipher solver code above. (Notice the print statement calling the function at the bottom of the code). Using the function, what is a good guess for what the string "n xtqaji ymj uwtgqjr!" encodes? What shift does the Caesar cipher use to encode this message?
Scan the results that the function prints out. Do any of the shifts yield valid English words? Yes! A shift of 21 on the input "n xtqaji ymj uwtgqjr!" yields "i solved the problem!".
See Also
References
- , C. Caesar3. Retrieved April 30, 2016, from https://commons.wikimedia.org/wiki/File:Caesar3.svg
- Sweigart, A. HACKING THE CAESAR CIPHER WITH THE BRUTE-FORCE TECHNIQUE. Retrieved May 10, 2016, from https://inventwithpython.com/hacking/chapter7.html