public static void Run(NetPeer peer)
		{
			//
			// Test XTEA
			//
			NetXtea xtea = new NetXtea("TopSecret");

			byte[] original = new byte[16];
			NetRandom.Instance.NextBytes(original);

			byte[] encrypted = new byte[original.Length];
			xtea.EncryptBlock(original, 0, encrypted, 0);
			xtea.EncryptBlock(original, 8, encrypted, 8);

			byte[] decrypted = new byte[original.Length];
			xtea.DecryptBlock(encrypted, 0, decrypted, 0);
			xtea.DecryptBlock(encrypted, 8, decrypted, 8);

			// compare!
			for (int i = 0; i < original.Length; i++)
				if (original[i] != decrypted[i])
					throw new NetException("XTEA fail!");

			Console.WriteLine("XTEA OK");

			NetOutgoingMessage om = peer.CreateMessage();
			om.Write("Hallon");
			om.Write(42);
			om.Write(5, 5);
			om.Write(true);
			om.Write("kokos");
			om.Encrypt(xtea);

			// convert to incoming message
			NetIncomingMessage im = Program.CreateIncomingMessage(om.PeekDataBuffer(), om.LengthBits);
			im.Decrypt(xtea);

			if (im.ReadString() != "Hallon")
				throw new NetException("fail");
			if (im.ReadInt32() != 42)
				throw new NetException("fail");
			if (im.ReadInt32(5) != 5)
				throw new NetException("fail");
			if (im.ReadBoolean() != true)
				throw new NetException("fail");
			if (im.ReadString() != "kokos")
				throw new NetException("fail");

			Console.WriteLine("Message encryption OK");
		}
        /// <summary>
        /// Encrypt this message using the XTEA algorithm; no more writing can be done before sending it
        /// </summary>
        public void Encrypt(NetXtea tea)
        {
            // need blocks of 8 bytes
            WritePadBits();
            int blocksNeeded = (m_bitLength + 63) / 64;
            int missingBits = (blocksNeeded * 64) - m_bitLength;
            int missingBytes = NetUtility.BytesToHoldBits(missingBits);
            for (int i = 0; i < missingBytes; i++)
                Write((byte)0);

            byte[] result = new byte[m_data.Length];
            for (int i = 0; i < blocksNeeded; i++)
                tea.EncryptBlock(m_data, (i * 8), result, (i * 8));
            m_data = result;
        }
        public void Decrypt(NetXtea tea)
        {
            // requires blocks of 8 bytes
            int blocks = m_bitLength / 64;

            if (blocks * 64 != m_bitLength)
            {
                throw new NetException("Wrong message length for XTEA decrypt! Length is " + m_bitLength + " bits");
            }

            byte[] result = new byte[m_data.Length];
            for (int i = 0; i < blocks; i++)
            {
                tea.DecryptBlock(m_data, (i * 8), result, (i * 8));
            }
            m_data = result;
        }
        /// <summary>
        /// Encrypt this message using the XTEA algorithm; no more writing can be done before sending it
        /// </summary>
        public void Encrypt(NetXtea tea)
        {
            // need blocks of 8 bytes
            WritePadBits();
            int blocksNeeded = (m_bitLength + 63) / 64;
            int missingBits  = (blocksNeeded * 64) - m_bitLength;
            int missingBytes = NetUtility.BytesToHoldBits(missingBits);

            for (int i = 0; i < missingBytes; i++)
            {
                Write((byte)0);
            }

            byte[] result = new byte[m_data.Length];
            for (int i = 0; i < blocksNeeded; i++)
            {
                tea.EncryptBlock(m_data, (i * 8), result, (i * 8));
            }
            m_data = result;
        }
		public void Decrypt(NetXtea tea)
		{
			// requires blocks of 8 bytes
			int blocks = m_bitLength / 64;
			if (blocks * 64 != m_bitLength)
				throw new NetException("Wrong message length for XTEA decrypt! Length is " + m_bitLength + " bits");

			byte[] result = new byte[m_data.Length];
			for (int i = 0; i < blocks; i++)
				tea.DecryptBlock(m_data, (i * 8), result, (i * 8));
			m_data = result;
		}
Example #6
0
        /// <summary>
        /// Creates a Lidgren network server with an application name and the port number
        /// to establish the connection.
        /// </summary>
        /// <param name="appName">An application name. Can be any names</param>
        /// <param name="portNumber">The port number to establish the connection</param>
        public LidgrenServer(String appName, int portNumber)
        {
            this.portNumber = portNumber;
            this.appName = appName;
            enableEncryption = false;
            IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());
            IPAddress addr = ipEntry.AddressList[0];
            myIPAddress = addr.GetAddressBytes();
            approveList = new Dictionary<string, string>();
            prevSender = null;
            clients = new Dictionary<string, NetConnection>();
            clientList = new List<NetConnection>();

            // Create a net configuration
            netConfig = new NetPeerConfiguration(appName);
            netConfig.MaximumConnections = 32;
            netConfig.Port = portNumber;
            netConfig.EnableMessageType(NetIncomingMessageType.ConnectionApproval);
            netConfig.EnableMessageType(NetIncomingMessageType.DiscoveryRequest);

            xtea = new NetXtea("GoblinXNA");
            sequenceChannel = 0;
            useSequencedInsteadOfOrdered = false;
        }
