Caesar Cipher Encoder and Decoder

0a->a
Decode
Encode
Code Snippets
const text = "hello world";
const shift = 11;

const alphabet = "abcdefghijklmnopqrstuvwxyz";
const shiftedAlphabet =
  alphabet.substring(shift) + alphabet.substring(0, shift);

const table = [...text]
  .map((x) => {
    const index = alphabet.indexOf(x);
    return index > -1 ? shiftedAlphabet[index] : x;
  })
  .join("");

console.log(`Shifting ${text} by ${shift}: ${table}`);

The Caesar cipher

The Caesar cipher is one of the most famous encryption methods, named after Julius Caesar, who used it to obsure his private correspondences. It's method for ecncryption and decryption is extremely simple. It requires a key, which can be any integer, positive or negative, representing the shift of each letter in the alphabet. As an example, shown in the below table, the default alphabet (A-Z) is shifted by both 3 and 7, resulting in the ciphered alphabet.

AlphabetABCDEFGHIJKLMNOPQRSTUVWXYZ
Shift: +3DEFGHIJKLMNOPQRSTUVWXYZABC
Shift: +7HIJKLMNOPQRSTUVWXYZABCDEFG

The Message "Hello World" encoded using the second row would be "Khoor Zruog", while encoding it using the third row would result in the ciphered message "Olssv Dvysk"

The mathmatical operation for encoding a character \(C\) with a given key \(S\) is shown below. \(C\) is equal to the character`s index in the caesar alphabet.

\(E_S(C)=(C+S)\mod 26\)

Decryption is performed by subtracting the shift value instead of adding it.

\(D_S(C)=(C-S)\mod 26\)

Applications

The Caesar cipher offers virtually no cryptographic security, only hiding information from cursory glances. However, it is an excellent introduction to the cryptography concept of symmetric encryption, as it introduces the concept of a key (the "shift"), and the data (the message you want to obscure).

A more complex version of the Caesar cipher is the Vigenere cipher, which is essentially a Caesar cipher applied to two dimensions. The ROT-13 cipher is a Caesar cipher with a shift value of 13, and since there are 26 letters in the Latin alphabet, its encryption process is also its decryption process.