def encryption_method(client_A, client_B, K1, K2, K3, IV): A_encryption_type = get_encyption_type(client_A) B_encryption_type = get_encyption_type(client_B) encryption_type = get_final_encryption(A_encryption_type, B_encryption_type) print("Selected encryption is: " + encryption_type.upper()[0:3]) # Encrypting messages from KM <-> A and KM <-> B in ECB if encryption_type.upper()[0:3] == 'ECB': print("K1: " + str(K1)) to_be_encrypted = 'ECB#' + str(K1) ecb_instance = ECB(str.encode(K3)) ciphermode = ecb_instance.ECB_encrypt(K3, to_be_encrypted, 10) client_A.sendall(str.encode(str(ciphermode[0]))) # send encryption client_A.sendall(str.encode(str( ciphermode[1]))) # send no_encrypted_blocks client_B.sendall(str.encode(str(ciphermode[0]))) # send encryption client_B.sendall(str.encode(str( ciphermode[1]))) # send no_encrypted_blocks print("KM -> A, B ECB#K1") return encryption_type, K1, None else: to_be_encrypted = 'OFB#' + K2 + '#' + IV ecb_instance = ECB(str.encode(K3)) ciphermode = ecb_instance.ECB_encrypt(K3, to_be_encrypted, 10) client_A.sendall(str.encode(str(ciphermode[0]))) # send encryption client_A.sendall(str.encode(str( ciphermode[1]))) # send no_encrypted_blocks client_B.sendall(str.encode(str(ciphermode[0]))) # send encryption client_B.sendall(str.encode(str( ciphermode[1]))) # send no_encrypted_blocks print("KM -> A, B OFB#K2#IV") return encryption_type, K2, IV
def get_confirmations(client_A, client_B, mode, key, iv): client_A_confirm_encrypt = client_A.recv(1024) print("client_A_confirm: " + str(client_A_confirm_encrypt)) client_B_confirm_encrypt = client_B.recv(1024) print("client_B_confirm: " + str(client_B_confirm_encrypt)) print("Done getting data from A, B") if iv is None: print("K1: " + str(key)) ecb_instance = ECB(str.encode(key)) decryption = ecb_instance.ECB_decrypt( key, client_A_confirm_encrypt.decode(), 1) A_confirm = str(decryption[0])[0:3] decryption = ecb_instance.ECB_decrypt( key, client_B_confirm_encrypt.decode(), 1) B_confirm = str(decryption[0])[0:3] print('Response_A_confirmation : ' + str(A_confirm) + "\nResponse_B_confirmation : " + str(B_confirm)) else: print("K2: " + str(key)) print("IV: " + str(iv)) ofb_instance = OFB() decryption = ofb_instance.decrypt_OFB( client_A_confirm_encrypt.decode(), key, iv.encode("utf8"), 1) A_confirm = str(decryption[0])[0:3] decryption = ofb_instance.decrypt_OFB( client_B_confirm_encrypt.decode(), key, iv.encode("utf8"), 1) B_confirm = str(decryption[0])[0:3] print('Response_A_confirmation : ' + str(A_confirm) + "\nResponse_B_confirmation : " + str(B_confirm)) if A_confirm == 'Yes' and B_confirm == 'Yes': if iv is None: print('AB can start now') ecb_instance = ECB(str.encode(key)) cipherstart = ecb_instance.ECB_encrypt(key, 'AB can start now', 1) client_A.sendall(str.encode(str( cipherstart[0]))) # send encryption to A client_B.sendall(str.encode(str( cipherstart[0]))) # send encryption to B return True else: print('AB can start now') ofb_instance = OFB() cipherstart = ofb_instance.encrypt_OFB('AB can start now', key, iv.encode("utf8"), 1) client_A.sendall(str.encode(str( cipherstart[0]))) # send encryption to A client_B.sendall(str.encode(str( cipherstart[0]))) # send encryption to B return True else: if iv is None: print('Please try again') ecb_instance = ECB(str.encode(key)) cipherstart = ecb_instance.ECB_encrypt(key, 'Please try again', 1) client_A.sendall(str.encode(str( cipherstart[0]))) # send encryption to A client_B.sendall(str.encode(str( cipherstart[0]))) # send encryption to B return False else: ofb_instance = OFB() cipherstart = ofb_instance.encrypt_OFB('Please try again', key, iv.encode("utf8"), 1) client_A.sendall(str.encode(str( cipherstart[0]))) # send encryption to A client_B.sendall(str.encode(str( cipherstart[0]))) # send encryption to B return False
def main(): ClientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: ClientSocket.connect((host, port)) except socket.error as e: print(str(e)) mode_enc = input('Send to KM ENC mode: ') print('mode_enc_client_A : ' + str(mode_enc)) ClientSocket.send(str.encode(mode_enc)) Response_KM_encryption = ClientSocket.recv(1024) Response_KM_no_blocks = ClientSocket.recv(1024) # First time we decrypt using K3 for getting K1 si K2 (+IV) ecb_instance = ECB(str.encode(K3)) decryption = ecb_instance.ECB_decrypt( K3, Response_KM_encryption.decode(), int(_bytes_to_string(Response_KM_no_blocks))) plain = re.sub('[\0]', '', decryption[0]) print('Response_KM : ' + str(plain)) mode_enc = (plain.split('#'))[0] print('MODE_FROM_KM: ' + str(mode_enc).upper()) # ok pana aici if mode_enc.upper() == 'ECB': # Send CONFIRMATION TO KM K1 = (plain.split('#'))[-1] print("K1 : " + str(K1)) ecb_instance = ECB(str.encode(K1)) cipherconfirmation = ecb_instance.ECB_encrypt(K1, 'Yes', 1) ClientSocket.send(str.encode(str( cipherconfirmation[0]))) # send encryption print("Confirmation sent to the KM") elif mode_enc.upper() == 'OFB': K2 = (plain.split('#'))[-2] print("K2 : " + str(K2)) IV = (plain.split('#'))[-1] print("IV : " + str(IV)) ofb_instance = OFB() cipherconfirmation = ofb_instance.encrypt_OFB('Yes', K2, IV.encode("utf8"), 1) ClientSocket.sendall(str.encode(str( cipherconfirmation[0]))) # send encryption print("Confirmation sent to the KM") else: ecb_instance = ECB(str.encode(K3)) cipherconfirmation = ecb_instance.ECB_encrypt(K3, 'No', 1) ClientSocket.send(str.encode(str(cipherconfirmation[0]))) print("Invalid data received!") # waiting for a start print("Waiting for a start coming from KM") Response_KM_start_enc = ClientSocket.recv(1024) if mode_enc.lower() == 'ecb': ecb_instance = ECB(str.encode(K1)) decryption = ecb_instance.ECB_decrypt(K1, Response_KM_start_enc.decode(), 1) plain = re.sub('[\0]', '', decryption[0]) else: ofb_instance = OFB() decryption = ofb_instance.decrypt_OFB(Response_KM_start_enc.decode(), K2, IV.encode("utf8"), 1) plain = re.sub('[\0]', '', decryption[0]) print('Response_KM_start: ' + str(plain)) if 'AB can start now' in str(plain): A = prepare_socket_server() B_socket, B_address = make_connection(A) print('Node B has connected') plaintext = '' with open('plaintext.txt', 'r') as file_obj: line = file_obj.readline() while line: plaintext += line line = file_obj.readline() plaintext = re.sub(r'[\n]+', '', plaintext) bool_var = False if 'ecb' in mode_enc.lower(): print("Start sending blocks to B in ECB") ecb_instance = ECB(str.encode(K1)) while len(plaintext) >= 0 or bool_var is True: encryption = ecb_instance.ECB_encrypt(K1, plaintext, 10) starting_point = int(encryption[3]) if len(str(encryption[0])) <= 0: bool_var = True break plaintext = plaintext[starting_point:] print("encryption: " + str(encryption[0])) B_socket.sendall(str.encode(encryption[0])) B_socket.sendall(str.encode(str(encryption[1]))) print("no_enc_blocks: " + str(encryption[1])) # SEND TO KM encryption_for_km = ecb_instance.ECB_encrypt( K1, 'Blocks are sent!', 1) ClientSocket.send(str.encode(str(encryption_for_km[0]))) print("Waiting for a sign from KM") Response_KM_sign_enc = ClientSocket.recv(1024) decryption = ecb_instance.ECB_decrypt( K1, Response_KM_sign_enc.decode(), 1) plain = str(decryption[0]) print('Response_KM_SIGN: ' + str(plain)) if 'Something wrong' in plain: break if len(plaintext) <= 0 or bool_var is True: print("FINISHED") encryption = ecb_instance.ECB_encrypt(K1, 'It has finished!', 1) print("encryption: " + str(encryption[0])) B_socket.sendall(str.encode(encryption[0])) B_socket.sendall(str.encode(str(encryption[1]))) print("no_enc_blocks: " + str(encryption[1])) # SEND TO KM - FINISHED encryption_for_km = ecb_instance.ECB_encrypt( K1, 'It has finished!', 1) ClientSocket.send(str.encode(str(encryption_for_km[0]))) bool_var = False else: # ofb print("Start sending blocks to B in OFB") ofb_instance = OFB() while len(plaintext) >= 0 or bool_var is True: encryption = ofb_instance.encrypt_OFB( plaintext, K2, IV.encode("utf8"), 10) # 0 la inceput si dupa adunam la plaintext left starting_point = int(encryption[3]) if len(str(encryption[0])) <= 0: bool_var = True break plaintext = plaintext[starting_point:] print("encryption: " + str(encryption[0])) B_socket.sendall(str.encode(encryption[0])) B_socket.sendall(str.encode(str(encryption[1]))) print("no_enc_blocks: " + str(encryption[1])) # SEND TO KM encryption_for_km = ofb_instance.encrypt_OFB( 'Blocks are sent!', K2, IV.encode("utf8"), 1) ClientSocket.send(str.encode(str(encryption_for_km[0]))) print("waiting for a sign from KM") Response_KM_sign_enc = ClientSocket.recv(1024) decryption = ofb_instance.decrypt_OFB( Response_KM_sign_enc.decode(), K2, IV.encode("utf8"), 1) plain = str(decryption[0]) print('Response_KM_SIGN: ' + str(plain)) if 'Something wrong' in plain: break if len(plaintext) <= 0 or bool_var is True: # we have finished print("FINISHED") encryption = ofb_instance.encrypt_OFB('It has finished!', K2, IV.encode("utf8"), 1) print("encryption: " + str(encryption[0])) B_socket.sendall(str.encode(encryption[0])) B_socket.sendall(str.encode(str(encryption[1]))) print("no_enc_blocks: " + str(encryption[1])) # SEND TO KM - FINISHED encryption_for_km = ofb_instance.encrypt_OFB( 'It has finished!', K2, IV.encode("utf8"), 1) ClientSocket.send(str.encode(str(encryption_for_km[0]))) bool_var = False else: print("Try again") ClientSocket.close()
def get_status(client_A, client_B, mode, key, iv): client_A_status_enc = client_A.recv(1024) print("client_A_status_enc : " + str(client_A_status_enc)) client_B_status_enc = client_B.recv(1024) print("client_B_status_enc : " + str(client_B_status_enc)) print("Done getting data from A, B") if iv is None: print("K1: " + str(key)) ecb_instance = ECB(str.encode(key)) decryption = ecb_instance.ECB_decrypt(key, client_A_status_enc.decode(), 1) A_status = str(decryption[0]) decryption = ecb_instance.ECB_decrypt(key, client_B_status_enc.decode(), 1) B_status = str(decryption[0]) print('Response_A_status: ' + str(A_status) + "\nResponse_B_status: " + str(B_status)) else: print("K2: " + str(key)) print("IV: " + str(iv)) ofb_instance = OFB() decryption = ofb_instance.decrypt_OFB(client_A_status_enc.decode(), key, iv.encode("utf8"), 1) A_status = str(decryption[0]) decryption = ofb_instance.decrypt_OFB(client_B_status_enc.decode(), key, iv.encode("utf8"), 1) B_status = str(decryption[0]) print('Response_A_status: ' + str(A_status) + "\nResponse_B_status: " + str(B_status)) if 'Blocks are sent!' in A_status and 'Blocks received!' in B_status: if iv is None: ecb_instance = ECB(str.encode(key)) cipherstart = ecb_instance.ECB_encrypt(key, 'Enc! Dec!', 1) client_A.sendall(str.encode(str( cipherstart[0]))) # send encryption to A client_B.sendall(str.encode(str( cipherstart[0]))) # send encryption to B return True else: ofb_instance = OFB() cipherstart = ofb_instance.encrypt_OFB('Enc! Dec!', key, iv.encode("utf8"), 1) client_A.sendall(str.encode(str( cipherstart[0]))) # send encryption to A client_B.sendall(str.encode(str( cipherstart[0]))) # send encryption to B return True elif 'It has finished!' in A_status and 'It has finished!' in B_status: print("Encryptions and decryptions have finished!") return False else: if iv is None: ecb_instance = ECB(str.encode(key)) cipherstart = ecb_instance.ECB_encrypt(key, 'Something wrong', 1) client_A.sendall(str.encode(str( cipherstart[0]))) # send encryption to A client_B.sendall(str.encode(str( cipherstart[0]))) # send encryption to B return False else: ofb_instance = OFB() cipherstart = ofb_instance.encrypt_OFB('Something wrong', key, iv.encode("utf8"), 1) client_A.sendall(str.encode(str( cipherstart[0]))) # send encryption to A client_B.sendall(str.encode(str( cipherstart[0]))) # send encryption to B return False
def main(): ClientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: ClientSocket.connect((host, port)) except socket.error as e: print(str(e)) mode_enc = input('Send to KM ENC mode: ') print('mode_enc_client_B : ' + str(mode_enc)) ClientSocket.send(str.encode(mode_enc)) Response_KM_encryption = ClientSocket.recv(1024) Response_KM_no_blocks = ClientSocket.recv(1024) ecb_instance = ECB(str.encode(K3)) decryption = ecb_instance.ECB_decrypt( K3, Response_KM_encryption.decode(), int(_bytes_to_string(Response_KM_no_blocks))) plain = re.sub('[\0]', '', decryption[0]) print('Response_KM : ' + str(plain)) mode_enc = (plain.split('#'))[0] print('MODE_FROM_KM: ' + str(mode_enc)) # ok pana aici if mode_enc == 'ECB': # Send CONFIRMATION TO KM K1 = (plain.split('#'))[-1] print("K1 : " + str(K1)) ecb_instance = ECB(str.encode(K1)) cipherconfirmation = ecb_instance.ECB_encrypt(K1, 'Yes', 1) print("cipherconfirmation B: " + str(cipherconfirmation)) ClientSocket.send(str.encode(str( cipherconfirmation[0]))) # trimit encryptarea print("B encrypt confirmation : " + str(cipherconfirmation[0])) print("Am trimis confirmarea") elif mode_enc == 'OFB': K2 = (plain.split('#'))[-2] print("K2 : " + str(K2)) IV = (plain.split('#'))[-1] print("IV : " + str(IV)) ofb_instance = OFB() cipherstart = ofb_instance.encrypt_OFB('Yes', K2, IV.encode("utf8"), 1) ClientSocket.sendall(str.encode(str( cipherstart[0]))) # trimit encryptarea print("am trimis confirmare") # else:# cum criptez si cu ce cheie va descripta KM # confirmation = "No" # encryptor = AES.new(K3, AES.MODE_ECB) # cipherconfirmation = encryptor.encrypt(pad(str.encode(confirmation), BLOCK_SIZE)) # ClientSocket.send(cipherconfirmation) # print("am trimis neconfirmare") # waiting for a start print("Waiting for a start") Response_KM_start_enc = ClientSocket.recv(1024) # Response_KM_start_no_enc_blocks = ClientSocket.recv(2) if mode_enc.lower() == 'ecb': ecb_instance = ECB(str.encode(K1)) # decryption = ecb_instance.ECB_decrypt(K1, Response_KM_start_enc.decode(), int(_bytes_to_string(Response_KM_start_no_enc_blocks))) decryption = ecb_instance.ECB_decrypt(K1, Response_KM_start_enc.decode(), 1) plain = re.sub('[\0]', '', str(decryption[0])) else: ofb_instance = OFB() decryption = ofb_instance.decrypt_OFB(Response_KM_start_enc.decode(), K2, IV.encode("utf8"), 1) plain = re.sub('[\0]', '', decryption[0]) print('Response_KM_start: ' + str(plain)) if 'AB can start now' in str(plain): ClientSocket_To_A = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: ClientSocket_To_A.connect((host, port_A_server)) except socket.error as e: print(str(e)) stop = False if mode_enc.lower() == 'ecb': print("Start receiving blocks from A in ECB") while stop is False: encryption = ClientSocket_To_A.recv(1024) print("encryption from A: " + str(encryption)) no_enc_blocks = ClientSocket_To_A.recv(2) print("no_enc_blocks from A: " + _bytes_to_string(no_enc_blocks)) ecb_instance = ECB(str.encode(K1)) decryption = ecb_instance.ECB_decrypt( K1, encryption.decode(), int(_bytes_to_string(no_enc_blocks))) print("DECRYPTION: " + decryption[0]) print("No decrypted blocks: " + str(decryption[1])) if 'It has finished!' != str(decryption[0]): print("Not finished yet!") # SEND TO KM encryption_for_km = ecb_instance.ECB_encrypt( K1, 'Blocks received!', 1) ClientSocket.send(str.encode(str(encryption_for_km[0]))) print("waiting for a sign from KM") Response_KM_sign_enc = ClientSocket.recv(1024) decryption = ecb_instance.ECB_decrypt( K1, Response_KM_sign_enc.decode(), 1) plain = str(decryption[0]) print('Response_KM_SIGN: ' + str(plain)) if 'Something wrong' in plain: break else: print("FINISHED!") encryption_for_km = ecb_instance.ECB_encrypt( K1, 'It has finished!', 1) ClientSocket.send(str.encode(str(encryption_for_km[0]))) stop = True else: print("Start receiving blocks from A in OFB") while stop is False: ofb_instance = OFB() encryption = ClientSocket_To_A.recv(1024) print("encryption from A: " + str(encryption)) no_enc_blocks = ClientSocket_To_A.recv(2) print("no_enc_blocks: " + _bytes_to_string(no_enc_blocks)) decryption = ofb_instance.decrypt_OFB( encryption.decode(), K2, IV.encode("utf8"), int(_bytes_to_string(no_enc_blocks))) print("DECRYPTION: " + decryption[0]) print("No decrypted blocks: " + str(decryption[1])) if 'It has finished!' != str(decryption[0]): print("Not finished yet!") # SEND TO KM encryption_for_km = ofb_instance.encrypt_OFB( 'Blocks received!', K2, IV.encode("utf8"), 1) ClientSocket.send(str.encode(str(encryption_for_km[0]))) print("Waiting for a sign from KM") Response_KM_sign_enc = ClientSocket.recv(1024) decryption = ofb_instance.decrypt_OFB( Response_KM_sign_enc.decode(), K2, IV.encode("utf8"), 1) plain = str(decryption[0]) print('Response_KM_SIGN: ' + str(plain)) if 'Something wrong' in plain: break else: print("Am terminat!") encryption_for_km = ofb_instance.encrypt_OFB( 'It has finished!', K2, IV.encode("utf8"), 1) ClientSocket.send(str.encode(str(encryption_for_km[0]))) stop = True else: print("Try again") ClientSocket.close()