private void IWriteNetClientConnect(plBufferedStream s, byte[] seed) { s.WriteByte(plNetCore.kNetCliConnect); s.WriteByte(66); s.WriteBytes(seed); s.Flush(); }
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); }