-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathq3-8.py
More file actions
50 lines (40 loc) · 1.47 KB
/
Copy pathq3-8.py
File metadata and controls
50 lines (40 loc) · 1.47 KB
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
from Crypto.Cipher import AES
def AES_encrypt(k, pt):
encryptor = AES.new(k.decode('hex'), AES.MODE_ECB)
ct = encryptor.encrypt(pt.decode('hex'))
return ct.encode('hex')
def AES_decrypt(k, ct):
encryptor = AES.new(k.decode('hex'), AES.MODE_ECB)
pt = encryptor.decrypt(ct.decode('hex'))
return pt.encode('hex')
def f1(x, y):
ct = AES_encrypt(y, x)
ct = ct.decode('hex')
y = y.decode('hex')
f1 = map(lambda v:"%02.0x" % (ord(v[0])^ord(v[1])), zip(ct, y))
return "".join(f1)
def f2(x, y):
ct = AES_encrypt(x, x)
ct = ct.decode('hex')
y = y.decode('hex')
f2 = map(lambda v:"%02.0x" % (ord(v[0])^ord(v[1])), zip(ct, y))
return "".join(f2)
def strxor(a, b): # xor two strings of different lengths
if len(a) > len(b):
return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a[:len(b)], b)])
else:
return "".join([chr(ord(x) ^ ord(y)) for (x, y) in zip(a, b[:len(a)])])
x1 = '00000000000000000000000000000000'
y1 = 'ffffffffffffffffffffffffffffffff'
y2 = 'cccccccccccccccccccccccccccccccc'
temp = strxor(strxor(AES_encrypt(y1,x1).decode('hex'),y1.decode('hex')),y2.decode('hex'))
x2 = AES_decrypt(y2,temp.encode('hex'))
print x2
x3 = '00000000000000000000000000000000'
y3 = 'ffffffffffffffffffffffffffffffff'
x4 = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
temp = strxor(f2(x3,y3).decode('hex'),AES_encrypt(x4,x4).decode('hex'))
y4 = temp.encode('hex')
print y4
print f1(x1, y1) == f1(x2, y2)
print f2(x3, y3) == f2(x4, y4)