Baby's first cipher: The affine cipher

The affine cipher is simple, cute and easy to implement. The affine cipher is a monoalphabetic substitution cipher (each plaintext character is mapped to another character in the ciphertext), and as such is susceptible to all the common attacks, such as frequency analysis, simple brute forcing or even a lucky guess. It is, therefore, rarely used as more than an introduction to cryptography, number theory or as a programming exercise.

But, as I said, it's a cute, fun little cipher. You can use it for an alphabet of arbitrary length (for example, to include punctuation), but for here I've kept it to the 26-letter alphabet.

Encryption

Step one: Substitute each letter in your plaintext message with a number.

The most common is to just use a=1, b=2, c=3, ..., z=26. It is not integral that your numbering scheme be clever of misleading. A generic letter that has been transformed into a number in this way will be refereed to as a

Example: with the plaintext 'hello' we first translate it into numbers (using a=1, b=2,..., z=26), resulting in '8,5,12,12,15' or '0805121215'

Step two: Encrypt.

Create a permutation of the alphabet by replacing each a with the result of a simple equation:
ma + c (mod 26)
Where m is relatively prime to 26 (or the length of whatever alphabet you're using), and c is an arbitrary integer of your choice. m and c need to be known to decrypt.

Example: We take each number of our translated plaintext ('8,5,12,12,15') and replace each number with the modified version. Let's use m=15, c=8:
15*8 + 8 (mod 26) = 24
15*5 + 8 (mod 26) = 5
15*12 + 8 (mod 26) = 6
15*15 + 8 (mod 26) = 25

Our cipher text is now '24,5,6,6,25' or '2405060625'

Step three: Replace with letters
Using the same alphabet substitution you use previously, (for example, a=1, b=2,..., z=26), replace each number in your ciphertext with a letter, and transmit that message.

Example: Our ciphertext '24,5,6,6,25' becomes 'xeffy'

Decryption

Step one
Calculate the multiplicative inverse of m modulus 26 (or whatever alphabet you're using). That is, solve the equation for m':
m' * m = 1 (mod 26)
(More detail about calculating inverses here)
It is easy enough to calculate the inverse by inspection if you're using the 26-letter alphabet: there are only 26 possible values. In addition to that, there are only a small number of possible ms to choose from, since there will only be a handful of values that are relatively prime to m. You could easily construct a chart of possible ms and their inverses.

In our example, the inverse of m=15 (mod 26) is 7.

Step two: Substitute each letter with a number
Using the same substitutions as before. (such as a=1,...z=26) The encrypted text 'xeffy' we received becomes '24,5,6,6,25'

Step three: Decrypt
Replace each number b with the value from the equation
m' * (b - c)
Where m' was calculated and c is given as part of the key.
Example:
7 * (24 - 8) (mod 26) = 8
7 * (5 - 8) (mod 26) = 5
7 * (6 - 8) (mod 26) = 12
7 * (25 - 8) (mod 26) = 15
So the ciphertext is now decrypted into the numeric representation, '8,5,12,12,15'

Step four: Substitute with letters
As before, substitute letters for numbers using the same substitution mapping. (A=1,...,z=26).
Example: Our decrypted message '8,5,12,12,15' becomes 'hello'

And now you know a cipher algorithm!

2 things about

Baby's first cipher: The affine cipher
  1. I saw numbers, and my brain turned off... damn math block.

    But since you made this, I can safely say it is awesome.

    <3

    ReplyDelete
  2. ya i totally agree with you biohazard...

    http://www.mumzone.com.au/baby-names.php

    ReplyDelete

Copyright 2012 Phile not Found. See About
Powered by Blogger

"Whenever you find that you are on the side of the majority, it is time to pause and reflect."