Example #7
0
        /// <summary>
        /// Creates a Lidgren network client with an application name, the port number,
        /// and the host name.
        /// </summary>
        /// <param name="appName">An application name. Must be the same as the server app name.</param>
        /// <param name="portNumber">The port number to establish the connection</param>
        /// <param name="hostName">The name of the server machine</param>
        public LidgrenClient(String appName, int portNumber, String hostName)
        {
            this.appName = appName;
            this.portNumber = portNumber;
            isConnected = false;
            isServerDiscovered = false;
            shutDownForced = false;
            enableEncryption = false;
            waitForServer = false;
            connectionTrialTimeout = -1;
            elapsedTime = 0;
            IPHostEntry ipEntry = Dns.GetHostEntry(Dns.GetHostName());
            myAddr = ipEntry.AddressList[0];
            myIPAddress = myAddr.GetAddressBytes();

            IPHostEntry hostEntry = Dns.GetHostEntry(hostName);
            IPAddress hostAddr = hostEntry.AddressList[0];
            hostPoint = new IPEndPoint(hostAddr, portNumber);

            isLocalAddress = IsLocalIpAddress(hostName);

            // Create a configuration for the client
            netConfig = new NetPeerConfiguration(appName);

            xtea = new NetXtea("GoblinXNA");
            sequenceChannel = 0;
            useSequencedInsteadOfOrdered = false;
        }
Example #8
0
        public static void Run(NetPeer peer)
        {
            //
            // Test XTEA
            //
            NetXtea xtea = new NetXtea("TopSecret");

            byte[] original = new byte[16];
            NetRandom.Instance.NextBytes(original);

            byte[] encrypted = new byte[original.Length];
            xtea.EncryptBlock(original, 0, encrypted, 0);
            xtea.EncryptBlock(original, 8, encrypted, 8);

            byte[] decrypted = new byte[original.Length];
            xtea.DecryptBlock(encrypted, 0, decrypted, 0);
            xtea.DecryptBlock(encrypted, 8, decrypted, 8);

            // compare!
            for (int i = 0; i < original.Length; i++)
                if (original[i] != decrypted[i])
                    throw new NetException("XTEA fail!");

            Console.WriteLine("XTEA OK");

            NetOutgoingMessage om = peer.CreateMessage();
            om.Write("Hallon");
            om.Write(42);
            om.Write(5, 5);
            om.Write(true);
            om.Write("kokos");
            om.Encrypt(xtea);

            // convert to incoming message
            NetIncomingMessage im = Program.CreateIncomingMessage(om.PeekDataBuffer(), om.LengthBits);
            im.Decrypt(xtea);

            if (im.ReadString() != "Hallon")
                throw new NetException("fail");
            if (im.ReadInt32() != 42)
                throw new NetException("fail");
            if (im.ReadInt32(5) != 5)
                throw new NetException("fail");
            if (im.ReadBoolean() != true)
                throw new NetException("fail");
            if (im.ReadString() != "kokos")
                throw new NetException("fail");

            for (int i = 0; i < 100; i++)
            {
                byte[] salt = NetSRP.CreateRandomSalt();
                byte[] x = NetSRP.ComputePrivateKey("user", "password", salt);

                byte[] v = NetSRP.ComputeServerVerifier(x);
                //Console.WriteLine("v = " + NetUtility.ToHexString(v));

                byte[] a = NetSRP.CreateRandomEphemeral(); //  NetUtility.ToByteArray("393ed364924a71ba7258633cc4854d655ca4ec4e8ba833eceaad2511e80db2b5");
                byte[] A = NetSRP.ComputeClientEphemeral(a);
                //Console.WriteLine("A = " + NetUtility.ToHexString(A));

                byte[] b = NetSRP.CreateRandomEphemeral(); // NetUtility.ToByteArray("cc4d87a90db91067d52e2778b802ca6f7d362490c4be294b21b4a57c71cf55a9");
                byte[] B = NetSRP.ComputeServerEphemeral(b, v);
                //Console.WriteLine("B = " + NetUtility.ToHexString(B));

                byte[] u = NetSRP.ComputeU(A, B);
                //Console.WriteLine("u = " + NetUtility.ToHexString(u));

                byte[] Ss = NetSRP.ComputeServerSessionValue(A, v, u, b);
                //Console.WriteLine("Ss = " + NetUtility.ToHexString(Ss));

                byte[] Sc = NetSRP.ComputeClientSessionValue(B, x, u, a);
                //Console.WriteLine("Sc = " + NetUtility.ToHexString(Sc));

                if (Ss.Length != Sc.Length)
                    throw new NetException("SRP non matching lengths!");

                for (int j = 0; j < Ss.Length; j++)
                {
                    if (Ss[j] != Sc[j])
                        throw new NetException("SRP non matching session values!");
                }

                var test = NetSRP.CreateEncryption(Ss);
            }

            Console.WriteLine("Message encryption OK");
        }