-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaes_decrypt_final.py
47 lines (36 loc) · 1.35 KB
/
aes_decrypt_final.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# decryption_code.py
from Crypto.Cipher import AES
from Crypto.Util.Padding import unpad
# Function to read the AES key from a file
def read_aes_key(file_path):
with open(file_path, 'rb') as file:
key = file.read()
return key
def aes_decrypt(ciphertext, key):
cipher = AES.new(key, AES.MODE_ECB)
try:
decrypted_data = unpad(cipher.decrypt(ciphertext), AES.block_size)
return decrypted_data.decode('utf-8')
except ValueError as e:
print(f"Decryption error: {e}")
return None
# Rest of the code remains unchanged
# File path for the AES key
key_file_path = 'keys/aes_key.bin'
# Read the AES key from the file
aes_key = read_aes_key(key_file_path)
# Read the encrypted data from the file (replace with the actual file path)
input_file_path = 'aes_ciphertext'
with open(input_file_path, 'rb') as input_file:
encrypted_data = input_file.read()
# Decrypt the data using AES
decrypted_data = aes_decrypt(encrypted_data, aes_key)
if decrypted_data is not None:
print(f"Decrypted data: {decrypted_data}")
else:
print("Decryption failed.")
# Save the decrypted data to a file
decrypted_file_path = 'decrypt1.txt'
with open(decrypted_file_path, 'w') as decrypted_file:
decrypted_file.write(decrypted_data)
print(f"Decrypted data saved to '{decrypted_file_path}'.")