def aes_128_ecb(self, attacker_encrypted_data): # with open("w5p1.txt") as f: # takes about 3.5 seconds with open("Cookie64.txt") as f: # takes about 2.3 seconds unknown_string = str(base64.b64decode(f.read()), 'latin1') return encrypt_ecb( bytes(attacker_encrypted_data + unknown_string, 'latin1'), self.random_unknown_key, True)
def encrypt_cbc(p, k, iv): p = pkcs7_pad(p.decode('latin1'), 128) # make list of blocks blocks = [] for i in range(0, int(len(p) / 16)): b = p[i * 16:i * 16 + 16] blocks.append(b) # xor and encrypt to get ciphertext h1 = binascii.hexlify(iv) c_text = [] for b in blocks: h2 = binascii.hexlify(b) x = binary_xor(h1.decode('latin1'), h2.decode('latin1')) x = hex(int(x, 2))[2:].zfill(32) do_aes = encrypt_ecb(binascii.unhexlify(x), k, False) h1 = binascii.hexlify(do_aes) # update h1 for next iteration c_text.append(str(do_aes, 'latin1')) return ''.join(c_text)
b_array = bytearray(my_encode(plaintext)) # randomly prepend 5-10 bytes random_prepend = os.urandom(1) num_prepend_bytes = 5 + random_prepend[0] % 6 # noOfPrependBytes for i in range(0, num_prepend_bytes): b_array.append(os.urandom(1)[0]) # randomly append 5-10 bytes random_append = os.urandom(1) num_append_bytes = 5 + random_append[0] % 6 # noOfAppendBytes for j in range(0, num_append_bytes): b_array.insert(0, os.urandom(1)[0]) # create modified plaintext plaintext = bytes(b_array) # encrypt if mode == 0: ciphertext = encrypt_ecb(plaintext, random_key, True) else: random_iv = os.urandom(16) ciphertext = bytes(my_encode(encrypt_cbc(plaintext, random_key, random_iv))) # ciphertext = bytes(test_encrypt(plaintext, random_key, random_iv)) # print detected mode if detect_ecb(binascii.hexlify(ciphertext)): print("ECB mode detected") else: print("CBC mode detected")