Understanding Cryptography by Christof Paar and Jan Pelzl - Chapter 1 Solutions - Ex1.11
- 3 mins- Return to index
- Exercise 1.1
- Exercise 1.2
- Exercise 1.3
- Exercise 1.4
- Exercise 1.5
- Exercise 1.6
- Exercise 1.7
- Exercise 1.8
- Exercise 1.9
- Exercise 1.10
- Exercise 1.11
- Exercise 1.12
- Exercise 1.13
- Exercise 1.14
Exercise 1.11
This problem deals with the affine cipher with the key parameters a = 7, b = 22.
falszztysyjzyjkywjrztyjztyynaryjkyswarztyegyyj
- Decrypt the text above.
- Who wrote the line?
Solution
This solution is verified as correct by the official Solutions for Odd-Numbered Questions manual.
1. Given that we possess the key, we can decrypt this directly:
FIRSTTHESENTENCEANDTHENTHEEVIDENCESAIDTHEQUEEN
2. Lewis Carroll wrote this in one of his poems.
I wrote a python script which can perform affine cipher encryptions/decryptions.
def egcd(a, b):
if a == 0:
return (b, 0, 1)
else:
g, y, x = egcd(b % a, a)
return (g, x - (b // a) * y, y)
def modinv(a, m):
a = a % m # allows this to work with negative numbers
g, x, y = egcd(a, m)
if g != 1:
raise Exception('modular inverse does not exist')
else:
return x % m
def letter_to_number(c):
return ord(c.lower()) - ord('a')
def number_to_letter(n):
return chr(n % 26 + ord('a'))
def affine_encrypt(a, b, plaintext):
def encrypt_letter(c):
n = letter_to_number(c)
return number_to_letter((a * n + b) % 26)
return "".join([encrypt_letter(c) for c in plaintext])
def affine_decrypt(a, b, ciphertext):
a_inv = modinv(a, 26)
def decrypt_letter(c):
n = letter_to_number(c)
return number_to_letter((a_inv * (n - b)) % 26)
return "".join([decrypt_letter(c) for c in ciphertext]).upper()
def print_ciphertext(ciphertext):
print "\n==========="
print "Ciphertext:"
print "===========\n"
print ciphertext
def print_plaintext(a, b, plaintext):
header = "Plaintext (as decrypted by ({}, {})):".format(a, b)
print "\n", "=" * len(header), "\n", header, "\n", "=" * len(header)
print "\n", plaintext, "\n"
if __name__ == "__main__":
ciphertext = "falszztysyjzyjkywjrztyjztyynaryjkyswarztyegyyj"
print_ciphertext(ciphertext)
plaintext = affine_decrypt(7, 22, ciphertext)
print_plaintext(7, 22, plaintext)
print "Q1.11.2 Answer: This was said by the Lewis Carrol in his poem\n"