public static void run(byte[] receivedData_init, byte[] BSKey, UdpClient Bob, IPEndPoint Alice) { //Console.WriteLine("Bob: listens on port 11010."); Bob.Connect(Alice); Int64 nonceB0 = NSUtilities.getNonce(); Int64 nonceB = NSUtilities.getNonce(); string dataString_init = NSUtilities.getString(receivedData_init); //Console.WriteLine("Bob: receive info from Alice."); string[] splits_init = dataString_init.Split(new string[] { " " }, StringSplitOptions.None); if (String.Compare(splits_init[0], "msg1:") != 0 || String.Compare(splits_init[1], NSUtilities.Alice_port + "") != 0) { //Console.WriteLine("Bob: does not recognize message."); return; } byte[] msg0_payload = NSUtilities.getBytes(NSUtilities.Alice_port + " " + nonceB0); byte[] msg0 = NSUtilities.getBytes("msg2: " + NSUtilities.getString(NSUtilities.Encrypt(msg0_payload, BSKey))); // Bob.Send(msg0,msg0.Length, Alice); NSUtilities.Send(Bob.Client, msg0); //Console.WriteLine("Bob: send first nonceB to Alice."); byte[] receivedData = Bob.Receive(ref Alice); string dataString = NSUtilities.getString(receivedData); //Console.WriteLine("Bob: receive Kab from Alice."); string[] splits = dataString.Split(new string[] { " " }, StringSplitOptions.None); if (String.Compare(splits[0], "msg5:") == 0) { byte[] cipher3 = NSUtilities.getBytes(dataString.Substring(6, dataString.Length - 6)); string msg3 = NSUtilities.getString(NSUtilities.Decrypt(cipher3, BSKey)); string[] msg3s = msg3.Split(new string[] { " " }, StringSplitOptions.None); if (int.Parse(msg3s[2]) == NSUtilities.Alice_port && Int64.Parse(msg3s[1]) == nonceB0) { //Console.WriteLine("Bob: verified the first nonceB."); KeyAB = NSUtilities.getBytes(msg3s[0]); byte[] msg4combine = NSUtilities.getBytes("msg6: " + NSUtilities.getString(NSUtilities.Encrypt(BitConverter.GetBytes(nonceB), KeyAB))); // Bob.Send(msg4combine,msg4combine.Length, Alice); NSUtilities.Send(Bob.Client, msg4combine); //Console.WriteLine("Bob: send second nonceB to Alice."); byte[] receivedData2 = Bob.Receive(ref Alice); string dataString2 = NSUtilities.getString(receivedData2); string[] splits2 = dataString2.Split(new string[] { " " }, StringSplitOptions.None); if (String.Compare(splits2[0], "msg7:") == 0) { byte[] cipher5 = NSUtilities.getBytes(dataString2.Substring(6, dataString2.Length - 6)); Int64 nonceBminus; //parse nonceB-1 nonceBminus = BitConverter.ToInt64(NSUtilities.Decrypt(cipher5, KeyAB), 0); if (nonceBminus + 1 != nonceB) { return; } //Console.WriteLine("Bob: verified nonceB-1."); //Console.WriteLine("Bob: successfully finished key negotiation."); } } } }
public static void run() { //Console.WriteLine("Alice: Sends its identity to Bob"); Alice.Connect(Bob); byte[] msg_init = NSUtilities.getBytes("msg1: " + NSUtilities.Alice_port); // Alice.Send(msg_init,msg_init.Length); NSUtilities.Send(Alice.Client, msg_init); byte[] receivedData_init = Alice.Receive(ref Bob); string dataString_init = NSUtilities.getString(receivedData_init); //Console.WriteLine("Alice: receive first nonce from Bob."); string[] splits_init = dataString_init.Split(new string[] { " " }, StringSplitOptions.None); if (String.Compare(splits_init[0], "msg2:") != 0) { //Console.WriteLine("Alice: does not recognize message."); return; } //Console.WriteLine("Alice: Send key request to server"); Alice.Connect(server); Int64 nonceA = NSUtilities.getNonce(); Int64 nonceB; byte[] msg = NSUtilities.getBytes("msg3: " + NSUtilities.Alice_port + " " + NSUtilities.Bob_port + " " + nonceA + " " + splits_init[1]); // send to server // Alice.Send(msg,msg.Length); NSUtilities.Send(Alice.Client, msg); // then receive data byte[] receivedData = Alice.Receive(ref server); string dataString = NSUtilities.getString(receivedData); //Console.WriteLine("Alice: receive key info from Server."); string[] splits = dataString.Split(new string[] { " " }, StringSplitOptions.None); if (String.Compare(splits[0], "msg4:") == 0) { byte[] cipher2 = NSUtilities.getBytes(dataString.Substring(6, dataString.Length - 6)); string msg2 = NSUtilities.getString(NSUtilities.Decrypt(cipher2, ASKey)); string[] msg2s = msg2.Split(new string[] { " " }, StringSplitOptions.None); if (Int64.Parse(msg2s[0]) == nonceA && int.Parse(msg2s[1]) == NSUtilities.Bob_port) { KeyAB = NSUtilities.getBytes(msg2s[2]); byte[] msg3combine = NSUtilities.getBytes("msg5: " + msg2s[3]); // IPEndPoint Bob = new IPEndPoint(IPAddress.Parse("127.0.0.1"), NSUtilities.Bob_port); Alice.Connect(Bob); // Alice.Send(msg3combine,msg3combine.Length); NSUtilities.Send(Alice.Client, msg3combine); //Console.WriteLine("Alice: send Kab to Bob."); byte[] receivedData2 = Alice.Receive(ref Bob); string dataString2 = NSUtilities.getString(receivedData2); string[] splits2 = dataString2.Split(new string[] { " " }, StringSplitOptions.None); if (String.Compare(splits2[0], "msg6:") == 0) { byte[] cipher4 = NSUtilities.getBytes(dataString2.Substring(6, dataString2.Length - 6)); // parse nounceB nonceB = BitConverter.ToInt64(NSUtilities.Decrypt(cipher4, KeyAB), 0); //Console.WriteLine("Alice: decrypted nonceB with Kab."); nonceB--; byte[] msg5combine = NSUtilities.getBytes("msg7: " + NSUtilities.getString(NSUtilities.Encrypt(BitConverter.GetBytes(nonceB), KeyAB))); // Alice.Send(msg5combine,msg5combine.Length); NSUtilities.Send(Alice.Client, msg5combine); //Console.WriteLine("Alice: successfully finished key negotiation."); } } } }