-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchallenge10.py
39 lines (33 loc) · 875 Bytes
/
challenge10.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
import base64
from Crypto.Cipher import AES
import challenge2
key = b'YELLOW SUBMARINE'
block_size = 16
cipher = AES.new(key, AES.MODE_ECB)
iv = bytes([0] * block_size)
def getBlocks(s):
return [s[i * block_size : (i + 1) * block_size] for i in range(len(s) // block_size)]
def CBCEncrypt(s):
blocks = getBlocks(s)
cipher_text = b''
prv = iv
for i in range(len(blocks)):
curblock = cipher.encrypt(challenge2.fixedXor(blocks[i], prv))
cipher_text += curblock
prv = curblock
return cipher_text
def CBCDecrypt(s):
blocks = getBlocks(s)
plain_text = b''
prv = iv
for i in range(len(blocks)):
curblock = challenge2.fixedXor(cipher.decrypt(blocks[i]), prv)
plain_text += curblock
prv = blocks[i]
return plain_text
def main():
f = open('data10.txt', 'r')
s = base64.b64decode(f.read())
print(CBCDecrypt(s).decode())
if __name__ == '__main__':
main()