-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge_10.py
More file actions
34 lines (25 loc) · 887 Bytes
/
challenge_10.py
File metadata and controls
34 lines (25 loc) · 887 Bytes
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
from base64 import b64decode
from challenge_02 import bytes_xor
from challenge_07 import aes_ecb_dec, AES
from challenge_08 import bytes_to_chunks
from challenge_09 import strip_pkcs7
BLOCK_SIZE = AES.block_size
def aes_cbc_dec(iv: bytes, key: bytes, ciphertext: bytes, use_pkcs7=True) -> bytes:
blocks = bytes_to_chunks(ciphertext, BLOCK_SIZE)
prev_ct = iv
plaintext = b""
for block in blocks:
raw_dec = aes_ecb_dec(key, block)
plaintext += bytes_xor(raw_dec, prev_ct)
prev_ct = block
if use_pkcs7:
plaintext = strip_pkcs7(plaintext)
return plaintext
if __name__ == "__main__":
with open("data/10.txt") as f:
data_b64 = f.read()
ciphertext = b64decode(data_b64)
key = b'YELLOW SUBMARINE'
iv = bytes(BLOCK_SIZE)
plaintext = aes_cbc_dec(iv, key, ciphertext)
print(plaintext.decode("ascii"))