-
Notifications
You must be signed in to change notification settings - Fork 0
/
p11.py
31 lines (26 loc) · 805 Bytes
/
p11.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
import os
from random import randint
from Crypto.Cipher import AES
from tools.oracles import detectMode
def encryptionOracle(plain, mode):
key = os.urandom(16)
plain = os.urandom( randint(5,10) ) + plain + os.urandom( randint(5,10) )
padding = 16 - (len(plain) % 16)
plain += chr(padding) * padding
if mode == "CBC":
cypher = AES.new(key, AES.MODE_CBC, os.urandom(16) )
else:
mode = "ECB"
cypher = AES.new(key, AES.MODE_ECB, os.urandom(16) )
return cypher.encrypt( plain )
if __name__ == '__main__':
num = 10000
correct = 0
for i in range(0,num):
mode = "CBC"
if randint(0,10) % 2:
mode = "ECB"
def f (x) : return encryptionOracle(x, mode)
if mode == detectMode(f):
correct += 1
print str(100 * correct / num) + "% Correctly detected"