Deciphering ASCII Codes with a 3 Letter Encryption Key

  • Context: Python 
  • Thread starter Thread starter Arman777
  • Start date Start date
  • Tags Tags
    Encryption
Click For Summary

Discussion Overview

The discussion revolves around deciphering a text of ASCII codes using a three-letter encryption key. Participants explore various approaches to decrypting the text, which is believed to contain English words. The conversation includes technical explanations of XOR encryption, coding strategies, and challenges faced during the decryption process.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant expresses uncertainty about how to approach the problem and questions the role of the XOR function in the decryption process.
  • Another participant clarifies that XOR encryption can be decrypted by applying the same XOR operation with the key.
  • A participant shares a code snippet demonstrating how to implement XOR decryption in Python and suggests checking for printable ASCII characters to identify valid English text.
  • Several participants discuss strategies for breaking the repeated key XOR encryption, including creating separate arrays for each character in the key and analyzing character frequency.
  • One participant expresses a lack of confidence in their coding skills and seeks further guidance on implementing frequency analysis.
  • Another participant points out that the use of `itertools.combinations_with_replacement` produces sorted keys, suggesting that `itertools.product` should be used instead to generate the keys correctly.
  • A participant updates their code to include checks for specific English words, indicating progress in their decryption efforts.

Areas of Agreement / Disagreement

Participants generally agree on the mechanics of XOR encryption and the need to identify English text in the decrypted output. However, there are differing opinions on the best methods for generating keys and analyzing frequency, and some participants express uncertainty about their coding abilities.

Contextual Notes

Some participants mention limitations in their coding skills and understanding of frequency analysis, which may affect their ability to implement suggested strategies effectively. There is also a discussion about the necessity of using specific functions from the itertools library to generate keys correctly.

Who May Find This Useful

This discussion may be useful for individuals interested in cryptography, programming in Python, or those seeking to improve their skills in deciphering encoded messages.

Arman777
Insights Author
Gold Member
Messages
2,163
Reaction score
191
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 ?
 
  • Like
Likes   Reactions: YoungPhysicist
Technology news on Phys.org
Is this a problem from cicada 3301?

 
  • Like
Likes   Reactions: 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.
 
  • Like
Likes   Reactions: 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.
 
  • Like
Likes   Reactions: 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
 
  • Like
Likes   Reactions: 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
 
  • #10
I have an idea on freq I ll try it
 
  • #11
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".
 
  • Like
Likes   Reactions: Arman777
  • #12
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)

I also add "the" condition.
 
  • #13
Arman777 said:
I am not that much a good coder.

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

BoB
 
  • Like
Likes   Reactions: jedishrfu
  • #14
I agree. However this thread is now quite old and the discussion is lost to time so I’ll close it.
 
  • Like
Likes   Reactions: rbelli1

Similar threads

  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 52 ·
2
Replies
52
Views
7K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
2
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K