-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathp25.py
31 lines (21 loc) · 802 Bytes
/
p25.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
from base64 import b64decode
from os import urandom
from main import Solution
from p02 import xor
from p07 import aes_ecb_decrypt
from p18 import aes_ctr
def _edit_ctxt(ctxt: bytes, key: bytes, offset: int, newtext: bytes) -> bytes:
new_ctxt = aes_ctr(newtext, key)
return ctxt[:offset] + new_ctxt + ctxt[offset + len(new_ctxt):]
def p25() -> bytes:
with open('Data/25.txt', 'rb') as f:
data = b64decode(f.read().replace(b'\n', b''))
key = b'YELLOW SUBMARINE'
ptxt = aes_ecb_decrypt(data, key)
key = urandom(16)
ctxt = aes_ctr(ptxt, key)
newtext = b'A' * len(ctxt)
edited = _edit_ctxt(ctxt, key, 0, newtext)
return xor(xor(edited, ctxt), newtext)
def main() -> Solution:
return Solution('25: Break "random access read/write" AES CTR', p25)