-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchallenge_8.py
48 lines (34 loc) · 1.19 KB
/
challenge_8.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
"""
Orel Ben-Reuven
https://cryptopals.com/sets/1/challenges/8
Detect AES in ECB mode
In this file are a bunch of hex-encoded ciphertexts.
One of them has been encrypted with ECB.
Detect it.
Remember that the problem with ECB is that it is stateless and deterministic;
the same 16 byte plaintext block will always produce the same 16 byte ciphertext.
"""
AES_BLOCK_SIZE = 16
def score_ecb_mode(cipher: bytes) -> float:
""" evaluate repetition of blocks """
blocks = []
for i in range(0, len(cipher), AES_BLOCK_SIZE):
blocks.append(cipher[i:i+AES_BLOCK_SIZE])
# evaluate number of distinct blocks relative to the total number of blocks
return len(set(blocks)) / len(blocks)
def main():
# load cypher and decode hex to bytes
with open('8.txt', 'r') as fh:
lines = fh.readlines()
ciphertext_list = list(map(bytes.fromhex, lines))
min_count = float('inf')
best_cipher = 0
for idx, ciphertext in enumerate(ciphertext_list):
count = score_ecb_mode(ciphertext)
if count < min_count:
min_count = count
best_cipher = idx
print(f'{best_cipher=}')
# best_cipher=132
if __name__ == '__main__':
main()