protected override void IOnConnect() { plBufferedStream bs = new plBufferedStream(new NetworkStream(fSocket, false)); fConnHdr.Write(bs); bs.WriteInt(20); pnHelpers.WriteUuid(bs, Guid.Empty); bs.Flush(); // Encryption if (!base.INetCliConnect(bs, 41)) Close(); bs.Close(); // Listen base.IOnConnect(); }
protected override void IOnConnect() { // Write out the lobby header & the server header plBufferedStream bs = new plBufferedStream(new NetworkStream(fSocket, false)); fConnHdr.Write(bs); bs.Flush(); // Encryption if (!base.INetCliConnect(bs, 2011)) Close(); bs.Close(); // Listen base.IOnConnect(); }
protected bool SetupEncryption(byte[] pubKey, byte[] privKey) { NetworkStream ns = new NetworkStream(fSocket, false); plBufferedStream temp = new plBufferedStream(ns); byte[] yData = IReadNetCliConnect(temp); if (yData != null) { BigNum Y = new BigNum(yData); BigNum N = new BigNum(pubKey); BigNum K = new BigNum(privKey); // Generate some randomness and do Y**K%N to get the key components byte[] server_seed = RNG.Random(7); BigNum client_seed = Y.PowMod(K, N); byte[] seed_data = client_seed.ToLittleArray(); // Grab the first 7 bytes and xor it with some randomness to make our key byte[] key = new byte[7]; for (int i = 0; i < key.Length; i++) if (i >= seed_data.Length) key[i] = server_seed[i]; else key[i] = (byte)(seed_data[i] ^ server_seed[i]); // Final setup fStream = new plBufferedStream(new pnSocketStream(fSocket, key)); IWriteNetCliEncrypt(temp, server_seed); // Explicitly clean up some resources so that we don't pressure the GC too much. // Those explicit finalizers really suck. Y.Dispose(); N.Dispose(); K.Dispose(); client_seed.Dispose(); } else // Write the error code to the unencrypted buffer temp.WriteByte(plNetCore.kNetCliError); temp.Flush(); temp.Close(); ns.Close(); return (yData != null); }