forked from jvdsn/crypto-attacks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plaintext_recovery.py
26 lines (25 loc) · 919 Bytes
/
plaintext_recovery.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
def attack(encrypt_oracle, unused_byte=0):
"""
Recovers a secret which is appended to a plaintext and encrypted using ECB.
:param encrypt_oracle: the encryption oracle
:param unused_byte: a byte that's never used in the secret
:return: the secret
"""
paddings = [bytes([unused_byte] * i) for i in range(16)]
secret = bytearray()
while True:
padding = paddings[15 - (len(secret) % 16)]
p = bytearray(padding + secret + b"0" + padding)
byte_index = len(padding) + len(secret)
end1 = len(padding) + len(secret) + 1
end2 = end1 + len(padding) + len(secret) + 1
for i in range(256):
p[byte_index] = i
c = encrypt_oracle(p)
if c[end1 - 16:end1] == c[end2 - 16:end2]:
secret.append(i)
break
else:
secret.pop()
break
return bytes(secret)