Caesar Cipher | Brilliant Math & Science Wiki (2024)

Sign up with Facebook or Sign up manually

Already have an account? Log in here.

Karleigh Moore, Satyabrata Dash, Eli Ross, and

  • Calvin Lin

contributed

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. Caesar Cipher | Brilliant Math & Science Wiki (1) A Caesar Cipher with a shift of 3.[1]

Contents

  • Using a Caesar Cipher
  • Pros and Cons of a Caesar Cipher
  • Implementation of a Caesar Cipher
  • See Also
  • References

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).

abcdefghijklmnopqrstuvwxyz
klmnopqrstuvwxyzabcdefghij

Note: this is a shift of 10.

A shift of 10 encodes “I like chemistry” to “S vsuo mrowscdbi”.

pmlkdbyly urqpigdqd dazyrpmzm khgfywtgt

Encode the word "spongebob" with a Caesar cipher with a forward shift of 11.

Using a Caesar cipher with a shift of 14, encode the following message: I send secret messages. (Note: you can disregard case).

abcdefghijklmnopqrstuvwxyz
opqrstuvwxyzabcdefghijklmn

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.

Originalabcdefghijklmnopqrstuvwxyz
Shift 25zabcdefghijklmnopqrstuvwxy
Shift 24yzabcdefghijklmnopqrstuvwx
Shift 23xyzabcdefghijklmnopqrstuvw
Shift 22wxyzabcdefghijklmnopqrstuv

Here is a table showing the message decrypted using each shift

Encrypted message: pdwk lv ixq

ShiftResulting Decrypted Message
Shift 25ocvj ku hwp
Shift 24nbui jt gvo
Shift 23math is fun
Shift 22lzsg hr etm
Shift 21kyrf 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 9101112131415161718192021222324252627282930313233
def CaesarCipher(message, shift): alphabet = 'abcdefghijklmnopqrstuvwxyz' #make sure the message we work with is lowercased to match symbols in alphabet message = message.lower() result = "" #run on each letter in the message for letter in message: if letter in alphabet: #find the index of the letter in the alphabet index = alphabet.find(letter) #determine the shift index = (index + shift)%(len(alphabet)) #deals with wrap around if index is greater than 26 or less than 0 if index < 0: index = index + len(alphabet) #adds letter to result result = result + alphabet[index] #if the symbol isn't a letter (like punctuation), just print that else: result = result + letter #prints what the text would read if it were decrypted using each possible shift #go through the list and figure out which shift gives you a sensible message return resultprint CaesarCipher("hello",1) print CaesarCipher("ifmmp",25)print CaesarCipher("abc",2)print CaesarCipher("zzz",4)

hrxtcnt nxdzixz blrnwln tdjfodf

Which of the following is not a valid caesar cipher encoding of the word “science”?

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 9101112131415161718192021222324252627282930313233
def CaesarCipherSolver(message): alphabet = 'abcdefghijklmnopqrstuvwxyz' #make sure the message we work with is lowercased to match symbols in alphabet message = message.lower() for key in range(len(alphabet)): result = "" #run on each letter in the message for letter in message: if letter in alphabet: #find the index of the letter in the alphabet index = alphabet.find(letter) #determine the shift index = (index + key)%(len(alphabet)) #deals with wrap around if index is greater than 26 or less than 0 if index < 0: index = index + len(alphabet) #adds letter to result result = result + alphabet[index] #if the symbol isn't a letter (like punctuation), just print that else: result = result + letter #prints what the text would read if it were decrypted using each possible shift #go through the list and figure out which shift gives you a sensible message print("Shift #%s: %s" %(26-key, result))print CaesarCipherSolver("n xtqaji ymj uwtgqjr!")

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

  • Vigenere Cipher

References

  1. , C. Caesar3. Retrieved April 30, 2016, from https://commons.wikimedia.org/wiki/File:Caesar3.svg
  2. Sweigart, A. HACKING THE CAESAR CIPHER WITH THE BRUTE-FORCE TECHNIQUE. Retrieved May 10, 2016, from https://inventwithpython.com/hacking/chapter7.html

Cite as: Caesar Cipher. Brilliant.org. Retrieved from https://brilliant.org/wiki/caesar-cipher/

Caesar Cipher | Brilliant Math & Science Wiki (2024)

References

Top Articles
Latest Posts
Article information

Author: Jerrold Considine

Last Updated:

Views: 5353

Rating: 4.8 / 5 (78 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Jerrold Considine

Birthday: 1993-11-03

Address: Suite 447 3463 Marybelle Circles, New Marlin, AL 20765

Phone: +5816749283868

Job: Sales Executive

Hobby: Air sports, Sand art, Electronics, LARPing, Baseball, Book restoration, Puzzles

Introduction: My name is Jerrold Considine, I am a combative, cheerful, encouraging, happy, enthusiastic, funny, kind person who loves writing and wants to share my knowledge and understanding with you.