Beispiel #1
0
def build_profile(userdata):
    plaintext = ('comment1=cooking%20MCs;userdata=' + quote(userdata) +
                 ';comment2=%20like%20a%20pound%20of%20bacon')
    plaintext = bytearray(plaintext, encoding='utf-8')
    plaintext = pkcs7padding(plaintext)
    ciphertext = aes_cbc_encrypt(plaintext, random_key, random_iv)
    return ciphertext
Beispiel #2
0
def encrypt_random(plaintext):
    bytes_to_add = random.randint(5, 10)
    plaintext = challenge9.pkcs7padding(
        random_string(bytes_to_add) + plaintext + random_string(bytes_to_add))
    ecb = random.randint(0, 1)
    if ecb:
        aes = AES.new(random_string(16))
        ciphertext = aes.encrypt(plaintext)
    else:
        ciphertext = challenge10.aes_cbc_encrypt(plaintext, random_string(16),
                                                 random_string(16))
    return ciphertext, (ecb == 1)
def encryption_oracle(input):
    random_key = os.urandom(16)
    modified_input = os.urandom(randrange(5,11)) + input + os.urandom(randrange(5,11))
    encrypted = bytearray()
    if randrange(0,2) == 1:
        # do cbc
        iv = os.urandom(16)
        encrypted = aes_cbc_encrypt(modified_input, random_key, iv)
    else:
        #do ecb
        encrypted = encrypt_block(pad(modified_input), random_key)
    return bytearray(encrypted)
Beispiel #4
0
def f1(s):
    # quote out ; and = in s
    s = s.replace(';', '";"')
    s = s.replace('=', '"="')

    res = PREPEND + s + APPEND

    # pad
    res_padded = pkcs7_pad(res, BLK_SZ)

    # encrypt under random AES key
    k = gen_aes_key()
    iv = secrets.token_bytes(BLK_SZ)

    res_enc = aes_cbc_encrypt(res_padded, k, iv)

    return k, res_enc, iv
Beispiel #5
0
def encryption_oracle(pt):
    """
    For the input @pt, pads it with 5-10 bytes on both sides, then encrypts it
    with either AES in ECB mode or AES in CBC mode with equal probability. The
    key and IV are randomly generated 16-byte strings.
    """

    key = rand_bytes(16)
    iv = rand_bytes(16) # In case the mode is CBC. Generate this before
                        # choosing the mode to protect against timing attacks.
    padded_pt = rand_bytes_range(5, 10) + pt + rand_bytes_range(5, 10)
    if random.randint(0, 1) == 0:
        # print True # Uncomment to check the oracle detector
        return aes_ecb_encrypt(key, padded_pt)
    else:
        # print False # Uncomment to check the oracle detector
        return aes_cbc_encrypt(key, padded_pt, iv)
Beispiel #6
0
def encryption_oracle(x):
    if type(x) == str:
        x = bytearray(x, "utf-8")
    bytes_append_len = 5 + secrets.randbelow(6)
    bytes_prepend_len = 5 + secrets.randbelow(6)

    bytes_appended = bytearray(secrets.token_bytes(bytes_append_len))
    bytes_prepended = bytearray(secrets.token_bytes(bytes_prepend_len))

    pt = pkcs7_pad(bytes_prepended + x + bytes_appended, 16)

    encryption_scheme = secrets.randbelow(2)

    if (encryption_scheme == ECB_MODE):
        # do ECB
        ct = encrypt_aes_ecb(pt, gen_aes_key())
    elif (encryption_scheme == CBC_MODE):
        # do CBC
        ct = aes_cbc_encrypt(pt, gen_aes_key(), secrets.token_bytes(BLK_SZ))

    return ct, encryption_scheme
Beispiel #7
0
def encrypt_cbc_random():
    plaintext = binascii.a2b_base64(random.choice(possible_b64_plaintexts))
    iv = random_string(AES.block_size)
    return aes_cbc_encrypt(plaintext, random_key, iv), iv
Beispiel #8
0
 def encrypt(self, pt):
     return aes_cbc_encrypt(self.__key, self.PREFIX + pt + self.SUFFIX,
                            self.__iv)