Esempio n. 1
0
def oracle(ptext):
    key, ecb_mode = make_aes_key(), random.getrandbits(1)

    pre = bytes(random.getrandbits(8) for _ in range(random.randint(5, 10)))
    post = bytes(random.getrandbits(8) for _ in range(random.randint(5, 10)))

    ptext = pre + ptext + post

    if ecb_mode:
        ctext = aes_ecb_encrypt(ptext, key)
    else:
        iv = make_aes_key()
        ctext = aes_cbc_encrypt(ptext, key, iv)

    return ctext, ecb_mode
def main():
    ecb_key, key = b"YELLOW SUBMARINE", make_aes_key()
    ptext = aes_ecb_decrypt(loader("25.txt", "base64", split=False), ecb_key)
    ctext = aes_ctr(ptext, key)

    print(f"Cracking ciphertext using {CPUS} cores, please wait.")

    # This signal handler business allows Ctrl-C to work more gracefully
    # with multiprocessing. (Tested on Linux only.)
    orig_handler = signal.signal(signal.SIGINT, signal.SIG_IGN)
    with Pool(CPUS) as pool:
        signal.signal(signal.SIGINT, orig_handler)

        mp_data = ((ctext, key, offset) for offset in range(len(ctext)))
        try:
            recovered_ptext = bytes(pool.imap(mp_find_ptext_byte, mp_data))
        except KeyboardInterrupt:
            raise
        else:
            print()
            print(recovered_ptext.decode())
        finally:
            print()
Esempio n. 3
0
#   Before you implement this attack, answer this question: why does CBC mode
#   have this property?
#
import inspect
import os
import sys

sys.path.append(
    os.path.dirname(
        os.path.dirname(os.path.abspath(inspect.getfile(lambda: 0)))))

from util.aes_wrappers import aes_cbc_decrypt, aes_cbc_encrypt, make_aes_key
from util.text import repeating_key_xor

# Key and IV are both 128 random bits.
KEY = make_aes_key()
IV = make_aes_key()
PREFIX = b"comment1=cooking%20MCs;userdata="
POSTFIX = b";comment2=%20like%20a%20pound%20of%20bacon"


def make_userdata(bs):
    userdata = bs.replace(b";", b"';'").replace(b"=", b"'='")
    ptext = PREFIX + userdata + POSTFIX
    return aes_cbc_encrypt(ptext, KEY, IV)


def is_admin(ctext):
    if b";admin=true;" in aes_cbc_decrypt(ctext, KEY, IV):
        return True
    return False
def submit_random_ptext():
    global KEY
    KEY, iv = make_aes_key(), make_aes_key()
    ptext = base64.b64decode(random.choice(B64STRS))
    ctext = aes_cbc_encrypt(ptext, KEY, iv)
    return ctext, iv