private void secondHandshake() { byte [] preRequest = new byte [1]; byte [] request = new byte [24]; byte [] response = request; preRequest [0] = TypeNewconn; ulong privateKey; ulong publicKey; DH64 dh64 = new DH64(); dh64.KeyPair(out privateKey, out publicKey); using (MemoryStream ms = new MemoryStream(request, 0, 8)) { using (BinaryWriter w = new BinaryWriter(ms)) { w.Write(publicKey); } } _BaseStream.Write(preRequest, 0, preRequest.Length); _BaseStream.Write(request, 0, 8); for (int n = 24; n > 0;) { int x = _BaseStream.Read(response, 24 - n, n); if (x == 0) { throw new EndOfStreamException(); } n -= x; } ulong challengeCode = 0; using (MemoryStream ms = new MemoryStream(response, 0, 24)) { using (BinaryReader r = new BinaryReader(ms)) { ulong serverPublicKey = r.ReadUInt64(); ulong secret = dh64.Secret(privateKey, serverPublicKey); using (MemoryStream ms2 = new MemoryStream(_Key)) { using (BinaryWriter w = new BinaryWriter(ms2)) { w.Write(secret); } } _ReadCipher = new RC4Cipher(_Key); _WriteCipher = new RC4Cipher(_Key); _ReadCipher.XORKeyStream(response, 8, response, 8, 8); _ID = r.ReadUInt64(); using (MemoryStream ms2 = new MemoryStream(request, 0, 16)) { using (BinaryWriter w = new BinaryWriter(ms2)) { w.Write(response, 16, 8); w.Write(_Key); MD5 md5 = MD5CryptoServiceProvider.Create(); byte [] hash = md5.ComputeHash(request, 0, 16); Buffer.BlockCopy(hash, 0, request, 0, hash.Length); _BaseStream.Write(request, 0, 16); } } } } }
private void handshake() { byte[] preRequest = new byte[1]; byte[] request = new byte[24]; byte[] response = request; preRequest[0] = TypeNewconn; ulong privateKey; ulong publicKey; DH64 dh64 = new DH64(); dh64.KeyPair(out privateKey, out publicKey); using (MemoryStream ms = new MemoryStream(request, 0, 8)) { using (BinaryWriter w = new BinaryWriter(ms)) { w.Write(publicKey); } } TcpClient client = new TcpClient(_Host.AddressFamily); var ar = client.BeginConnect(_Host, _Port, null, null); ar.AsyncWaitHandle.WaitOne(new TimeSpan(0, 0, 0, 0, ConnectTimeout)); if (!ar.IsCompleted) { throw new TimeoutException(); } client.EndConnect(ar); setBaseStream(client.GetStream()); _BaseStream.Write(preRequest, 0, preRequest.Length); _BaseStream.Write(request, 0, 8); for (int n = 24; n > 0;) { int x = _BaseStream.Read(response, 24 - n, n); if (x == 0) { throw new EndOfStreamException(); } n -= x; } ulong challengeCode = 0; using (MemoryStream ms = new MemoryStream(response, 0, 24)) { using (BinaryReader r = new BinaryReader(ms)) { ulong serverPublicKey = r.ReadUInt64(); ulong secret = dh64.Secret(privateKey, serverPublicKey); using (MemoryStream ms2 = new MemoryStream(_Key)) { using (BinaryWriter w = new BinaryWriter(ms2)) { w.Write(secret); } } _ReadCipher = new RC4Cipher(_Key); _WriteCipher = new RC4Cipher(_Key); _ReadCipher.XORKeyStream(response, 8, response, 8, 8); _ID = r.ReadUInt64(); using (MemoryStream ms2 = new MemoryStream(request, 0, 16)) { using (BinaryWriter w = new BinaryWriter(ms2)) { w.Write(response, 16, 8); w.Write(_Key); MD5 md5 = MD5CryptoServiceProvider.Create(); byte[] hash = md5.ComputeHash(request, 0, 16); Buffer.BlockCopy(hash, 0, request, 0, hash.Length); _BaseStream.Write(request, 0, 16); } } } } }