-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathencryptor.py
53 lines (41 loc) · 1.35 KB
/
encryptor.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
import json
import os
from base64 import b64encode
from Crypto.Cipher import AES
KEY = bytes(
[
57, 114, 107, 120, 67, 80, 108, 106, 83,
77, 49, 71, 86, 81, 104, 87, 119, 49, 114,
49, 114, 75, 79, 72, 71, 99, 99, 98, 50, 105, 74, 53
]
)
SKIPPED_FILES = [
"profile_flag.json",
]
def encrypt(opentext: str) -> str:
aes = AES.new(KEY, AES.MODE_CBC, bytes(16))
ciphertext = aes.encrypt(opentext.encode())
buffer = b64encode(ciphertext)
return buffer.decode()
def validate_json(json_data):
if json_data.get("encrypted", False):
return False
if "data" not in json_data:
return False
return True
def main():
for dirpath, dirnames, filenames in os.walk("./profile"):
for filename in filenames:
with open(os.path.join(dirpath, filename), "r") as file:
if filename in SKIPPED_FILES:
continue
json_data = json.loads(file.read())
if not validate_json(json_data):
continue
json_data["data"] = encrypt(json_data["data"])
with open(os.path.join(dirpath, filename), "w") as file:
if json_data.get("encrypted"):
json_data["encrypted"] = True
file.write(json.dumps(json_data))
if __name__ == '__main__':
main()