def PerformEncryptAction(): #Here we call the crypt function print "[*] Decryption Mode, you may decrypt anyfile you want..." while True: infile = raw_input("[*] Input File: ") output = raw_input("[*] Output File: ") passphrase = raw_input("[*] PassPhrase: ") cbc = CBC(file_in=infile, file_out=output, key=passphrase) cbc.encrypt()
def server_program(): host = socket.gethostname() port = 5000 key_to_encrypt_keys = b'veryawesomekeyyy' key_for_CBC = 'best key for cbc' key_for_ECB = 'best key for ecb' server_socket = socket.socket() # get instance server_socket.bind((host, port)) # bind host address and port together server_socket.listen(2) conn, address = server_socket.accept() # accept new connection print("Connection from: " + str(address)) while True: data = conn.recv(1024).decode() if not data: break if str(data) == 'ECB': temp = ECB(key_for_ECB, key_to_encrypt_keys) to_send = temp.encrypt(key_for_ECB) conn.send(to_send.encode()) # send data to the client mesaj = conn.recv(1024).decode() if str(mesaj) == 'ready to communicate': text_de_trimis = 'ana are ECB' temp2 = ECB(text_de_trimis, key_for_ECB.encode()) crypto = temp2.encrypt(text_de_trimis) conn.send(crypto.encode()) if str(data) == 'CBC': temp = CBC(key_for_CBC, key_to_encrypt_keys) to_send_key = temp.encrypt(key_for_CBC) to_send_iv = temp.iv conn.send(to_send_key.encode()) # send data to the client conn.send(str(to_send_iv).encode()) mesaj = conn.recv(1024).decode() # poate incepe comunicarea if str(mesaj) == 'ready to communicate': text_de_trimis = 'ana are CBC' temp2 = CBC(text_de_trimis, key_for_CBC.encode()) iv = temp2.iv crypto = temp2.encrypt(text_de_trimis) conn.send(crypto.encode()) conn.send(str(iv).encode()) conn.close() # close the connection
def encrypt_oracle(p: bytes): c = b"" amount = random.randrange(5, 10) c += pseudorandom(amount) strategy = random.randrange(0, 2) print(strategy) if strategy == 0: aes = AES.new(pseudorandom(16), AES.MODE_ECB) c += bytes(aes.encrypt(p)) else: iv = pseudorandom(16) aes = CBC(pseudorandom(16), iv) c += aes.encrypt(str(p)) amount = random.randrange(5, 10) c += pseudorandom(amount) return c
def descifrado(): print("\n\nMenu. \n 1.Descifrado Manual \n 2.Descifrado automático") opc = input("Elija una opción: ") if opc == '1': texto = input("Introduzca el texto a descifrar: ") IV = input("Introduzca el vector de inicializacion: ") clave = input("Introudzca la clave: ") cifr = CBC(texto, IV, clave) cifr.cifrar_cbc() elif opc == '2': # Formato hexadecimal: clave = '00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F' IV = '00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00' texto = '69 C4 E0 D8 6A 7B 04 30 D8 CD B7 80 70 B4 C5 5A 4F 63 8C 73 5F 61 43 01 56 78 24 B1 A2 1A 4F 6A' cifr = CBC(texto, IV, clave) cifr.descifrar_cbc() else: print('Opción no reconocida.')
def cifrado(): print("\n\nMenu. \n 1.Cifrado Manual \n 2. Cifrado automático") opc = input("Elija una opción: ") if opc == '1': texto = input("Introduzca el texto a cifrar: ") IV = input("Introduzca el vector de inicializacion: ") clave = input("Introudzca la clave: ") cifr = CBC(texto, IV, clave) cifr.cifrar_cbc() elif opc == '2': # Formato hexadecimal: clave = '00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F' IV = '00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00' texto = '00 11 22 33 44 55 66 77 88 99 AA BB CC DD EE FF 00 00 00 00 00 00 00 00 00 00' cifr = CBC(texto, IV, clave) cifr.cifrar_cbc() else: print('Opción no reconocida.')
def __init__(self, key=None, padding=padWithPadLen(), keySize=16): CBC.__init__(self, AES(key, noPadding(), keySize), padding) self.name = 'AES_CBC'
def __init__(self, key=None, padding=padWithPadLen(), keySize=16): CBC.__init__( self, AES(key, noPadding(), keySize), padding) self.name = 'AES_CBC'
def client_program(): host = socket.gethostname() # as both code is running on same pc port = 5000 # socket server port number key_to_decrypt_keys = b'veryawesomekeyyy' client_socket = socket.socket() # instantiate client_socket.connect((host, port)) # connect to the server message = input("Encryption type: [ECB | CBC]: ... ") # take input while message.lower().strip() != 'end conn': if message.lower().strip() == 'ecb': # se trimite modul de criptare client_socket.send(message.encode()) # se primeste cheia criptata enc_text = client_socket.recv(1024).decode() print('text criptat: ' + enc_text) temp = ECB(enc_text, key_to_decrypt_keys) # se decripteaza cheia cheie = temp.decrypt(enc_text).encode() print('cheie=', cheie) # poate incepe comunicarea client_socket.send('ready to communicate'.encode()) # se primeste textul criptat text_primit = client_socket.recv(1024).decode() print('text primit de la server, criptat: ', text_primit) # se decripteaza textul primit temp2 = ECB(text_primit, cheie) txt_decr = temp2.decrypt(text_primit) print('textul decriptat:', txt_decr) elif message.lower().strip() == 'cbc': # se trimite modul de criptare client_socket.send(message.encode()) # se primeste cheia criptata vectorul de initializare enc_text = client_socket.recv(1024).decode() to_rec_iv = client_socket.recv(1024).decode() to_rec_iv = json.loads(to_rec_iv) # string list to list # se decripteaza cheia temp = CBC(enc_text, key_to_decrypt_keys) temp.set_iv(to_rec_iv) cheie = temp.decrypt(enc_text).encode() print('cheie=', cheie) # poate incepe comunicarea client_socket.send('ready to communicate'.encode()) # se primeste textul criptat text_primit = client_socket.recv(1024).decode() print('text primit de la server, criptat: ', text_primit) # se decripteaza textul primit temp2 = CBC(text_primit, cheie) print('textul decriptat:', temp2.decrypt(text_primit)) to_rec_iv = client_socket.recv(1024).decode() iv = json.loads(to_rec_iv) temp2.set_iv(iv) print('text decriptat: ', temp2.decrypt(text_primit)) message = input(" -> ") # again take input client_socket.close() # close the connection
en_ECB_lib_file = './images/encryptedJapanECB2.png' de_ECB_lib_file = './images/decryptedJapanECB2.png' en_CBC_file = './images/encryptedJapanCBC.png' de_CBC_file = './images/decryptedJapanCBC.png' # LIB CHECK print("Lib RSA ECB") ecb_lib = ECB_LIB(original_file, en_ECB_lib_file, de_ECB_lib_file, n, e, d) ecb_lib.encryptPNG() ecb_lib.decryptPNG() # CUSTOM ECB CHECK print("Custom RSA ECB") ecb = ECB(original_file, en_ECB_file, de_ECB_file, n, e, d) ecb.encryptPNG() ecb.decryptPNG() # CUSTOM CBC CHECK print("Custom RSA CBC") cbc = CBC(original_file, en_CBC_file, de_CBC_file, n, e, d) cbc.encryptPNG() cbc.decryptPNG() # DISPLAYING IMAGES showImage(original_file, en_ECB_lib_file, de_ECB_lib_file, "ECB using RSA from library") showImage(original_file, en_ECB_file, de_ECB_file, "ECB using custom RSA") showImage(original_file, en_CBC_file, de_CBC_file, "CBC using custom RSA")
from cbc import CBC import base64 from keygen import pseudorandom from oracle import encrypt_oracle from xor import xor with open('10.txt') as f: k = b'YELLOW SUBMARINE' iv = b"\x00\x00\x00\x00" * 4 decoded = base64.b64decode(f.read()) aes = CBC(k, iv) p = aes.decrypt(decoded) print(encrypt_oracle(b"aaaaaaaaaaaaaaaa"))