-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaes_encrypt.py
59 lines (42 loc) · 1.68 KB
/
aes_encrypt.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
48
49
50
51
52
53
54
55
56
57
58
59
# encryption_script.py
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad, unpad
from Crypto.Random import get_random_bytes
# 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
# Function to perform AES encryption
def aes_encrypt(data, key):
cipher = AES.new(key, AES.MODE_ECB)
ciphertext = cipher.encrypt(pad(data, AES.block_size))
return ciphertext
def aes_decrypt(ciphertext, key):
cipher = AES.new(key, AES.MODE_ECB)
decrypted_data = unpad(cipher.decrypt(ciphertext), AES.block_size)
return decrypted_data.decode('utf-8')
# 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)
# Get the input data from the file
input_file_path = 'part1.txt'
with open(input_file_path, 'r') as input_file:
part1 = input_file.read().strip()
# Convert the input data to bytes
data_bytes = part1.encode('utf-8')
# Encrypt the data using AES
encrypted_data = aes_encrypt(data_bytes, aes_key)
# Save the encrypted data to a file
output_file_path = 'aes_ciphertext'
with open(output_file_path, 'wb') as output_file:
output_file.write(encrypted_data)
print(f"AES encryption complete. Ciphertext saved to '{output_file_path}'.")
# Decrypt the data using AES
decrypted_data = aes_decrypt(encrypted_data, aes_key)
# 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}'.")