Code breakers

(due: Thursday, November 2, 8:00 AM)

ASCII codes

Each character on a computer keyboard is assigned an ASCII code, which is an integer in the range 0-127. The ASCII code of a character can be obtained using the ord() function:

for c in "This is MTH 337":
    print('{:>3} {}'.format(ord(c), "'"+c+"'"))

84 ‘T’ 104 ‘h’ 105 ‘i’ 115 ‘s’ 32 ‘ ‘ 105 ‘i’ 115 ‘s’ 32 ‘ ‘ 77 ‘M’ 84 ‘T’ 72 ‘H’ 32 ‘ ‘ 51 ‘3’ 51 ‘3’ 55 ‘7’

Conversely, the function chr() converts ASCII codes into characters:

char_list = []
for n in [104, 101, 108, 108, 111]:
    char_list.append(chr(n))
    txt = ''.join(char_list)
print(txt)

hello

Text encryption

In order to securely send a confidential message one usually needs to encrypt it in some way to conceal its content. Here we consider the following encryption scheme:

  • One selects a secret key, which is sequence of characters. This key is used to both encrypt and decrypt the message.

  • Characters of the secret key and characters of the message are converted into ASCII codes. In this way the key is transformed into a sequence of integers \((k_1, k_2, \dots, k_r)\), and the message becomes another sequence of integers \((m_1, m_2, \dots, m_s)\). If \(r < s\) then the secret key sequence is extended by repeating it as many times as necessary until it matches the length of the message.

  • Let \(c_i\) be the reminder from the division of \(m_i+k_i\) by 128. The sequence of numbers \((c_1, c_2, \dots, c_s)\) is the encrypted message.

For example, if the message is ‘Top secret!’ and the secret key is ‘buffalo’ then the encrypted message is:

\[\tt{54,100,86,6,84,81,82,84,90,90,7}\]

In order to decrypt the message we work backwards: for each number \(c_i\) we compute the reminder from the division of \(c_i-k_i\) by 128. This number is equal to \(m_i\), so converting it into a character we get the \(i\)-th letter of the message.

Project

Each file listed below contains text (a fragment of a book in English) encrypted using the method described above. The secret key used to encrypt the text is a word, different for each file. A dictionary of over 60,000 English words is available here, and all words used as secret keys are listed in this dictionary. Write code that decrypts the file assigned to you.

Note. This is a programming project. Your project report does not need include narrative, beyond comments explaining how your code works. The project will be graded according to the following rubrics:

  • Code that successfully decrypts the text file: 70%

  • Report organization and code documentation: 30%