-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchallenge10.py
39 lines (25 loc) · 905 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
from Crypto.Cipher import AES
from base64 import b64decode
from challenge7 import ecb_dec
from challenge9 import pkcs7
from util import chunk, xor, randombytes
def cbc_dec(ciphertext, key, iv):
prevciph = iv
plaintext = b''
for cipherchk in chunk(ciphertext, 16):
ax = ecb_dec(cipherchk, key)
plainchk = xor(ax, prevciph)
prevciph = cipherchk
plaintext += plainchk
return plaintext
def cbc_enc(plaintext, key, iv):
cipher = AES.new(key, AES.MODE_CBC, iv)
return bytearray(cipher.encrypt(pkcs7(plaintext)))
if __name__ == '__main__':
with open('assets/10ciphertext.txt', 'r') as file_:
data = file_.read()
data = data.replace('\n', '')
ciphertext = b64decode(data)
plaintext = cbc_dec(ciphertext, b'YELLOW SUBMARINE', b'\x00'*16)
print(plaintext)
print(cbc_enc(b'123', randombytes(16), randombytes(16)))