-
Notifications
You must be signed in to change notification settings - Fork 4
/
common.py
53 lines (41 loc) · 1.12 KB
/
common.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 urllib
import sys
import os
import struct
from Crypto.Cipher import AES
import hashlib
def be32(s):
return struct.unpack('>I', s)[0]
def be16(s):
return struct.unpack('>H', s)[0]
def get_key(name):
try:
return open(os.environ['WIIU']+'/keys/'+name,'rb').read()
except:
print 'Key \'%s\' not found..' % name
print 'You need to set WIIU environment variable.'
sys.exit(1)
def aes_cbc_dec_file(inf, outf, key, iv, offset=0):
in_file = open(inf, 'rb')
out_file = open(outf, 'wb')
in_file.seek(offset)
while True:
cipher = AES.new(key, AES.MODE_CBC, iv)
enc = in_file.read(16)
if len(enc) == 0:
break
if len(enc) < 16:
break
dec = cipher.decrypt(enc)
out_file.write(dec)
iv = enc
in_file.close()
out_file.close()
def aes_cbc_dec(inb, key, iv='\x00'*16):
return AES.new(key, AES.MODE_CBC, iv).decrypt(inb)
def aes_cbc_enc(inb, key, iv='\x00'*16):
return AES.new(key, AES.MODE_CBC, iv).encrypt(inb)
def sha1(buf):
m = hashlib.sha1()
m.update(buf)
return m.digest()