-
Notifications
You must be signed in to change notification settings - Fork 4
/
p10.py
37 lines (24 loc) · 849 Bytes
/
p10.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
from base64 import b64decode
from main import Solution
from p02 import xor
from Crypto.Cipher import AES
def aes_cbc_decrypt(ctxt: bytes, key: bytes, iv: bytes) -> bytes:
ptxt = b''
cipher = AES.new(key, AES.MODE_ECB)
prev_block = iv
for block in range(len(ctxt) // AES.block_size):
start = block * AES.block_size
end = start + AES.block_size
cur_block = ctxt[start:end]
tmp = cipher.decrypt(cur_block)
ptxt += xor(prev_block, tmp)
prev_block = cur_block
return ptxt
def p10() -> bytes:
key = b'YELLOW SUBMARINE'
iv = b'\x00' * AES.block_size
with open('Data/10.txt', 'rb') as f:
data = b64decode(f.read().replace(b'\n', b''))
return aes_cbc_decrypt(data, key, iv)
def main() -> Solution:
return Solution('10: Implement CBC mode', p10)