def subByteTest(): aes = AES(master_key) m = master_message c = aes.encrypt(m) for i in range(128): for j in range(i+1, 128): ci = aes.encrypt(m ^ (1 << i)) cj = aes.encrypt(m ^ (1 << j)) ij_mov = 1 << i ^ 1 << j cij = aes.encrypt(m ^ ij_mov) assert(c != ci ^ cj ^ cij)
def test_AES(inp): aes = AES(4, "00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f") key = os.urandom(16).hex() ciphertext = aes.encrypt(inp, key, "bytes") deciphered = aes.decrypt(ciphertext, key, "bytes") assert deciphered == inp
class TestChunkEncryption(unittest.TestCase): def setUp(self): self.aes = AES(b'z' * 16) self.iv = b'\x01' * 16 def test_long_msg(self): message = b'M' * 228 ciphertext = self.aes.encrypt(message, self.iv) self.assertEqual(self.aes.decrypt(ciphertext, self.iv), message) def test_diff_iv(self): iv2 = b'\x02' * 16 message = b'M' * 16 ciphertext1 = self.aes.encrypt(message, self.iv) ciphertext2 = self.aes.encrypt(message, iv2) self.assertNotEqual(ciphertext1, ciphertext2) plaintext1 = self.aes.decrypt(ciphertext1, self.iv) plaintext2 = self.aes.decrypt(ciphertext2, iv2) self.assertEqual(plaintext1, plaintext2) self.assertEqual(plaintext1, message) self.assertEqual(plaintext2, message) def test_bad_iv(self): message = b'M' * 16 with self.assertRaises(AssertionError): self.aes.encrypt(message, b'short') with self.assertRaises(AssertionError): self.aes.encrypt(message, b'long' * 25)
def calculation(message, n, e, d, p, q, startzustand=[1, 1, 1, 1, 0, 0, 0, 0], verbose=False): ### Alice ### ## 1, a = bitarray(startzustand) start_lfsr_alice = LFSR.lfsr(a, [0, 1, 3, 4]) key = [next(start_lfsr_alice) for _ in range(120)] key = "".join(str(x) for x in startzustand + key) print("--ALICE--------") print("LFSR-Key: {}".format(helper.get_split_string_from_list(list(key)))) ## 2, rsa = RSA(p="", q="", n=n, e=e) if verbose: rsa.print_stats() c_1 = rsa.short_public_exponent_encrypt(int("".join( str(i) for i in startzustand), base=2), verbose=verbose) print("RSA Ciphertext: {}".format(c_1)) ## 3, aes = AES(key) c_2 = aes.encrypt(message, verbose=verbose) print("AES Ciphertext: {}".format(c_2)) ### Bob ### ## 1, rsa = RSA(p=p, q=q, e=e, private_key=d) print("--BOB----------") print("Decryption....") bin_str = bin(rsa.chinese_decrypt(c_1, verbose=verbose))[2:] print("RSA Plaintext: {}".format( helper.get_split_string_from_list(list(bin_str)))) ## 2, a = bitarray(bin_str) start_lfsr_bob = LFSR.lfsr(a, [0, 1, 3, 4]) key_bob = [next(start_lfsr_bob) for _ in range(120)] key_bob = "".join(str(x) for x in list(bin_str) + key_bob) print("LFSR-Key: {}".format( helper.get_split_string_from_list(list(bin_str)))) ## 3, aes = AES(key_bob) corresponding_message = aes.decrypt(c_2, verbose=verbose) print("Message: {}".format(corresponding_message)) return message
class Cryptography: def __init__(self, private_key, public_key, P): self.private_key = private_key self.public_key = public_key self.session_key = pow(public_key, private_key, P) self.aes_obj = AES(self.session_key) def encrypt(self, message): blocks = convertMessage(message) y = [] for block in blocks: y.append(str(self.aes_obj.encrypt(block))) return '&'.join(y) def decrypt(self, message): x = [] message = message.split("&") for block in message: x.append(self.aes_obj.decrypt(int(block))) return getMessage(x)
############################################ # Se cifran los documentos for documento in documentos: # Se instancia el cifrador simetrico aeso = AES(k, iv) # Se lee el archivo f = open(f'documentos/{documento}', 'rb') contenido = f.read() f.close() ########## Tiempo de cifrado ############### # Se cifra el contenido de cada archivo tiempos.medir(f'cifrado_{documento}_llave_{x}') cyphertext = aeso.encrypt(contenido) tiempos.medir(f'cifrado_{documento}_llave_{x}') ############################################ # Se retira la extension [opcional] nombre = documento.split('.') ########## Tiempo de firmado ############### # Se firma el contenido tiempos.medir(f'firmado_{documento}_llave_{llaves[x]}') firma = origen.sign(contenido) tiempos.medir(f'firmado_{documento}_llave_{llaves[x]}') ############################################ # Se guarda el archivo cifrado y la firma en otro documento f = open(f'documcifrados/{nombre[0]}', 'wb')
#!/usr/bin/env sage from sage.all import * from AES import AES instance = AES(key="sometestkey", rounds=2) plaintext = "some message to encrypt" print "plaintext:", plaintext print "plaintext length:", len(plaintext) ciphertext = instance.encrypt(plaintext) print "ENCRYPTION RESULT" print plaintext, "->", AES.state_int(ciphertext) print print "DECRYPTION RESULT" decrypted_plaintext = instance.decrypt(ciphertext) print AES.state_int(ciphertext), "->", AES.state_str(decrypted_plaintext)
# coding=UTF-8 from AES import AES import numpy as np key = 0x3220db6534d687f844c41b6de5a4c737 aes = AES(key, 1, 0) inp_row = np.array([172,47,117,192,67,251,195,103,9,211,21,242,36,87,70,216]) cipher_text, trace = aes.encrypt(inp_row) assert ([173,205,44,52,32,86,75,184,193,231,36,82,28,6,44,234] == cipher_text).all() print("AES Test PASS")