/// <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; }
protected NetIncomingMessage DecryptedReceve(NetIncomingMessage message) { NetEncryption algo = new NetXtea(NetPeerObject, "SharedKey45B635DF-649B-4C10-B110-439CE1784C59"); message.Decrypt(algo); return(message); }
/// <summary> /// TODO implement proper private/public key. is that even possible to do transperantly? /// </summary> /// <param name="recever"></param> /// <param name="message"></param> protected NetOutgoingMessage Encrypt(NetOutgoingMessage message) { NetEncryption algo = new NetXtea(NetPeerObject, "SharedKey45B635DF-649B-4C10-B110-439CE1784C59"); message.Encrypt(algo); return(message); }
/// <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; }
private static void Main(String[] args) { NetworkClient client = new NetworkClient(); client.Start(); //client.Connect(); client.DiscoverPeers(); NetClient netClient = client.NetClient; var algo = new NetXtea(netClient, NetworkSetting.Encryptionkey); while (!Console.KeyAvailable || Console.ReadKey().Key != ConsoleKey.Escape) { NetIncomingMessage msg; while ((msg = netClient.ReadMessage()) != null) { msg.Decrypt(algo); switch (msg.MessageType) { case NetIncomingMessageType.DiscoveryResponse: Console.WriteLine("Found server at " + msg.SenderEndPoint + " name: " + msg.ReadString()); client.Connect(msg.SenderEndPoint); break; case NetIncomingMessageType.VerboseDebugMessage: case NetIncomingMessageType.DebugMessage: case NetIncomingMessageType.WarningMessage: case NetIncomingMessageType.ErrorMessage: Console.WriteLine(msg.ReadString()); break; case NetIncomingMessageType.StatusChanged: NetConnectionStatus status = (NetConnectionStatus)msg.ReadByte(); if (status == NetConnectionStatus.Connected) { Console.WriteLine(" connected to" + msg.SenderConnection.RemoteUniqueIdentifier); } break; case NetIncomingMessageType.Data: var data = msg.Data; var message = SerializeTools.Deserialize <GameMessage>(data); Console.WriteLine(message.ClientId + "-" + message.Content + "-" + (Int32)message.MsgCode); client.SendMessage(client.CreateGameMessage <HelloMessage>()); break; } netClient.Recycle(msg); } } client.Shutdown(); }
public void Decrypt() { NetEncryption algorithm = new NetXtea(_netPeer, "EoE"); _message.Decrypt(algorithm); }
public void Encrypt() { NetEncryption algo = new NetXtea(_netPeer, "EoE"); _message.Encrypt(algo); }
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"); }