-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge11.py
56 lines (36 loc) · 1.18 KB
/
challenge11.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
46
47
48
49
50
51
52
53
54
55
56
import random
from util import randombytes, chunk
from challenge7 import ecb_enc
from challenge10 import cbc_enc
ENC_BASE = b'A' * 100
def random_enc():
enc_data = randombytes(random.randint(5, 10)) + ENC_BASE + randombytes(random.randint(5, 10))
key = randombytes(16)
enc_method = random.choice(['ecb', 'cbc'])
if enc_method == 'ecb':
ciphertext = ecb_enc(enc_data, key)
elif enc_method == 'cbc':
ciphertext = cbc_enc(enc_data, key, randombytes(16))
else:
raise Exception(f'eh?: {enc_method}')
return ciphertext, enc_method
def guess_enc_method(ciphertext):
blocks = {}
for chk in chunk(ciphertext, 16):
if blocks.get(chk):
return 'ecb'
else:
blocks[chk] = True
return 'cbc'
def detect_enc():
ROUNDS = 100
correctly_guessed = 0
for i in range(ROUNDS):
ciphertext, enc_method = random_enc()
if enc_method == guess_enc_method(ciphertext):
correctly_guessed += 1
print(ROUNDS, correctly_guessed)
if __name__ == '__main__':
assert guess_enc_method(b'A'*100) == 'ecb'
assert guess_enc_method(randombytes(100)) == 'cbc'
detect_enc()