Ejemplo n.º 1
0
def encrypt_cbc(data, iv, key):
    enc_data = []
    keylen = len(key)
    xor_with = binascii.hexlify(iv)
    for block in [data[i:i + keylen] for i in range(0, len(data), keylen)]:
        xored = hex_xor(binascii.hexlify(block), xor_with)
        enc_aes = aes_ecb_encrypt(binascii.unhexlify(xored), key)
        enc_data.append(''.join(enc_aes))
        xor_with = binascii.hexlify(enc_aes)
    return ''.join(enc_data)
Ejemplo n.º 2
0
        #repeats=0
        for chunk in [
                data[i + offset:i + offset + chunksize]
                for i in range(0, len(data), chunksize)
        ]:
            if chunk in chunks:
                return True  #if duplicate chunk found then ECB
            else:
                chunks.add(chunk)
        #all_repeats.append(repeats)
    return False  #(max(all_repeats)>0) #CBC


if __name__ == '__main__':
    key = random_key(16)
    iv = random_key(16)
    mode = random.randint(0, 1)
    pre_buffer = random_key(random.randrange(5))
    post_buffer = random_key(random.randrange(5))

    script_dir = os.path.dirname(__file__)
    fo = open(os.path.join(script_dir, "asciidata.txt"), "r")
    raw_data = fo.read()
    fo.close()
    rand_data = pkcs7_key(str(pre_buffer) + raw_data + str(post_buffer), key)

    if mode:  #1
        enc_data = aes_ecb_encrypt(rand_data, key)
    else:  #0
        enc_data = encrypt_cbc(rand_data, iv, key)
    print(key, mode, oracle(enc_data))
Ejemplo n.º 3
0
def make_enc_str(s1,s2,key):
    return(aes_ecb_encrypt(pkcs7_key(s1+s2,key),key))
Ejemplo n.º 4
0
def decrypt_ecb(str,key):
    keylen=len(key)
    hashes=make_hashes(keylen,key)
    indexes=[hashes.index(aes_ecb_encrypt("a"*(keylen-1)+x,key)) for x in str]
    return ''.join([chr(x) for x in indexes])
Ejemplo n.º 5
0
def make_hashes(keylen,key):
    return [aes_ecb_encrypt("a"*(keylen-1)+chr(x),key) for x in range(256)]