cryptopals

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit a860e4e4bdbfb3542f23bfb91aaf23fa8dea5e8d
parent c2b2be0007dea5f4d526f313fcfd0934c9fd5b2b
Author: mpizzzle <michael.770211@gmail.com>
Date:   Sun,  8 Oct 2017 18:25:51 +0100

set 2 challenge 12 complete, a few steps missing though

Diffstat:
Aset2/byte_at_a_time_ecb_decryption.py | 31+++++++++++++++++++++++++++++++
1 file changed, 31 insertions(+), 0 deletions(-)

diff --git a/set2/byte_at_a_time_ecb_decryption.py b/set2/byte_at_a_time_ecb_decryption.py @@ -0,0 +1,31 @@ +import sys +from Crypto.Cipher import AES +from Crypto import Random +from Crypto.Random import random + +def random_key(): + return Random.new().read(AES.block_size) + +def encryption_oracle(key, msg): + plaintext = msg + + if len(msg) % 16 != 0: + for i in range(16 - (len(msg) % 16)): + msg += '\x04' + + return AES.new(key, AES.MODE_ECB).encrypt(msg) + +pt1 = "Um9sbGluJyBpbiBteSA1LjAKV2l0aCBteSByYWctdG9wIGRvd24gc28gbXkg\n" +pt2 = "aGFpciBjYW4gYmxvdwpUaGUgZ2lybGllcyBvbiBzdGFuZGJ5IHdhdmluZyBq\n" +pt3 = "dXN0IHRvIHNheSBoaQpEaWQgeW91IHN0b3A/IE5vLCBJIGp1c3QgZHJvdmUg\n" +pt4 = "YnkK" +aaa = "AAAAAAAAAAAAAAA" + +plaintext = aaa + str(pt1 + pt2 + pt3 + pt4).decode("base64") # no peeking! +key = random_key() + +for i in range(len(plaintext)): + dict = {encryption_oracle(key, aaa + chr(j)) : chr(j) for j in range(0xff)} + cipher = encryption_oracle(key, plaintext[i:]) + sys.stdout.write(dict[cipher[:AES.block_size]]) + aaa = aaa[1:] + dict[cipher[:AES.block_size]]