-
Notifications
You must be signed in to change notification settings - Fork 0
/
binpack.py
64 lines (58 loc) · 1.95 KB
/
binpack.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
import os
import struct
import hashlib
import binascii
from Crypto.Cipher import AES
'''
# \brief : bin file replace data
# file_path[string]: file path
# addr[int]: file offset
# data[bytes]:replace data
'''
def replace_data(file_path, addr, data):
with open(file_path, "rb+") as fd:
fd.seek(addr, 0)
fd.write(bytes(data))
def insert_data(file_path, addr, data, output_path):
with open(file_path, "rb") as fd :
file_data = fd.read()
with open(output_path, "wb") as output_fd:
if addr > len(file_data):
print("arg:addr error!")
return
output_fd.write(file_data[0:addr])
output_fd.write(bytes(data))
output_fd.write(file_data[addr:])
def md5_get(file_path):
with open(file_path, "rb") as fd:
md5 = hashlib.md5(fd.read()).hexdigest()
return bytes(md5, encoding="utf-8")
def aes_cbc_encrypt(file_path, key, iv, output_path):
cryptor = AES.new(key, AES.MODE_CBC, iv)
with open(file_path, "rb") as fd, open(output_path, "wb") as output_fd:
while True:
data = fd.read(16)
length = len(data)
#read end exit
if length <= 0:
break
elif length != 16:
data = data + (b'\0' * (16 - length))
enc_data = cryptor.encrypt(data)
output_fd.write(enc_data)
def aes_ecb_encrypt(file_path, key, output_path):
cryptor = AES.new(key, AES.MODE_ECB)
with open(file_path, "rb") as fd, open(output_path, "wb") as output_fd:
while True:
data = fd.read(16)
length = len(data)
#read end exit
if length <= 0:
break
elif length != 16:
data = data + (b'\0' * (16 - length))
enc_data = cryptor.encrypt(data)
output_fd.write(enc_data)
if __name__ == "__main__":
path = "./test.bin"
# aes_ecb_encrypt(path, b"1234567890123456", enc_path)