Affine Cipher

Suggested experiment 1

  • Enter english text in the INPUT TEXT box.
  • Enter the key (two numbers from 0 to 25) in the ALPHA and BETA boxes. Note that ALPHA must be relatively prime to 26 (it must be odd, and not 13).
  • Click ENCRYPT to encrypt the text (without spaces) and see the ciphertext.
  • Change the key and click DECRYPT to see the "plaintext" resulting from decrypting with an incorrect key.

Suggested experiment 2

  • Click SECRET to generate ciphertext from a secret english phrase using a random key.
  • Click the FREQUENCY tab
  • Find the highest point on the graph. This shows which key will decrypt the ciphertext into the most english-like plaintext (i.e. mazimizes the dot product of the probability vectors).
  • Slide the slider until the red bar is on the highest point. The lower ALPHA and BETA boxes now contain that key.
  • Click SET to copy the lower ALPHA and BETA boxes into the upper boxes.
  • Click DECRYPT to try decrypting with that key.
  • If this is the right key, the INPUT TEXT box will contain actual english text with the spaces removed.
  • If that failed, try again using the second-highest point on the graph.
  • Click on SECRET again and repeat to see a different text encrypted with a different key.

Overview

An affine cipher encrypts this way: where: Enter a sentence and an alpha and beta below and click ENCRYPT to encrypt that sentence with that key. Enter a key and click DECRYPT to decrypt. Decryption will not do anything unless the alpha is relatively prime to 26 (i.e. is odd and is not 0 or 13). The SECRET button encrypts an english phrase with a random key. The FREQUENCY tab has tools to help discover that key and decrypt the phrase.

Affine Map

The AFFINE MAP tab shows the encryption for a given key. Enter an alpha and beta and click either ENCRYPT or DECRYPT to see how each plaintext letter (across the bottom) encrypts to a ciphertext letter (down the left side). A black dot gives each encryption. Note that changing beta shifts the graph vertically. If alpha is 1, then the dots form a line of slope 1, wrapping around when it goes above the top of the graph. Increasing alpha will increase that slope, though this is harder to see for large values such as alpha=11.

Frequency

The FREQUENCY tab has black bars that show how often each letter occurs in typical english text. If either SECRET or DECRYPT have been clicked to create ciphertext, and if a key has been entered in the lower alpha and beta boxes, then the red bars show the frequency of the plaintext, if the ciphertext is decrypted with that key. The key can be chosen by entering numbers, or with the slider.

The graph at the bottom shows the dot product of the black and red frequencies, for each possible key. When the curve is high, that shows a large dot product, which means the two frequencies are very similar. The correct key is likely to be the highest point on that graph. If that key has been chosen with the slider, clicking SET will copy it up to the upper alpha and beta boxes, and then clicking DECRYPT can try it to see what plaintext is generated.