212 http://inventwithpython.com/hacking
Email questions to the author: [email protected]
The affine cipher is sort of like the Caesar cipher, except it uses multiplication instead of addition
to encrypt letters. Not all numbers will work as keys though. The key number and the size of the
symbol set must be relatively prime towards each other.
To decrypt with the affine cipher we also use multiplication. To decrypt, the modular inverse of
the key is the number that is multiplied. The modular inverse of “a mod m” is a number i such
that (a * i) % m == 1. To write a function that finds the modular inverse of a number, we
use Euclid’s Extended Algorithm.
Once we understand these math concepts, we can write a program for the affine cipher in the next
chapter.