# Caesar Cipher: Encode and Decode Online

The Caesar cipher 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 right shift of 3, a would be replaced by D, B would be replaced by E, and so on.

## 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 of the encryption and decryption steps involved with the Caesar cipher. The text we will encrypt is “attack at noon”, with a shift (key) of 1.

### How to encrypt using Caesar cipher?

Encryption with Caesar code is a monoalphabetic substitution, ie, a same letter is replaced with only one other. Caesar code is defined on an alphabet shift: a letter further in the alphabet.

Plain Alphabet |
ABCDEFGHIJKLMNOPQRSTUVWXYZ |

Caesar Alphabet (+3) |
DEFGHIJKLMNOPQRSTUVWXYZABC |

For example, let’s encrypt “attack at noon” with a shift of 3.

To encrypt A, take the alphabet and look 3 letters after: D. So, A is encrypted with D.

To encrypt T, loop the alphabet: after T: U, after U: V, after V: W.

So, T is coded W.

“Attack at noon” is coded “Dwwdfn dw qrrq”.

### How to decrypt Caesar cipher?

Caesar code decryption replaces a letter to another with an inverse alphabet shift: a previous letter in the alphabet.

For example, let’s decrypt “Dwwdfn dw qrrq” with a shift of 3.

To decrypt D, take the alphabet and look 3 letters before: A. So, D is decrypted with A.

To decrypt W, loop the alphabet: before W: V, before V: U, before U: T.

So, W is decoded T.

“Dwwdfn dw qrrq” is decoded “Attack at noon”.

## Mathematical description

First, we translate all of our characters to numbers, A=0, B=1, C=2, …., Z=25. We can now represent the Caesar cipher encryption function, e(x), where x is the character we are encrypting, as:

**e(x) = (x + k) (mod 26)**

Where k is the key (the shift) applied to each letter. After applying this function, the result is a number which must then be translated back into a letter.

For example, to encrypt D (of value 3), add the shift 3: 3 + 3 = 6 and find the letter for 6: 6 = G, so D is encrypted with G.

The decryption function is:

**e(x) = (x – k) (mod 26)**

For example, to decrypt G (of value 6), subtract the shift 6 – 3 = 3 and 3 = D, so G is decrypted with D.

## JavaScript example of the Caesar cipher

Use the above Caesar cipher decoder and encoder to encrypt and decrypt simple messages. The tool allows you to encrypt a text with a simple offset algorithm – also known as Caesar cipher.

If you are using 13 as the key, the result is similar to an ROT13 encryption.

## How to recognize Caesar ciphertext?

A message encoded with the Caesar cipher has a shift in its frequency analysis diagram (equal to the selected shift) and a coincidence index similar to the one of the plain text.

Any reference to Caesar, emperor of Rome, or generally to antiquity and the Roman Empire are clues.

Also, the presence of key words like Brutus, Augustus, or a (Caesar) salad can remind us of the Caesar imperator.

## Breaking the cipher

Cryptanalysis is the art of breaking codes and ciphers. The Caesar cipher is probably the easiest of all ciphers to break.

Since the shift has to be a number between 1 and 25, (0 or 26 would result in an unchanged plaintext) 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 calculate the frequency distribution of the letters in the cipher text.

This consists of counting how many times each letter appears. Natural English text has a very distinct distribution that can be used to help crack codes.

From the analysis the letter E is the most common, and appears almost 13% of the time, whereas Z appears far less than 1% of the time.

Application of the Caesar cipher does not change these letter frequencies, it simply shifts them along a bit (for a shift of 1, the most frequent ciphertext letter becomes F).

A code-breaker just has to find the shift that causes the ciphertext frequencies to match up closely with the natural English frequencies, then decrypt the text using that shift.

This method can be used to easily break Caesar ciphers by hand.

For a method that works well on computers, we need a way of figuring out which of the 25 possible decryptions looks the most like English text.

For that we need to use quadgram statistics as a fitness measure. This technique is used to characterize text by adding up the likelihoods of all length 4 blocks of ciphertext.

A high number means the text is very similar to English, a low number means it is not.

The key (or shift) that results in a decryption with the highest likelihood of being English text is most probably the correct key.

