Created
April 18, 2015 02:30
-
-
Save ztgrace/d99b8dd77aa7bdc07864 to your computer and use it in GitHub Desktop.
AES ECB chosen plaintext attack example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from Crypto.Cipher import AES | |
import sys | |
def split_len(seq, length): | |
return [seq[i:i+length] for i in range(0, len(seq), length)] | |
def oracle(chosen): | |
secret = "foobarbaz1234567890%sSecret42" % chosen # target to decrypt | |
secret = getPadding(secret) | |
if display: | |
displaySecret(secret) # For illustrative purposes | |
ct = cipher.encrypt(secret) | |
return ct | |
def getPadding(secret): | |
pl = len(secret) | |
mod = pl % 16 | |
if mod != 0: | |
padding = 16 - mod | |
secret += "X" * padding | |
return secret | |
def displaySecret(secret): | |
split = split_len(secret, 16) | |
display = "" | |
for i in split: | |
for j in split_len(i, 1): | |
display += j + " " | |
display += " " | |
print "pt: %s" % display | |
def displayCiphertext(ct): | |
split = split_len(ct, 16) | |
display = "" | |
for i in split: | |
display += i.encode('hex') + " " | |
print "ct: %s" % (display) | |
if __name__ == "__main__": | |
if len(sys.argv) < 2: | |
exit("Usage: %s plaintext" % sys.argv[0]) | |
display=True # Toggle for verbose out put | |
key = "deadbeefcafecode" | |
cipher = AES.new(key, AES.MODE_ECB, "\x00" * 16) | |
cipher.block_size=8 | |
chosen = sys.argv[1] | |
ct = oracle(chosen) | |
if display: | |
displayCiphertext(ct) | |
else: | |
print ct.encode('hex') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment