-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencryption.py
More file actions
56 lines (45 loc) · 1.84 KB
/
encryption.py
File metadata and controls
56 lines (45 loc) · 1.84 KB
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
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import padding
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
import os
def generate_aes_key(password, salt=b'salt_123'):
password = password.encode()
kdf = PBKDF2HMAC(
algorithm=hashes.SHA256(),
length=32, # 256-bit key for AES-256
salt=salt,
iterations=100000,
backend=default_backend()
)
key = kdf.derive(password)
return key
def pad(data):
padder = padding.PKCS7(128).padder()
padded_data = padder.update(data)
padded_data += padder.finalize()
return padded_data
def encrypt_file(file_path, key):
iv = b'1234567890123456' # IV should be 16 bytes for AES
with open(file_path, 'rb') as infile:
data = infile.read()
padded_data = pad(data)
cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend())
encryptor = cipher.encryptor()
encrypted_data = encryptor.update(padded_data) + encryptor.finalize()
encrypted_file_path = file_path + '.enc'
with open(encrypted_file_path, 'wb') as outfile:
outfile.write(iv)
outfile.write(encrypted_data)
os.remove(file_path)
if __name__ == "__main__":
folder_location = 'dot' # Replace with your dataset folder path
password = "MySecretPassword123"
generated_key = generate_aes_key(password)
print("Generated AES Key:", generated_key.hex())
for root, dirs, files in os.walk(folder_location):
for file in files:
if file.endswith('.txt'):
file_path = os.path.join(root, file)
encrypt_file(file_path, generated_key)