def test_ctr_mode(self): """ Test CTR mode. """ cipher = self.cipher block_multiple_data = self.block_multiple_data nonce = int.from_bytes(urandom(8), "big") for i in range(0, 8): with self.subTest(extra_bytes=i): data = block_multiple_data + urandom(i) encrypted_data = b"".join(cipher.encrypt_ctr(data, blowfish.ctr_counter(nonce, operator.xor))) decrypted_data = b"".join(cipher.decrypt_ctr(encrypted_data, blowfish.ctr_counter(nonce, operator.xor))) self.assertEqual(data, decrypted_data)
def test_ctr_mode(self): """ Test CTR mode. """ cipher = self.cipher block_multiple_data = self.block_multiple_data nonce = int.from_bytes(urandom(8), "big") for i in range(0, 8): with self.subTest(extra_bytes=i): data = block_multiple_data + urandom(i) encrypted_data = b"".join( cipher.encrypt_ctr( data, blowfish.ctr_counter(nonce, operator.xor))) decrypted_data = b"".join( cipher.decrypt_ctr( encrypted_data, blowfish.ctr_counter(nonce, operator.xor))) self.assertEqual(data, decrypted_data)
print("\nTesting block encrypt:") text = b'testtest' print("\tText:\t\t%s" % text) crypted = cipher.encrypt_block(text) print("\tEncrypted:\t%s" % crypted) decrypted = cipher.decrypt_block(crypted) print("\tDecrypted:\t%s" % decrypted) print("\nTesting CTR encrypt:") text = b"The quick brown fox jumps over the lazy dog" print("\tText:\t\t", text) # increment by one counters nonce = int.from_bytes(os.urandom(8), "big") enc_counter = blowfish.ctr_counter(nonce, f=xor) dec_counter = blowfish.ctr_counter(nonce, f=xor) crypted = b''.join(cipher.encrypt_ctr(text, enc_counter)) print("\tEncrypted:\t%s" % crypted) decrypted = b''.join(cipher.decrypt_ctr(crypted, dec_counter)) print("\tDecrypted:\t", decrypted) print("\nTesting CBC encrypt:") text = b"The quick brown fox jumps over the lazy dogXXXXX" print("\tText:\t\t", text) iv = os.urandom(8) # initialization vector print('\tInit Vector = %s' % str(iv)) crypted = b''.join(cipher.encrypt_cbc(text, iv))
print("\nBenchmarking 'decrypt_ofb'...") total = 0 for n in range(1, times): timer = Timer(perf_counter) with timer: b"".join(test_cipher.decrypt_ofb(rand_bytes, iv)) print("{} random bytes in {:.5f} sec".format(num_bytes, timer.elapsed)) total += timer.elapsed print("{} random bytes in {:.5f} sec (average)".format( num_bytes, total / n)) print("\nBenchmarking 'encrypt_ctr'...") total = 0 for n in range(1, times): timer = Timer(perf_counter) counter = blowfish.ctr_counter(nonce, operator.xor) with timer: b"".join(test_cipher.encrypt_ctr(rand_bytes, counter)) print("{} random bytes in {:.5f} sec".format(num_bytes, timer.elapsed)) total += timer.elapsed print("{} random bytes in {:.5f} sec (average)".format( num_bytes, total / n)) print("\nBenchmarking 'decrypt_ctr'...") total = 0 for n in range(1, times): timer = Timer(perf_counter) counter = blowfish.ctr_counter(nonce, operator.xor) with timer: b"".join(test_cipher.decrypt_ctr(rand_bytes, counter)) print("{} random bytes in {:.5f} sec".format(num_bytes, timer.elapsed))
print("\nBenchmarking 'decrypt_ofb'...") total = 0 for n in range(1, times): timer = Timer(perf_counter) with timer: b"".join(test_cipher.decrypt_ofb(rand_bytes, iv)) print("{} random bytes in {:.5f} sec".format(num_bytes, timer.elapsed)) total += timer.elapsed print("{} random bytes in {:.5f} sec (average)".format(num_bytes, total / n)) print("\nBenchmarking 'encrypt_ctr'...") total = 0 for n in range(1, times): timer = Timer(perf_counter) counter = blowfish.ctr_counter(nonce, operator.xor) with timer: b"".join(test_cipher.encrypt_ctr(rand_bytes, counter)) print("{} random bytes in {:.5f} sec".format(num_bytes, timer.elapsed)) total += timer.elapsed print("{} random bytes in {:.5f} sec (average)".format(num_bytes, total / n)) print("\nBenchmarking 'decrypt_ctr'...") total = 0 for n in range(1, times): timer = Timer(perf_counter) counter = blowfish.ctr_counter(nonce, operator.xor) with timer: b"".join(test_cipher.decrypt_ctr(rand_bytes, counter)) print("{} random bytes in {:.5f} sec".format(num_bytes, timer.elapsed)) total += timer.elapsed