def CPAExperiment(total): start = time.time() generator = InitialGenerator(True) key = getDefaultKey() baseMessage = get_random_bytes(AES.block_size) baseCipher, baseIV = _encryptionOracle_unsecure([baseMessage], key, generator)[0] win = 0 iv = baseIV for i in range(total): nextIV = compute_next_IV(iv) prepared = prepare_message(baseMessage, baseIV, nextIV) random = get_random_bytes(AES.block_size) while random is prepared: random = get_random_bytes(AES.block_size) output, bit = _challenge_unsecure([prepared, random], key, generator) cipher, iv = output[0], output[1] guessed = guess_bit(baseCipher,cipher) if guessed == bit: win += 1 end = time.time() print(f"Guessed [{win}] of [{total}] attempts [{win*100/total}%].") print(f"Guessed in [{end - start} s]") print(f"Average time [{(end - start)/total} s]")
def get_key(args): if args.path is None or args.password is None or args.alias is None: print("Using default key!") key = getDefaultKey() else: key = getKey(args.path, args.alias, args.password) return key
def test_EAX(self): aes = AES_util() mode = "EAX" key = getDefaultKey() message = get_random_bytes(AES.block_size) cipher, iv = aes.encrypt(message, mode, key) decrypted = aes.decrypt(cipher, mode, key, iv) self.assertEqual(message, decrypted) self.assertNotEqual(message, cipher)
def __init__(self, init=None): if init is None: self.init = InitialGenerator() else: self.init = init self.key = getDefaultKey()