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