-
Notifications
You must be signed in to change notification settings - Fork 29
/
fb2enc.py
168 lines (149 loc) · 6.39 KB
/
fb2enc.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python3
import sys
import getopt
USAGE = '''Usage: fb2enc.py [OPTION]...
Encode or decode standard input to standard output.
-d, --decode decode data
-h, --help display this help and exit
'''
# Copied from original binary
ENCDATA = {
'M': [
0x95, 0x8f, 0x34, 0x69, 0x15, 0xf1, 0x65, 0x5f, 0xba, 0x2a, 0x30, 0x27,
0xa2, 0x1f, 0x3b, 0xe5, 0x81, 0xf7, 0x1a, 0x45, 0xcd, 0xff, 0x79, 0xb6,
0xc0, 0x79, 0xe9, 0x83, 0x47, 0x3f, 0xc9, 0xe4, 0xd7, 0x71, 0x11, 0x89,
0x8e, 0x44, 0x1c, 0x7c, 0xf5, 0x4d, 0xae, 0x07, 0x87, 0x34, 0x6c, 0xa0,
0xfd, 0x14, 0x5c, 0x17, 0xd3, 0x96, 0x91, 0xa7, 0x93, 0x3e, 0x79, 0x74,
0xd8, 0xef, 0x28, 0x2b, 0x6c, 0xce, 0xab, 0x5f, 0x91, 0xcd, 0x6e, 0x1e,
0xc0, 0xc9, 0x9a, 0x1a, 0xc9, 0x14, 0xb4, 0xd3, 0xae, 0x68, 0x68, 0x93,
0x06, 0x15, 0x5b, 0xeb, 0x26, 0x9f, 0xa6, 0xd9, 0xfd,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0xb8,
0xeb, 0x10, 0x35, 0x51, 0x79, 0xf8, 0x75, 0x9e, 0x11, 0x57, 0xf7, 0xcd,
0x10, 0x81, 0x7b, 0xff, 0x03, 0x59, 0x0b, 0x62, 0x3a, 0x7d, 0xb5, 0xec,
0x28, 0x63, 0x8d, 0xe8, 0x73, 0x55, 0x64, 0xcd, 0xbe, 0x54, 0xe2, 0xd9,
0xd6, 0x73, 0x3e, 0xd8, 0xef, 0x2c, 0x6f, 0x45, 0x87, 0x8e, 0xf8, 0xf0,
0xb4, 0x9d, 0x29, 0x69
],
'P': [
0x07, 0x0d, 0x0c, 0x04, 0x0f, 0x0e, 0x0b, 0x08, 0x03, 0x09, 0x0a, 0x05,
0x00, 0x06, 0x02, 0x01
],
'Pminus': [
0x0c, 0x0f, 0x0e, 0x08, 0x03, 0x0b, 0x0d, 0x00, 0x07, 0x09, 0x0a, 0x06,
0x02, 0x01, 0x05, 0x04
]
}
def log(str):
sys.stderr.write('%s: %s\n' % (sys.argv[0], str))
# Notice padding still occur even if (len(blk) % 16) == 0
def padding_pkcs7(blk):
rmd = 16 - (len(blk) % 16)
return blk + bytes([rmd] * rmd)
def encrypt_block(blk):
blk = list(blk)
buf = [0] * 16
for i in range(16):
for l in range(16):
buf[l] = blk[ENCDATA['P'][l]]
for j in range(16):
t = 0
for k in range(16):
t += ENCDATA['M'][16 * k + j] * buf[k]
blk[j] = t & 0xff
return bytes(blk)
def decrypt_block(blk):
blk = list(blk)
buf = [0] * 16
for i in range(16):
for j in range(16):
t = 0
for k in range(16):
t += ENCDATA['Mminus'][16 * k + j] * blk[k]
buf[j] = t & 0xff
for l in range(16):
blk[l] = buf[ENCDATA['Pminus'][l]]
return bytes(blk)
def encrypt(data):
data = padding_pkcs7(data)
enc = b''
for pos in range(0, len(data), 16):
enc += encrypt_block(data[pos : pos + 16])
return enc
def decrypt(data):
if len(data) % 16 != 0:
log('data corrupt: not padded')
exit(1)
dec = b''
for pos in range(0, len(data) - 16, 16):
dec += decrypt_block(data[pos : pos + 16])
lastblk = decrypt_block(data[-16 : ])
padlen = lastblk[-1]
if lastblk[-padlen : ] != bytes([padlen] * padlen):
log('data corrupt: padding error')
exit(1)
dec += lastblk[ : 16 - padlen]
return dec
def usage():
sys.stderr.write(USAGE)
def main():
flgEncode = True
try:
opts, args = getopt.getopt(sys.argv[1 : ], 'hd', ['help', 'decode'])
except getopt.GetoptError as e:
log(e)
return 1
for arg in args:
log('ignoring unknown argument "%s"' % arg)
for opt, arg in opts:
if opt in ('-h', '--help'):
usage()
return 1
elif opt in ('-d', '--decode'):
flgEncode = False
data = sys.stdin.buffer.read(1024)
data_next = sys.stdin.buffer.read(1024)
while data_next:
for pos in range(0, 1024, 16):
blk = data[pos : pos + 16]
sys.stdout.buffer.write(encrypt_block(blk) if flgEncode else decrypt_block(blk))
data = data_next
data_next = sys.stdin.buffer.read(1024)
sys.stdout.buffer.write(encrypt(data) if flgEncode else decrypt(data))
return 0
if __name__ == '__main__':
try:
exit(main())
except KeyboardInterrupt:
exit(130)