예제 #1
0
        public static void run(byte[] receivedData_init, byte[] BSKey, UdpClient Bob, IPEndPoint Alice)
        {
            //Console.WriteLine("Bob: listens on port 11010.");

            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);
            //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);
                    //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.");
                    }
                }
            }
        }
예제 #2
0
        //      private PerformanceCounter theCPUCounter =
        // new PerformanceCounter("Processor", "% Processor Time", Process.GetCurrentProcess().ProcessName);
        // public NSServer(byte[] Key1,byte[] Key2){
        //     ASKey=Key1;
        //     BSKey=Key2;
        //     remoteEP = new IPEndPoint(IPAddress.Parse("127.0.0.1"), NSUtilities.server_port);
        //     udpServer = new UdpClient(NSUtilities.server_port);
        // }
        static void Main(string[] args)
        {
            byte[]     ASKey;
            byte[]     BSKey;
            IPEndPoint remoteEP;
            UdpClient  udpServer;

            remoteEP  = new IPEndPoint(IPAddress.Parse("127.0.0.1"), NSUtilities.server_port);
            udpServer = new UdpClient(NSUtilities.server_port);
            // string[] keys = args[0].Split(new string[]{" "}, StringSplitOptions.None);
            // BSKey=NSUtilities.getBytes(args[1]);
            // ASKey=NSUtilities.getBytes(args[0]);
            // ASKey=NSUtilities.ASKey;
            // BSKey=NSUtilities.BSKey;
            ASKey = new byte[] { 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1, 0x8, 0x8, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
                                 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1, 0x8, 0x8, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7 };
            BSKey = new byte[] { 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1, 0x8, 0x8, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
                                 0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1, 0x8, 0x8, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7 };
            string nonceA = null;
            string nonceB = null;
            //Console.WriteLine("Start Server at port 11000");

            int i = 0;

            Console.WriteLine("  RoleS start processor time: {0}",
                              Process.GetCurrentProcess().TotalProcessorTime);
            while (true)
            {
                byte[] data = udpServer.Receive(ref remoteEP); // listen on port 11000

                //Console.WriteLine("Server: receive key request from Alice. ");
                string dataString = NSUtilities.getString(data);

                string[] msgs = dataString.Split(new string[] { " " }, StringSplitOptions.None);

                if (String.Compare(msgs[0], "msg3:") == 0 && int.Parse(msgs[1]) == NSUtilities.Alice_port &&
                    int.Parse(msgs[2]) == NSUtilities.Bob_port)
                {
                    nonceA = msgs[3];
                    string payload_B = NSUtilities.getString(NSUtilities.Decrypt(NSUtilities.getBytes(msgs[4]), BSKey));
                    nonceB = payload_B.Split(new string[] { " " }, StringSplitOptions.None)[1];

                    // Aes aesAlg = Aes.Create();
                    byte[] keyAB = NSUtilities.getKey(32);
                    string kAB_s = NSUtilities.getString(keyAB);
                    byte[] kAB_A = NSUtilities.getBytes(kAB_s + " " + nonceB + " " + NSUtilities.Alice_port);

                    byte[] enc_kAB_A = NSUtilities.Encrypt(kAB_A, BSKey);

                    string enc_kAB_A_s = NSUtilities.getString(enc_kAB_A);
                    byte[] msg2s       = NSUtilities.getBytes(nonceA + " " + NSUtilities.Bob_port + " " + kAB_s + " " + enc_kAB_A_s);

                    byte[] msg2        = NSUtilities.Encrypt(msg2s, ASKey);
                    byte[] msg2combine = NSUtilities.getBytes("msg4: " + NSUtilities.getString(msg2));
                    udpServer.Send(msg2combine, msg2combine.Length, remoteEP);
                    //Console.WriteLine("Server: send Alice Kab. ");
                }
                i++;
                if (i == NSUtilities.loop)
                {
                    Console.WriteLine("  RoleS end processor time: {0}",
                                      Process.GetCurrentProcess().TotalProcessorTime);
                    Process.GetCurrentProcess().Kill();
                }
            }
        }
예제 #3
0
        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);

            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);

            // 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);
                    //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);
                        //Console.WriteLine("Alice: successfully finished key negotiation.");
                    }
                }
            }
        }