A classical cipher, mono-alphabetic cipher. Every letter is shifted by the same amount, so plaintext letter frequencies are preserved in the ciphertext.
You can find an implementation of the shift cipher in sahithyandev/ciphers.
Encryption
Each letter in the alphabet is replaced by a letter located at a fixed distance from it.
Here:
- : plaintext letter, encoded as an integer 0 (a) to 25 (z)
- : the shift value, the secret key
Viewed as a stream cipher, a stream of plaintext characters and a stream of keys are input to , producing a stream of ciphertext characters . The shift cipher’s keystream is a repeating sequence of just one key value .
Decryption
Each letter is shifted back by the same key .
Attacking
The shift cipher has only 26 possible keys, so an attacker can try each in turn until the correct plaintext is recognized. This is called an exhaustive key search attack.
Character frequency analysis computes the frequency of characters in the ciphertext and matches it against the standard character frequency table of the language.
- Replace the highest frequency ciphertext characters with the corresponding plaintext characters by frequency rank.
- The 6 most frequent English characters account for approximately 44.4% of characters in a block of text, so the correct key is usually found quickly.
- Trial and error over different orderings of characters may be needed to find the correct key.
A more rigorous technique computes the statistical distance between the standard character frequency distribution and the ciphertext’s frequency distribution, for each candidate shift .
Here:
- : random variable distributed according to the standard character frequency
- : random variable distributed according to the shift
- : the set of values which can occur for or with non-zero probability
Computing for each of the 26 values of and taking the smallest value gives the secret key.
Worked Example
Plaintext bazinga under , with to .
Each letter adds modulo .
Ciphertext is edclqjd.
Decryption subtracts modulo .
Plaintext is bazinga.
The exhaustive attack tries the 26 keys in turn against edclqjd.
- gives
dcbkpic, not language. - gives
cbajohb, not language. - gives
bazinga, recognised as language. Key is .