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 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
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
def rip_video(self): r = self.httpy.get(self.url) if 'video_title":"' not in r: raise Exception('no video_title":" found at %s' % self.url) title = self.httpy.between(r, 'video_title":"', '"')[0] title = title.replace('+', ' ') if '0p":"' not in r: raise Exception('no 0p":" found at %s' % self.url) quality = self.httpy.between(r, '0p":"', '"')[0] quality = unquote(quality) vid = AES.decrypt(quality, title, 256) result = self.get_video_info(vid) result['poster'] = None # Beeg doesn't provide video splash images return result
def rip_video(self): r = self.httpy.get(self.url) if 'video_title":"' not in r: raise Exception('no video_title":" found at %s' % self.url) title = self.httpy.between(r, 'video_title":"', '"')[0] title = title.replace('+', ' ') if 'video_url":"' not in r: raise Exception('no video_url":" found at %s' % self.url) quality = self.httpy.between(r, 'video_url":"', '"')[0] quality = unquote(quality) vid = AES.decrypt(quality, title, 256) result = self.get_video_info(vid) result['poster'] = None # No Preview result['no_video'] = True # Don't try to display the video result['title'] = title return result
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)
for documento in documentos: # Se instancia el cifrador con la llave recibida aesr = AES(key, iv) # Se lee el archivo nombre = documento.split('.') f = open(f'documcifrados/{nombre[0]}', 'rb') contenido = f.read() f.close() # Se separa la firma del contenido contenido = contenido.split(b'firma') ########## Tiempo de descifrado ############### # Se obtiene el contenido del archivo tiempos.medir(f'descifrado_{documento}_llave_{x}') text = aesr.decrypt(contenido[0]) tiempos.medir(f'descifrado_{documento}_llave_{x}') ############################################### ########## Tiempo de verificación firmas ############### # Se verifica la integridad de los datos tiempos.medir(f'verificacionfirmado_{documento}_llave_{llaves[x]}') coinciden = receptor.verify(contenido[1], text) tiempos.medir(f'verificacionfirmado_{documento}_llave_{llaves[x]}') ######################################################## # Si coinciden se guarda la información en un archivo if coinciden: # Se guarda el resultado f = open(f'documdecifrados/{documento}', 'wb') f.write(text)
#!/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)