-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge27.py
45 lines (35 loc) · 1.03 KB
/
challenge27.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
40
41
42
43
44
45
#!python
import AES
from utils import *
key = AES.randomKey()
iv = bytes(key)
def enc(url):
return AES.CBC_encrypt(key, url, iv)
def is_ascii(data):
for b in data:
if b>127:
return False
return True
def dumb_dec(cipher):
plain = AES.CBC_decrypt(key, bytes(cipher), iv)
if (not (is_ascii(plain))):
raise ValueError(plain);
return plain
def main():
url = b'https://cryptopals.com/sets/4/challenges/27 '
# Encrypt URL
cipher = enc(url)
# Attack the ciphertext + add correct padding
attack_cipher = cipher[0:16] + bytes([0]*16) + cipher[0:16] + cipher[48:]
#Decrypt attacked ciphertext
plain=b''
try:
plain = dumb_dec(attack_cipher)
except Exception as error:
plain = bytes(error.args[0])
# Recover IV (which is the key)
recover_key = xor_bytes(plain[0:16], plain[32:48]);
print ('Recover key = ', list(recover_key))
print ('Real key = ', list(key))
if __name__ == "__main__":
main()