-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathencrypt.py
39 lines (30 loc) · 1.05 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
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):
# get the plaintext
with open(path) as f:
plain_text = f.read()
# The key length must be 16 (AES-128), 24 (AES-192), or 32 (AES-256) Bytes.
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())
# output
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)")