# Deciphering ASCII Codes with a 3 Letter Encryption Key

• Python
• Arman777
In summary, the conversation discusses a problem with deciphering a text of ASCII codes using a three-letter encryption key consisting of lowercase characters. The speaker has searched online sources for help but is still unsure how to approach the problem. They mention using XOR function and trying to find the most frequently used character in the ASCII text. The speaker also shares their code for XOR encryption and asks for advice on how to recognize if the output is English text. Another speaker suggests using a character frequency histogram and comparing it to an English plaintext or using a simpler method of breaking the repeated key XOR. The conversation ends with the original speaker expressing their lack of coding skills and the other speaker encouraging them to continue practicing.

#### Arman777

Gold Member
I come across a problem where I should decipher a text of ASCII codes using a encryption key consists of three lower case characters. And I know that the plain text should contain english words.

I searched online sources but I still didnt understand how should I approach the problem. Any help would be appriciated.

my understading is that first I should try to find the 3 letter encryption key by using that after the decryption that text will look like english ? . Also How does the XOR function will work in this case ?

YoungPhysicist
Is this a problem from cicada 3301?

YoungPhysicist and Arman777
Sadly not. Its a Project Euler question. It was 59 I guess

You can decrypt xor encryption by Xoring with the same values.

to encrypt: encrypted(i) = message(i) xor key(i)
to decrypt: message(i) = encrypted(i) xor key(i)

You really don't have to know anything about the english language, you just have to know what the most frequently used character in nearly every ASCII text is.

YoungPhysicist
how can I code xor ? is it some kind of a sum process ?
For example here my code
Python:
import itertools
file = open("cipher.txt","r")
A = []
for line in file:
y = line.split(",")
Encrypt = [int(e) for e in y]

B = [chr(i) for i in range(32,127)]

low_let = [i for i in range(97,123)]

h = list(itertools.combinations(low_let, 3))

Now I ll try to make random keys I guess like using the h list and I ll sum the key number and the encryptied mssg and the result will be message ?

You can use this to encrypt/decrypt
Code:
s = [79,59,12,2,79,35]   #truncated, there were 1201 numbers

key = "abc"

result = ""
for i in range(len(s)):
result += chr(s[i] ^ ord(key[i%3]))
print (result)

You could try all the keys, there are only 17576 of them. You will need some way to recognize if the output is english text. With ascii text, you might perhaps just check if all the ascii codes are printable. If the problem was harder, it might be necessary to download lists of english words, or letter frequency tables and check which of the decryptions contains the most words.

You can do it in a much simpler way tough.I wote only 2 lines of python for it.

Arman777
Breaking repeated key xor is pretty easy, especially when you know the key length

So when the keylength is 3 you'll want to make 3 arrays and take every nth byte of the ciphertext and put it in the right bucket:
ex.: "abcdefghijklmnopqrstuvwxyz" -> "adgjmpsvy", "behknqtwz", "cfilorux"

Then you can crack each part as if it were encrypted with only a single character key, reducing the search space by a lot

After you xor the array against a single character create a character frequency histogram for the resulting block and compare the difference against an english plaintext (use a histogram of moby dick or something)

The difference is the score, pick the one with the least difference and that is probably the key for that array.

Then just repeat for each array and then reassemble back in order

Arman777
I don't think I can do letter freq thing. I am not that much a good coder.

I wrote something like this so far
Python:
import itertools
import string
file = open("cipher.txt","r")
for line in file:
y = line.split(",")
Dec = [int(e) for e in y]

low_case = string.ascii_lowercase

h = list(itertools.combinations_with_replacement((low_case),3))

def test2(n):
if "this" in n:
return True

for i in range(len(h)):
key = h[i]
result = ""
for f in range(len(Dec)):
result += chr(Dec[f] ^ ord(key[f%3]))
if test2(result) == True:
print(result)

I am stuck at the test part. I am not sure what to do now..or how can I do that word freq thing.

Also thanks for the code part that you shared it was really helpful for me to understand the idea. I wrote like a 20 line code for the XOR operator (I used ^ but I didnt know I could have done it just for 1 line). Also key[i%3] part is amazing and appending strings like that. I didnt know how to use those but I learned now.

Edit: I did the printible thing but it didnt change much

I have an idea on freq I ll try it

Arman777 said:
I am stuck at the test part. I am not sure what to do now..or how can I do that word freq thing.

The test part is fine. The problem is that itertools.combinations_with_replacement only produces sorted keys. you need itertools.product.
(for some reason you need repeat = 3 as the second argument of product() instead of just 3).
You'll get your decryption with a few random ones that happen to contain"this".

Arman777
willem2 said:
The test part is fine. The problem is that itertools.combinations_with_replacement only produces sorted keys. you need itertools.product.
(for some reason you need repeat = 3 as the second argument of product() instead of just 3).
You'll get your decryption with a few random ones that happen to contain"this".

I am kind of confused about why I have to change it like that ?

I find it :)
Python:
import itertools
import string
from itertools import product

file = open("cipher.txt","r")
for line in file:
y = line.split(",")
Dec = [int(e) for e in y]

low_case = string.ascii_lowercase

h = list(itertools.product((low_case),repeat = 3))

def test2(n):
if "this" in n and "the" in n:
return True

for i in range(len(h)):
key = h[i]
result = ""
for f in range(len(Dec)):
result += chr(Dec[f] ^ ord(key[f%3]))
if test2(result) == True:
print(result)

Arman777 said:
I am not that much a good coder.

Keep doing these exercises and that will be less and less true.

BoB

jedishrfu
I agree. However this thread is now quite old and the discussion is lost to time so I’ll close it.

rbelli1

## 1. What is an ASCII code?

An ASCII code is a numerical representation of a character, such as a letter or symbol, used by computers to store and communicate text. It stands for American Standard Code for Information Interchange.

## 2. How does ASCII code encryption work?

ASCII code encryption involves converting plain text into a series of numerical values using the ASCII code, and then using a 3 letter encryption key to shift and rearrange the values. This creates a scrambled code that can only be deciphered with the same encryption key.

## 3. Why is a 3 letter encryption key used?

A 3 letter encryption key is used because it provides enough variation to create a strong and unique encryption for each character. Using more letters would result in a longer and more complex code, but not necessarily increase the security.

## 4. What is the purpose of deciphering ASCII codes with a 3 letter encryption key?

The purpose of deciphering ASCII codes with a 3 letter encryption key is to protect sensitive information and prevent unauthorized access. By using a specific key, only those who have the key can decrypt the code and access the original information.

## 5. Are there any limitations to using ASCII code encryption with a 3 letter key?

Yes, there are limitations to using ASCII code encryption with a 3 letter key. It may not be strong enough to protect highly sensitive information, as the code can still potentially be cracked with advanced decryption methods. Additionally, using a 3 letter key may not provide enough variation to encrypt longer strings of text.