A classical and mono-alphabetic cipher. The key is a proper permutation of the plaintext alphabet.
You can find an implementation of the substitution cipher in sahithyandev/ciphers.
Encryption
The key is a permutation of the alphabet. Each plaintext letter is replaced by the letter mapped to it by the key. The same letter always maps to the same ciphertext letter.
The key space equals the number of permutations of the alphabet, . A massive key space does not make the substitution cipher unbreakable. Statistical techniques based on language characteristics still apply.
Decryption
The receiver inverts the key permutation, mapping each ciphertext letter back to the plaintext letter that produced it.
Attacking
Frequency analysis recovers the key without searching the key space. Might fail on short messages, as the sample size is too small. A few hundred characters is usually sufficient.
Steps:
- Count the frequency of each ciphertext letter.
- Match the most frequent ciphertext letters to the most frequent letters of the plaintext language. In English, , , , , , are the most common.
- Compare digram (such as
th,in) and trigram (such asthe) counts against known language statistics. - Use word boundaries when spaces are retained. Single-letter words are or . Common short words are , , , .
- Fix the confident mappings, then fill the rest by trial, checking that partially decrypted words are valid.
Worked Example
Key alphabet LOBSTERACDFGHIJKMNPQUVWXYZ, so plaintext a maps to L, b to O, c to B, and so on.
Each plaintext letter is looked up in the key alphabet.
hmaps toAomaps toJwmaps toWymaps toY
Plaintext howyoudoin gives ciphertext AJWYJUSJCI.
The inverse map sends each ciphertext letter back.
Amaps tohJmaps tooWmaps towYmaps toy
Ciphertext AJWYJUSJCI gives plaintext howyoudoin.
For a longer ciphertext in this key, a frequency count leads the recovery.
- The most frequent ciphertext letter is matched to
e, the next tot, and so on down the English frequency rank. - The digram
QArecurs, matched toth, which fixesQtotandAtoh. QATthen readsthe, fixingTtoe.- Remaining letters are filled by trial, checking that partially decrypted words are valid.