Hacking Secret Ciphers with Python

(Ann) #1
Chapter 15 – The Affine Cipher 225

Look carefully at the output. You’ll notice that the ciphertext for Key A of 2 is the exact same as
the ciphertext for Key A of 97! In fact, the ciphertext from keys 3 and 98 are the same, as are the
ciphertext from keys 4 and 99!


Notice that 97 - 95 is 2. This is why a Key A of 97 does the same thing as a Key A of 2 : the
encrypted output repeats itself (that is, “wraps around”) every 95 keys. The affine cipher has the
same “wrap-around” for the Key A as it does for Key B! It seems like it is limited to the symbol
set size.


95 possible Key A keys multiplied by 95 possible Key B keys means there are 9 , 025 possible
combinations. If you subtract the integers that can’t be used for Key A (because they are not
relatively prime with 95), this number drops to 7,125 possible keys.


Summary


7,125 is about the same number of keys that’s possible with most transposition cipher messages,
and we’ve already learned how to program a computer to hack that number of keys with brute-
force. This means that we’ll have to toss the affine cipher onto the heap of weak ciphers that are
easily hacked.


The affine cipher isn’t any more secure than the previous ciphers we’ve looked at. The
transposition cipher can have more possible keys, but the number of possible keys is limited to
the size of the message. For a message with only 20 characters, the transposition cipher can only
have at most 18 keys (the keys 2 to 19). The affine cipher can be used to encrypt short messages
with more security than the Caesar cipher provided, since its number of possible keys is based on
the symbol set.


But we did learn some new mathematical concepts that we will use later on. The concepts of
modular arithmetic, greatest common divisor, and modular inverses will help us in the RSA
cipher at the end of this book.


But enough about how the affine cipher is weak in theory. Let’s write a brute-force program that
can actually break affine cipher encrypted messages!

Free download pdf