-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncrypt_files_and_folder.py
More file actions
32 lines (27 loc) · 932 Bytes
/
Copy pathEncrypt_files_and_folder.py
File metadata and controls
32 lines (27 loc) · 932 Bytes
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
import sys
import os
from Cryptodome.Cipher import AES
from Cryptodome import Random
from binascii import b2a_hex
def encrypt_dir(path):
for root,_,files in os.walk("."):
for file in files:
file_path = os.path.join(root,file)
print(file_path + "is encrypting.")
encrypt_file(file_path)
def encrypt_file(path):
with open(path) as f:
plain_text =f.read()
key=b'this is a 16 key'
iv = Random.new().read(AES.block_size)
mycipher = AES.new(key,AES.MODE_CFB,iv)
ciphertext = iv + mycipher.encrypt(plain_text.encode())
with open(path + ".bin","wb") as file_out:
file_out.write(ciphertext[16:])
path = sys.argv[1]
if os.path.isdir(path) and os.path.exists(path):
encrypt_dir(path)
elif os.path.isfile(path) and os.path.exists(path):
encrypt_file(path)
else:
print("it's a special file(socket,FIFO,device file")