Of course, the more ciphertext you have, the more likely this is to be true (this is the case for all statistical measures, including the frequency approach above).

So, the method used is to take the ciphertext, try decrypting it with each key, then see which decryption looks the best.

This simplistic method of cryptanalysis only works on very simple ciphers such as the Caesar cipher and the rail fence cipher, even slightly more complex ciphers can have far too many keys to check all of them.

## How to decipher Caesar without knowing the shift?

The easiest keyless/shiftless method consist in testing all shifts, if the alphabet has 26 letters, it takes only 25 tries.

## History and usage

The Caesar cipher is one of the oldest forms of cryptography in recorded history. The cipher itself is named after the famous Roman Emperor and General Julius Caesar.

Julius used the cipher with a shift of 3 to encode military messages to his commanders.

After his death, Caesar’s nephew Augustus carried on his uncle’s usage of the cipher to protect his correspondence but changed his messages encryption to a right shift of one.

Though there were more complex codes back then, the cipher was favored by the emperor due to its simplicity.

Also, the cipher was likely effective due to the illiteracy of many of those who would intercept them. And a common misconception that the messages were written in a mysterious foreign language.

The only thing that enforced this assumption of security was the lack of evidence that any methods for solving substitution ciphers existed at the time.

However, centuries after Caesar’s assassination, the first instances of frequency analysis appeared in the middle east.

Despite its growing ineffectiveness in the modern era, the cipher would nonetheless remain in use by various groups for less vital communications such as by Jewish groups to encrypt the name of god on the mezuzah.

And later by people wishing to exchange messages in plain view by posting encoded passages in newspapers.

The last major use of this cipher for warfare was by imperial Russian forces in the first world war due to the common solders struggling to understand more complex encryption methods.

A choice that was found to be a failure as contemporary German and Austrian code-breakers were easily able to decipher any messages sent in the code.

A more recent usage of this cipher was in 2006 when a Sicilian mob boss named Bernardo Provenzano was captured by the police due to his usage of an altered version of the Caesar cipher where letters were replaced by numbers after their shift.

Likewise, in 2011, a British counter-terrorist operation foiled a planned airplane bombing due to the usage of an easily breakable Caesar cipher by the perpetrators in their internet communications.

Despite its lack of security in helping to send confidential communications by itself the Caesar cipher still has several applications today in a variety of fields.

This is due to its versatility in acting as both a simple code for education and fun and as a building block for more complex encryptions.

For instance, the ROT13 system is a special case of the Caesar cipher that operates on a shift of 13.

ROT13 system started in the 1980s with the jokes newsgroup. Today is commonly used on online forums to hide, punchlines, plot points, and puzzle solutions in discussions to prevent spoilers.

In addition, Caesar ciphers are commonly used in children’s decoder rings to create simple codes and puzzles to teach cryptanalysis.

Particularly, decoder rings themselves are a modern version of the Caesar wheel, an early decryption tool used to break the Caesar cipher.

Though the Caesar cipher is easy to break for even the most beginner modern cryptanalyst, forms a key part of the much more difficult to break Vigenere cipher.

A cipher which took nearly 3 centuries to break and operates on interweaving different alphabets with several Caesar ciphers.

Finally, the Caesar cipher forms the basis of the unbreakable one-time pad cipher.

## What are the variants of the Caesar cipher?

Caesar cipher is best known with a shift of 3, all other shifts are possible. Some shifts are known with other cipher names.

Another variant changes the alphabet, and introduce digits for example. A Caesar cipher with an offset of N corresponds to an Affine cipher Ax + B with A = 1 and B = N.

## How to encrypt digits and numbers using Caesar cipher?

Caesar cipher is applicable only to letters of the alphabet. There are, however, several solutions to encrypt numbers:

Write the numbers in Roman numerals, the numbers becoming letters, it is enough to encode them normally.

For example, nine becomes IX which becomes LA with a shift of 3.

Secondly, you can shift the numbers with the same shift as the letters.

For example, 9 becomes 12 (shift of +3).

Finally, you can integrate numbers in the alphabet.

For example, with the alphabet ABCDEF123, 21 becomes BA with an offset of 3.

## Other Implementations

For Caesar cipher code in various programming languages like Python, C++ and Java, see the implementations here.