· 6 years ago · Aug 23, 2019, 06:34 AM
1# Author: howdoyouturnthison
2# How to encrypt and decrypt your data with AES and python:
3# https://www.youtube.com/watch?v=YqY6H1IPBDY
4
5import os, random, struct, sys
6from Cryptodome.Cipher import AES
7
8
9def decrypt_file(key, input_file):
10 """ Decrypts a file using AES CBC mode with the
11 given key.
12 """
13
14 # This the block size of data we read, decrypt and write.
15 # It should be a multiple of 16 for AES.
16 chunksize=16*4*1024
17
18 # get rid of the .aes ending
19 output_filename = os.path.splitext(input_file)[0]
20
21 try:
22 with open(input_file, 'rb') as in_file:
23 # retrieve the original size of the encrypted data
24 original_size = struct.unpack('<Q', in_file.read(struct.calcsize('Q')))[0]
25 # retrieve the initialization vector
26 initial_vector = in_file.read(16)
27 decryptor = AES.new(key, AES.MODE_CBC, initial_vector)
28
29 # read block for block the encrypted data, decrypt and write
30 # sometimes decrypted files are already in place and
31 # in use by the system and not writeable e.g. Thumbs.db
32 try:
33 with open(output_filename, 'wb') as out_file:
34 while True:
35 chunk = in_file.read(chunksize)
36 if len(chunk) == 0:
37 break
38 out_file.write(decryptor.decrypt(chunk))
39 # restore original size
40 out_file.truncate(original_size)
41 except PermissionError:
42 print("\nCouldn't write ", output_filename)
43 except PermissionError:
44 print("\nCouldn't read ", input_file)
45
46def main():
47 #secret_key = b'--> I am a random secret key <--'
48
49 secret_key = input("Give me your secret key (16, 24 or 32 characters):").encode("utf-8")
50 while len(secret_key) != 16 and len(secret_key) != 24 and len(secret_key) != 32:
51 print("\nThere was an error! Please try again with 16, 24 or 32 characters:")
52 secret_key = input("\nGive me your secret key (16, 24 or 32 characters):").encode("utf-8")
53
54 path = input("\nGive me a path or file to decrypt:")
55 while os.path.isdir(path) != True and os.path.isfile(path) != True:
56 print("\nThere was an error! Please try again with a correct path or file to decrypt:")
57 path = input("\nGive me a path or file to decrypt:")
58
59 # path = directory?
60 if os.path.isdir(path):
61 files = []
62 # traverse all subfolders and save path + filename
63 for root, d_names, f_names in os.walk(path):
64 for f in f_names:
65 files.append(os.path.join(root, f)) # these are all files in all subfolders
66 for file in files:
67 # better skip not encrypted files
68 if file.endswith(".aes"):
69 print("\nDecrypting ", file)
70 decrypt_file(secret_key, file)
71 # after decryption we could delete the file.. ;)
72 # just remove the '#' in the next line
73 #os.remove(file)
74
75 # path is no directory, but must be a file
76 else:
77 # better skip not encrypted files + dirty fix
78 if path.endswith(".aes") and not path.endswith("Thumbs.db.aes"):
79 print("\nDecrypting ", path)
80 decrypt_file(secret_key, path)
81 # after decryption we could delete the file.. ;)
82 # but think about typing in the wrong key...
83 # just remove the '#' in the next line
84 #os.remove(path)
85
86if __name__ == '__main__':
87 main()