-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdecode.py
executable file
·72 lines (60 loc) · 1.94 KB
/
decode.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
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
from Crypto.Cipher import AES
import hashlib
import os
import sys
import re
import json
directory = sys.argv[1] if len(sys.argv) > 1 else "."
key = "HiReg-5D765B15-8F5B-46DC-9B7C-80322B8F74E4"
def decrypt(content, key):
try:
aes_key = hashlib.md5(key.encode()).digest()
cipher = AES.new(aes_key, AES.MODE_ECB)
return cipher.decrypt(content).decode()
except ValueError as e:
print(f"Invalid key: {str(e)}")
return None
def getjsonprofile(filename, content):
var = {}
variable_pattern = re.compile(r'(\w+)=(.+)')
for match in variable_pattern.finditer(content):
name = match.group(1)
value = match.group(2)
var[name] = value
json_data = {
"name": filename,
"DDRSTEP0": [int(x, 16) for x in var['DDRSTEP0'].split(',')],
"ADDRESS": [
var['ADDRESS0'],
var['ADDRESS1'],
var['ADDRESS2']
],
"FILELEN": [
var['FILELEN0'],
var['FILELEN1']
],
"STEPLEN": [
var['STEPLEN0'],
var['STEPLEN1']
]
}
return json_data
def main():
for filename in os.listdir(directory):
if filename.endswith(".chip"):
name, _ = os.path.splitext(filename)
filepath = os.path.join(directory, filename)
try:
with open(filepath, 'rb') as file:
content = file.read()
print(f"Decoding {filename}")
decrypted_content = decrypt(content, key).replace('\r','')
jsonprofile = getjsonprofile(name.lower(), decrypted_content)
if jsonprofile:
with open(directory + '/' + name.lower() + '.json', 'w') as f:
json.dump(jsonprofile, f)
except Exception as e:
print(f"Error processing {filename}: {str(e)}")
if __name__ == "__main__":
main()