def needhamSchroeder(package, packageConnection): #receinving the contents from step 1 #package is IDA||IDB||N1 IDa = package[:8] IDaAsInt = int(IDa) IDaAsBinary = bin(IDaAsInt)[2:].zfill(8) IDb = package[8:16] IDbAsInt = int(IDa) IDbAsBinary = bin(IDaAsInt)[2:].zfill(8) nonce = package[16:] AsKey = userKeys[IDa] BsKey = userKeys[IDb] Ks = nonceGenerator() T = nonceGenerator() #creating the smaller envelope messageToBeEncrypted = Ks + IDaAsBinary + T encryptedMessage = library.encrypt(messageToBeEncrypted, BsKey) #creating the bigger envelop nextMessage = Ks + IDbAsBinary + T + encryptedMessage finalEncryptedMessage = library.encrypt(nextMessage, AsKey) return finalEncryptedMessage
def needhamSchroeder(soc): #receiving the package from step 2 message = soc.recv(1024).decode('utf8') #decrypting the message decrypedMessage = library.decrypt(message, KDC_key) Ks = decrypedMessage[0:10] IDb = decrypedMessage[10:18] T = decrypedMessage[18:28] smallEncryption = decrypedMessage[28:] #now we connect to the harcoded channel client 2 is waiting for us to connect to mySocket = socket.socket() mySocket.connect((HOST, PORT)) #sending over step 3 to Bob mySocket.send(smallEncryption.encode()) #receiving step 4 from Bob newNonce = mySocket.recv(1024).decode() #decrypting step 4 decryptedNonce = library.decrypt(newNonce, Ks) #turning it into and int changedNonce = int(decryptedNonce, 2) #subtracting 1: this si the F function that is predetermined by Alice and Bob changedNonce = changedNonce - 1 #turning it back into a binary string changedNonce = bin(changedNonce)[2:].zfill(10) #encrypting f(nonce) encryptedNonce = library.encrypt(changedNonce, Ks) #sending step 5 to Bob mySocket.send(encryptedNonce.encode()) #if Bob received the anticipated differentiation in nonce value #using the same encryption/decryption key..... #We now have a secure chat! if mySocket.recv(1024).decode() == "VERIFIED": while message != 'q': message = input("Enter the message you want to encrypt -> ") #encrypting the message using DES finalEncryptedMessage = library.encrypt(message, Ks) #encrypting the message #sending the message mySocket.send(finalEncryptedMessage.encode()) #receiving the response from the other user data = mySocket.recv(1024).decode() #decrypting the other user's message decryptedMessage = library.decrypt(data, Ks) if not data: break print("Decrypted Message = " + str(decryptedMessage))
def Main(): host = "127.0.0.1" port = 5001 #necessary to initiate the server mySocket = socket.socket() mySocket.bind((host, port)) print("Waiting for connection.....") #listens for a user to connect mySocket.listen(2) #getting the user's connection info conn, addr = mySocket.accept() print("Connection from: " + str(addr)) while True: #receiving the response from the other user data = conn.recv(1024).decode() print("Received from client = " + data) #decrypting the other user's message decryptedMessage = library.decrypt(data) if not data: break print("Decrypted Message = " + str(decryptedMessage)) print("\n") message = input("Enter the message you want to encrypt -> ") #encrypting the message using DES finalEncryptedMessage = library.encrypt(message) print("Encrypted message = " + finalEncryptedMessage) #prints the pretty loading bar library.sending() #sending the message conn.send(finalEncryptedMessage.encode()) conn.close()
def Main(): host = "127.0.0.1" port = 5001 #necessary to connect to the server mySocket = socket.socket() mySocket.connect((host,port)) message = input("Enter the message you want to encrypt -> ") #encrypting the message using DES finalEncryptedMessage = library.encrypt(message) print("Encrypted message = " + finalEncryptedMessage) #have the communication go on forever while message != 'q': #prints the pretty loading bar library.sending() #encrypting the message finalEncryptedMessage = library.encrypt(message) #sending the message mySocket.send(finalEncryptedMessage.encode()) #receiving the response from the other user data = mySocket.recv(1024).decode() print("Received from server = " + data) #decrypting the other user's message decryptedMessage = library.decrypt(data) if not data: break print ("Decrypted Message = " + str(decryptedMessage)) print("\n") #setting up the message all over again.... message = input("Enter the message you want to encrypt -> ") finalEncryptedMessage = library.encrypt(message) print("Encrypted message = " + finalEncryptedMessage) mySocket.close()
def main(): soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) host = "127.0.0.1" port = 5000 try: soc.connect((host, port)) except: print("Connection error") sys.exit() #create the key and use it in function call Key = random10bit() diffieHelman(soc, Key) #print the user options while True: printMenuOptions() message = input(" -> ") if message == "quit": break #print the user options soc.send(message.encode("utf8")) if soc.recv(5120).decode("utf8") == "-": pass # null operation if message == "list": soc.send(message.encode("utf8")) userList = soc.recv(1024).decode('utf8') print(userList) if 'wait' in message: mySocket = socket.socket() mySocket.bind((HOST, PORT)) print("Waiting for connection.....") #listens for a user to connect mySocket.listen(1) #getting the user's connection info conn, addr = mySocket.accept() print("Connection from: " + str(addr)) #this means that Alice has initiated NS with the KDC and has now #sent us an encrypted envelope with a session key package = conn.recv(1024).decode() #we decrypte it decryptedPackage = library.decrypt(package, KDC_key) Ks = decryptedPackage[:10] IDa = decryptedPackage[10:18] nonce = decryptedPackage[18:] #now we send back an an encrypted nonce newNonce = nonceGenerator() encryptedNonce = library.encrypt(newNonce, Ks) conn.send(encryptedNonce.encode()) # we get an encrypted altered nonce from A incomingChangedNonce = conn.recv(1024).decode() changedIncomingNonce = library.decrypt(incomingChangedNonce, Ks) #if the difference is what we expect (pre-determined), then.... #we now have a secure encrypted communication! if int(changedIncomingNonce, 2) == int(newNonce, 2) - 1: conn.send("VERIFIED".encode()) while True: data = conn.recv(1024).decode() decryptedMessage = library.decrypt(data, Ks) if not data: break print("Decrypted Message = " + str(decryptedMessage)) message = input( "Enter the message you want to encrypt -> ") #encrypting the message using DES finalEncryptedMessage = library.encrypt(message, Ks) #prints the pretty loading bar #sending the message conn.send(finalEncryptedMessage.encode()) soc.send(b'--quit--')