· 6 years ago · Aug 21, 2019, 04:06 PM
1import os, random, struct
2from Cryptodome.Cipher import AES
3
4def encrypt_file(key, input_file):
5 """ Encrypt a file using AES CBC mode.
6
7 key:
8 The encryption key - This is our secret to
9 encrypt and decrypt.
10 Should be 16 (AES-128), 24 (AES-192 ) or
11 32 bytes (AES-256) (characters) long.
12
13 input_file:
14 The input file
15 """
16
17 # This the block size of data we read, encrypt and write.
18 # It should be a multiple of 16 for AES.
19 chunksize=16*4*1024
20
21 output_filename = input_file + '.aes'
22
23 # 16 random bytes as initialization vector
24 initial_vector = os.urandom(16) # urandom: best method :)
25
26 encryptor = AES.new(key, AES.MODE_CBC, initial_vector)
27 filesize = os.path.getsize(input_file)
28
29 with open(input_file, 'rb') as in_file:
30 with open(output_filename, 'wb') as out_file:
31 # put the initial_vector at the beginning of the file
32 # we need to read from this position to decrypt correctly later
33 out_file.write(struct.pack('<Q', filesize))
34 out_file.write(initial_vector)
35
36 # go block for block through the data
37 while True:
38 # read a block of data
39 chunk = in_file.read(chunksize)
40 # if chunk == 0, we are at the end
41 if len(chunk) == 0:
42 break
43 # else we need to fill the last block of bytes up to 16
44 elif len(chunk) % 16 != 0:
45 chunk += b' ' * (16 - len(chunk) % 16)
46 # encrypt this block and append it to the file
47 out_file.write(encryptor.encrypt(chunk))
48
49
50def main():
51 # secret_key = b'--> I am a random secret key <--'
52
53 secret_key = input("Give me a secret key (16, 24 or 32 characters):").encode("utf-8")
54 while len(secret_key) != 16 and len(secret_key) != 24 and len(secret_key) != 32:
55 print("\nThere was an error! Please try again with 16, 24 or 32 characters:")
56 secret_key = input("\nGive me a secret key (16, 24 or 32 characters):").encode("utf-8")
57
58 path = input("\nGive me a path or file to encrypt:")
59 while os.path.isdir(path) != True and os.path.isfile(path) != True:
60 print("\nThere was an error! Please try again with a correct path or file to encrypt:")
61 path = input("\nGive me a path or file to encrypt:")
62
63 # path = directory?
64 if os.path.isdir(path):
65 files = []
66 # traverse all subfolders and save path + filename
67 for root, d_names, f_names in os.walk(path):
68 for f in f_names:
69 files.append(os.path.join(root, f)) # these are all files in all subfolders
70 for file in files:
71 # better skip already encrypted files
72 if not file.endswith(".aes"):
73 print("\nEncrypting ", file)
74 encrypt_file(secret_key, file)
75 # after encryption we could delete the file.. ;)
76 # just remove the '#' in the next line
77 # os.remove(file)
78
79 # path is no directory, but must be a file
80 else:
81 # better skip already encrypted files
82 if not path.endswith(".aes"):
83 print("\nEncrypting ", path)
84 encrypt_file(secret_key, path)
85 # after encryption we could delete the file.. ;)
86 # just remove the '#' in the next line
87 # os.remove(path)
88
89main()