-
Notifications
You must be signed in to change notification settings - Fork 4
/
p27.py
38 lines (28 loc) · 1023 Bytes
/
p27.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
from binascii import hexlify
from os import urandom
from main import Solution
from p02 import xor
from p10 import aes_cbc_decrypt
from p11 import aes_cbc_encrypt
from Crypto.Cipher import AES
def _check_ascii_compliant(msg: bytes):
for c in msg:
if c < 32:
raise ValueError(msg)
def p27() -> bytes:
key = urandom(16)
print(f'The key is {hexlify(key).decode()}')
msg = b'Super secret message unfortunately encrypted in a bad manner'
ctxt = aes_cbc_encrypt(msg, key, key)
c1 = ctxt[:AES.block_size]
zeros = b'\x00' * AES.block_size
ctxt = c1 + zeros + c1 + ctxt[3 * AES.block_size:]
try:
plaintext = aes_cbc_decrypt(ctxt, key, key)
return _check_ascii_compliant(plaintext)
except ValueError as e:
ptxt = e.args[0]
p1, p3 = ptxt[:AES.block_size], ptxt[2 * AES.block_size:3 * AES.block_size]
return b'Recovered ' + hexlify(xor(p1, p3))
def main():
return Solution('27: Recover the key from CBC with IV=Key', p27)