- 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:
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.
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.