-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontainer.template.py
62 lines (48 loc) · 1.65 KB
/
container.template.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from base64 import b64decode
from lzma import decompress
from os import path
from sys import argv
# FILE INFORMATION
original_filename = "${name}"
custom_message = "${msg}"
is_base_encoded = ${base64_enc}
is_aes_encrypted = ${aes_enc}
is_compressed = ${compression}
aes_nonce = ${nonce}
aes_tag = ${tag}
blob_repr = ${bin}
if __name__ == '__main__':
# Print custom message
if custom_message:
print(f"\n{' - ' * 12}\n{custom_message}\n{' - ' * 12}\n")
# Confirm file recovery
if input(f"Recover original file ? [Y/n] ") in {'n', 'N'}:
print("Cancelling.")
exit()
print(f"Recovering \'{original_filename}\'... ")
if is_base_encoded:
blob_repr = b64decode(blob_repr)
if is_aes_encrypted:
from Crypto.Cipher import AES
from Crypto.Hash import SHA3_256
good_password = False
while not good_password:
password = input("Password : ")
hasher = SHA3_256.new(password.encode('utf-8'))
key_hash = hasher.digest()
cipher = AES.new(key_hash, AES.MODE_EAX, aes_nonce)
try:
blob_repr = cipher.decrypt_and_verify(blob_repr, aes_tag)
except ValueError:
print("Wrong password.")
else:
good_password = True
if is_compressed:
blob_repr = decompress(blob_repr)
while path.exists(original_filename):
original_filename = input("File already exists. New name: ")
with open(original_filename, 'wb') as dest_file:
size = dest_file.write(blob_repr)
print(f"Done. {size // 1000}kB written.")