Lably
Skip to content

Caesar cipher

Simple and widely known encryption technique

  • 6 min read
  • 3 chapters

Related on Lably

Through Alphabet, Modular arithmetic and Modulo

The encryption step performed by a Caesar cipher is often incorporated as part of more complex schemes, such as the Vigenère cipher, and still has modern application in the ROT13 system. As with all single-alphabet substitution ciphers, the Caesar cipher is easily broken and in modern practice offers essentially no communications security.

Example

The transformation can be represented by aligning two alphabets; the cipher is the plain alphabet shifted left or right by a certain number of positions. For instance, here is a Caesar cipher using a left shift of 3 places, equivalent to a right shift of 23 (the shift parameter is used as the key):

When encrypting, a person looks up each letter of the message in the "plain" line and writes down the corresponding letter in the "cipher" line.

Plaintext: THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG Ciphertext: QEB NRFZH YOLTK CLU GRJMP LSBO QEB IXWV ALD

Deciphering is done in reverse, with a right shift of 3.

The encryption can also be represented using modular arithmetic by first transforming the letters into numbers, according to the scheme, A → 0, B → 1, ..., Z → 25. Encryption of a letter x by a shift n can be described mathematically as:

En(x)=(x+n)mod26.

Decryption is performed similarly:

Dn(x)=(x−n)mod26.

The modulo operation (mod) may need to be performed twice during decryption depending upon the environment, as x−n is negative if n>x:

Dn(x)=(((x−n)mod26)+26)mod26.

A negative number mod a positive number has no standard definition, and programming languages differ in how it is defined. As an example, the operation −12mod5 returns 3 when performed in Python but -2 when performed in JavaScript.

The replacement remains the same throughout the message, so the cipher is classed as a type of monoalphabetic substitution, as opposed to polyalphabetic substitution.

Sign in to keep reading

2 more chapters are free with a Lably account.

More in Computing and communication