Caesar cipher

Description

Algorithm

Implementation

The Caesar cipher is a simple substitution cipher, which replaces each plaintext letter by a different letter of the alphabet. The cipher is named after Gaius Julius Caesar (100 BC – 44 BC), who used it for communication with his friends and allies.

Simple substitution cipher

Julius Caesar encrypted his correspondence in many ways, for example by writing texts in reverse order or writing Latin texts using Greek letters. Some ancient authors (for example, the Roman historian Gaius Suetonius Tranquillus, who lived in the first century of our era) wrote that he was using the cipher with various shifts, of one or three characters.

The Caesar cipher is one of the simplest substitution ciphers.

Each plaintext letter is replaced by another one, which is offset by a certain amount of alphabet positions (always in the same direction). If the algorithm points to the position after the last letter in the alphabet, one should move to the beginning of the alphabet.

The cipher can be presented using mathematical formulas for encrypting and decrypting characters:En(x) = (x+n) mod 26Dn(x) = (x-n) mod 26
where:n is the offset (the secret key) and 26 is the total number of letters in the Latin alphabet (of course, for other languages one should use other a different number).

Security of the Caesar cipher

The Caesar cipher can be easily broken using brute force attacks. To discover the plaintext one should just check all the possible 26 (for the Latin alphabet) offsets.