-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge11.py
37 lines (31 loc) · 1011 Bytes
/
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
#! python
import utils
import AES
def randomPadLen():
return utils.random_bytes(1)[0] % 6 + 5;
def encryption_oracle(input):
pad1 = utils.random_bytes(randomPadLen());
pad2 = utils.random_bytes(randomPadLen());
input = pad1 + input + pad2
if (utils.random_bool()):
print ('Performing CBC')
return ('CBC', AES.CBC_encrypt(AES.randomKey(), input, AES.randomIV()))
else:
print ('Performing ECB')
return ('ECB', AES.ECB_encrypt(AES.randomKey(), input))
def encryption_guess(input):
if (input[16:32] == input[32:48]):
return 'ECB'
return 'CBC'
def main():
for i in range(1000):
input = bytes(bytearray([0]*64))
(type, input) = encryption_oracle(input)
guess = encryption_guess(input);
if (guess == type):
print ("Guessed correctly: " + guess)
else:
print ("Failed to guess correctly")
exit(0)
if __name__ == "__main__":
main()