kopia lustrzana https://github.com/fagci/qs-uvk5-firmware-modder
refactor
rodzic
9d3d15dd54
commit
e56e56dcd2
59
encdec.py
59
encdec.py
|
@ -1,10 +1,40 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
from binascii import crc_hqx
|
||||
from itertools import cycle
|
||||
import os
|
||||
from pathlib import Path
|
||||
from sys import argv
|
||||
from sys import stderr
|
||||
|
||||
from lib.base import eprint, encrypt, decrypt
|
||||
# Structure of pre-encoded payload
|
||||
# 8196 | 16 | ... | 2 |
|
||||
# data | version | data | crc |
|
||||
|
||||
KEY = Path('./key.bin').read_bytes()
|
||||
|
||||
V_OFFSET = 8192
|
||||
V_LEN = 16
|
||||
CRC_LEN = 2
|
||||
|
||||
def eprint(*args, **kwargs):
|
||||
print(*args, **kwargs, file=stderr)
|
||||
|
||||
|
||||
def xor(var):
|
||||
return bytes(a ^ b for a, b in zip(var, cycle(KEY)))
|
||||
|
||||
|
||||
def decrypt(data):
|
||||
decrypted = xor(data)
|
||||
eprint('version:', decrypted[V_OFFSET:V_OFFSET+V_LEN].decode())
|
||||
return decrypted[:-CRC_LEN]
|
||||
|
||||
|
||||
def encrypt(data):
|
||||
encrypted = xor(data)
|
||||
checksum = crc_hqx(encrypted, 0).to_bytes(2, byteorder='little')
|
||||
return encrypted + checksum
|
||||
|
||||
|
||||
def usage(info = None):
|
||||
|
@ -17,24 +47,19 @@ def usage(info = None):
|
|||
|
||||
|
||||
def main():
|
||||
if len(argv) != 3:
|
||||
usage()
|
||||
if len(argv) == 3:
|
||||
mode = argv[1]
|
||||
file_bytes = Path(argv[2]).read_bytes()
|
||||
|
||||
encdec = argv[1]
|
||||
fname = argv[2]
|
||||
file_bytes = Path(fname).read_bytes()
|
||||
if mode == 'd':
|
||||
os.write(1, decrypt(file_bytes))
|
||||
eprint('Success!')
|
||||
return
|
||||
|
||||
if encdec == 'd':
|
||||
decrypted = decrypt(file_bytes)
|
||||
os.write(1, bytes(decrypted))
|
||||
eprint('Success!')
|
||||
return
|
||||
|
||||
if encdec == 'e':
|
||||
encrypted = encrypt(file_bytes)
|
||||
os.write(1, bytes(encrypted))
|
||||
eprint('Success!')
|
||||
return
|
||||
if mode == 'e':
|
||||
os.write(1, encrypt(file_bytes))
|
||||
eprint('Success!')
|
||||
return
|
||||
|
||||
usage()
|
||||
|
||||
|
|
37
lib/base.py
37
lib/base.py
|
@ -1,37 +0,0 @@
|
|||
from itertools import cycle
|
||||
from pathlib import Path
|
||||
from sys import stderr
|
||||
from binascii import crc_hqx
|
||||
|
||||
# Structure of pre-encoded payload
|
||||
# 8196 | 16 | ... | 2 |
|
||||
# data | version | data | crc |
|
||||
|
||||
KEY = Path('./key.bin').read_bytes()
|
||||
|
||||
V_OFFSET = 8192
|
||||
V_LEN = 16
|
||||
CRC_LEN = 2
|
||||
|
||||
def eprint(*args, **kwargs):
|
||||
print(*args, **kwargs, file=stderr)
|
||||
|
||||
|
||||
def xor(var):
|
||||
return bytes(a ^ b for a, b in zip(var, cycle(KEY)))
|
||||
|
||||
|
||||
def make_16byte_version(version):
|
||||
return bytes([ord(c) for c in version] + [0] * (16 - len(version)))
|
||||
|
||||
|
||||
def decrypt(data):
|
||||
decrypted = xor(data)
|
||||
eprint('version:', decrypted[V_OFFSET:V_OFFSET+V_LEN])
|
||||
return decrypted[:-CRC_LEN]
|
||||
|
||||
|
||||
def encrypt(data):
|
||||
encrypted = xor(data)
|
||||
checksum = crc_hqx(encrypted, 0).to_bytes(2, byteorder='little')
|
||||
return encrypted + checksum
|
Ładowanie…
Reference in New Issue