Understanding Cryptography by Christof Paar and Jan Pelzl - Chapter 1 Solutions - Ex1.11

- 3 mins

Exercise 1.11

This problem deals with the affine cipher with the key parameters a = 7, b = 22.

falszztysyjzyjkywjrztyjztyynaryjkyswarztyegyyj
  1. Decrypt the text above.
  2. 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"

Thomas Busby

Thomas Busby

I write about computing stuff

comments powered by Disqus
rss facebook twitter github youtube mail spotify instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora