commit c2b2be0007dea5f4d526f313fcfd0934c9fd5b2b
parent c974e741092f409af7540ec5d60d401dfc458352
Author: mpizzzle <michael.770211@gmail.com>
Date: Sun, 8 Oct 2017 13:40:53 +0100
set 2 challenge 11 complete (I think?)
git push
Diffstat:
1 file changed, 42 insertions(+), 0 deletions(-)
diff --git a/set2/ecb_cbc_detection_oracle.py b/set2/ecb_cbc_detection_oracle.py
@@ -0,0 +1,42 @@
+from Crypto.Cipher import AES
+from Crypto import Random
+from Crypto.Random import random
+from sets import Set
+
+def random_key():
+ return Random.new().read(AES.block_size)
+
+def encryption_oracle(key, msg):
+ plaintext = msg
+ ciphertext = ""
+
+ for i in range(random.randint(5, 10)):
+ plaintext = '\x04' + plaintext
+ for i in range(random.randint(5, 10)):
+ plaintext += '\x04'
+
+ mod = len(plaintext) % 16
+
+ if mod != 0:
+ for i in range(16 - mod):
+ plaintext = plaintext + '\x04'
+
+ if random.randint(0, 1):
+ ciphertext = AES.new(key, AES.MODE_ECB).encrypt(plaintext)
+ print '1'
+ else:
+ ciphertext += AES.new(key, AES.MODE_CBC, random_key()).encrypt(plaintext)
+ print '0'
+
+ return ciphertext
+
+with open('files/10_decrypted.txt') as f:
+ file = f.read()
+
+ciphertext = encryption_oracle(random_key(), file)
+blocks = [ciphertext[i:i + 16] for i in range(0, len(ciphertext), 16)]
+
+if len(blocks) != len(Set(blocks)):
+ print '1'
+else:
+ print '0'