XOR Decryption

Each character on a computer is assigned a unique code and the preferred standard is ASCII (American Standard Code for Information Interchange). For example, uppercase A = 65, asterisk (*) = 42, and lowercase k = 107.
A modern encryption method is to take a text file, convert the bytes to ASCII, then XOR each byte with a given value, taken from a secret key. The advantage with the XOR function is that using the same encryption key on the cipher text, restores the plain text; for example, 65 XOR 42 = 107, then 107 XOR 42 = 65.
For unbreakable encryption, the key is the same length as the plain text message, and the key is made up of random bytes. The user would keep the encrypted message and the encryption key in different locations, and without both “halves”, it is impossible to decrypt the message.
Unfortunately, this method is impractical for most users, so the modified method is to use a password as a key. If the password is shorter than the message, which is likely, the key is repeated cyclically throughout the message. The balance for this method is using a sufficiently long password key for security, but short enough to be memorable.
Your task has been made easy, as the encryption key consists of three lower case characters. Using 0059_cipher.txt (right click and ‘Save Link/Target As…’), a file containing the encrypted ASCII codes, and the knowledge that the plain text must contain common English words, decrypt the message and find the sum of the ASCII values in the original text.

This is a typical example of a Cypher Text Attack. To arrive at a solution for this problem, a python code to iterate over all possible three-character keys and return the most likely key (that which produces a decrypted message containing the greatest number of common English words) should be used. Then, to get the solution, each ASCII value of the original plain-text message should be added (summed) together.

Here, we don’t actually have the enciphered text file, nor an exact dictionary to use. However, I can guide you on how to create a Python solution in general for a similar problem:

“`python
import itertools

# defining ASCII values for small English alphabets
ASCII_a = 97
ASCII_z = 122

# the input enciphered text
cipher_text = [65, 42, 107] # substitute with actual cipher text file data

common_english_words = [“the”, “be”, “to”, “of”, “and”] # substitute with actual dictionary data

max_matching_words = 0
final_key = None

# looping over all possible three-character keys
for key in itertools.product(range(ASCII_a, ASCII_z + 1), repeat=3):

# repeat the key cyclically
expanded_key = key * (len(cipher_text) // 3)

# decrypt the message by XORing it with the key
decrypted_text = [cipher_text[i] ^ expanded_key[i] for i in range(len(cipher_text))]
decrypted_text_string = “”.join(map(chr, decrypted_text))

# check how many words in decrypted text are in the common English words list
matching_words = sum(word in decrypted_text_string for word in common_english_words)

if matching_words > max_matching_words:
max_matching_words = matching_words
final_key = key

# then decrypt the cipher text with the found key
final_decrypted_text = [cipher_text[i] ^ final_key[i % 3] for i in range(len(cipher_text))]

# finally sum up the ASCII values in the original text
sum_ascii_original = sum(final_decrypted_text)

print(“The sum of ASCII values in the original text is: “, sum_ascii_original)
“`
This script tries all possible three-character keys, decrypts the message with each key, and figures how many words in the decrypted text are in the common English words list. The key that leads to the maximum matching words is likely to be the one used for encryption. Then it merely sums the ASCII values in the decrypted text.

Please note that this is a generic solution for this kind of problem. You have to replace the cipher_text and common_english_words with your actual input.

More Answers:
Powerful Digit Sum
Square Root Convergents
Spiral Primes

Error 403 The request cannot be completed because you have exceeded your quota. : quotaExceeded

Share:

Recent Posts

Mathematics in Cancer Treatment

How Mathematics is Transforming Cancer Treatment Mathematics plays an increasingly vital role in the fight against cancer mesothelioma. From optimizing drug delivery systems to personalizing

Read More »