Last active
October 6, 2018 11:05
-
-
Save ykm11/1557c5f46d5d54b817dfe8e1740969c4 to your computer and use it in GitHub Desktop.
test_ecb.py
This file contains 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
from Crypto.Cipher import AES | |
import string | |
import os | |
key = os.urandom(16) | |
# For brute-force | |
chars = string.ascii_uppercase + string.ascii_lowercase + string.digits | |
SECRET = b'IenagaMugiKawaii' | |
def pad(m): | |
return m + b'\x00'*(16 - len(m) % 16) | |
def encrypt(message): | |
aes = AES.new(key, AES.MODE_ECB) | |
return aes.encrypt(pad(message)) | |
def bytes_split(message): | |
return [message[i:i+16] for i in range(0, len(message), 16)] | |
def show_blocks(message): | |
for block in bytes_split(message): | |
print(block) | |
print() | |
if __name__ == '__main__': | |
S = [] | |
for _ in range(len(SECRET)): | |
for ch in chars: | |
pt = b'a'*(15-len(S)) + b"".join(S) + ch.encode() | |
pt += b'a'*(15-len(S)) | |
pt += SECRET | |
ct = encrypt(pt) | |
print(ch, end="\n") | |
show_blocks(ct) | |
blocks = bytes_split(ct) | |
if len(blocks) != len(set(blocks)): | |
print("OK", ch) | |
S.append(ch.encode()) | |
break | |
print(b"".join(S)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment