Output confusion

Joined
Mar 9, 2023
Messages
1
Reaction score
0
Hello Everyone,
This is my first post on here I had a project that I turned in for one of my classes(probably not for a good grade lol), and I was having trouble with some Python coding. It is a cryptography class, and I am not well versed in Python but I needed to use it for the libraries, and I am mainly just having trouble with the output. I am supposed to use brute force to find a key for an encrypted text using various methods and blah blah blah. The code is as follows:


Code:
from Crypto.Cipher import AES
import itertools

# Define the known plaintext-ciphertext pairs and nonces
plaintext1 = open('m1.txt', 'rb').read()
ciphertext1 = open('c1.bin', 'rb').read()
nonce1 = open('nonce1.bin', 'rb').read()

plaintext2 = open('m2.txt', 'rb').read()
ciphertext2 = open('c2.bin', 'rb').read()
nonce2 = open('nonce2.bin', 'rb').read()

plaintext3 = open('m3.txt', 'rb').read()
ciphertext3 = open('c3.bin', 'rb').read()
nonce3 = open('nonce3.bin', 'rb').read()

print(plaintext1)
print(ciphertext1)
print(nonce1)
print() #I dont know how to do breaks that dont mess up the syntax of the code for some reason
print(plaintext2)
print(ciphertext2)
print(nonce2)
print()
print(plaintext3)
print(ciphertext3)
print(nonce3)
print()

# Define the challenge ciphertext and nonce
challenge_ciphertext = open('c_c.bin', 'rb').read()
challenge_nonce = open('nonce_c.bin', 'rb').read()

# Generate all possible values for the last 3 bytes of the key
key_suffixes = itertools.product(range(256), repeat=3)

# Iterate over all possible keys
for suffix in key_suffixes:
    # Construct the key with the correct prefix and suffix
    key = bytes([0x80] + [0x00]* 12 + list(suffix))

    # Use the key and nonce to encrypt the plaintexts
    cipher1 = AES.new(key, AES.MODE_CTR, nonce=nonce1)
    cipher2 = AES.new(key, AES.MODE_CTR, nonce=nonce2)
    cipher3 = AES.new(key, AES.MODE_CTR, nonce=nonce3)
    
    # Check if the ciphertexts match the known values
    if cipher1.encrypt(plaintext1) == ciphertext1 and cipher2.encrypt(plaintext2) == ciphertext2 and cipher3.encrypt(plaintext3) == ciphertext3:
        print("Key found:", key)
        break
else:
    print("Key not found. :(")

print()
print(challenge_ciphertext)
print(challenge_nonce)
print()

cipher_challenge = AES.new(key, AES.MODE_EAX, nonce=challenge_nonce)
challenge_plaintext = cipher_challenge.decrypt(challenge_ciphertext)

#was having trouble simply printing the message so I am just going to send it to the file first
with open('secretmsg.txt', 'w', encoding='utf-8') as f:
    f.write(challenge_plaintext.decode('latin-1'))
    
with open('secretmsg.txt', 'r', encoding='utf-8') as f:
    plaintext = f.read()

print('The super secret plain text is:', plaintext)


Output:
b'GET /home.html HTTP/1.1'
b'\x91_{eO\x116\xe7\xa1\xf9\x17t\x1c\t\x073&\x14t<\x9e\x02\xd3'
b'\xfd\x1b\xa6"\xcf\x9f?k'

b'Host: developer.mozilla.org'
b'\x19\x11\x01\x91\xf5\xcc\xed\x1c3Z\x9c\xb5\xc4\xe5\xdf\xe2\x00 \xc8\xa2n\xe4\xc0\xa9\xc1\xb3\xfe'
b'k\xe1`\x007\x14\x91\xe7'

b'User-Agent: Mozilla/5.0'
b'\xec\x1d\xeb\x9a\xe5\xd9\x07\xaa6\x15\x9b\x86/Ek\xd5\x08\xc2?@@[\xd5'
b'\xbd\xf80\x0f ?\xe5u'

Key found: b'\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00_\xeed'

b'9W\xe5ib\xcd\xac>Wz\xef+N(\x084\xf3\x84\xa4wgl\xf5)e\xb6<\xf6%+\xf4\x0f\xcd \x87\xca\x91\x01\x93^N'
b'\x08\xf4\xdbb+8E\x97'

The super secret plain text is: ÌòWÑ;¿Î1P]þiÞ¨éªx[6õ{µø,wxëÏ9)`



I am mainly having trouble with it coming out in proper readable English. I feel as if I did the code right it's just the output formatting I am having trouble with. How would I print out the decrypted plaintext message as well as write it to a file? Feel free to edit anything that you think may improve it. I can attach my zip file below if you want to work on it a bit more but no big deal if you don't.
Thanks.
 

Attachments

  • challenge.zip
    50.1 KB · Views: 9
Joined
Sep 21, 2022
Messages
126
Reaction score
16
The first part of the program uses encrypt, the second part uses decrypt.

A bug will be easier to locate if the first part of the program is testing keys with decrypt.

If the correct key is not found, then the decrypt is the problem, not the output.
 
Joined
Mar 15, 2023
Messages
5
Reaction score
0
Based on the code you provided, it seems that the decrypted plaintext message is being written to a file named 'secretmsg.txt' using the following code:

Python:
with open('secretmsg.txt', 'w', encoding='utf-8') as f:
    f.write(challenge_plaintext.decode('latin-1'))

To print the decrypted plaintext message to the console, you can add the following line of code after reading the plaintext from the file:

Python:
print('The decrypted plaintext message is:', plaintext)

Regarding the formatting of the output, the decrypted plaintext message appears to be in some non-English language, possibly due to the use of a different character encoding. The code you provided uses 'latin-1' as the decoding format when writing the plaintext to the file, so you can try using the same format when reading the file:

Python:
with open('secretmsg.txt', 'r', encoding='latin-1') as f:
    plaintext = f.read()

Alternatively, you can try different encoding formats to see if they produce a more readable output. Some common encoding formats are 'utf-8' and 'ascii'. You can replace 'latin-1' with the desired encoding format in both the write and read statements.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,813
Messages
2,569,696
Members
45,483
Latest member
TedDvb6626

Latest Threads

Top