public void SendHandshakeRequest(Stream handshakeRequest) { if (status != DhStatus.None) { throw new InvalidOperationException($"Wrong status {status}, expected: {DhStatus.None}"); } byte[] privateKeyBytes = new byte[64]; byte[] publicKeyBytes = new byte[64]; rngCsp.GetBytes(privateKeyBytes); rngCsp.GetBytes(publicKeyBytes); privateKey = new BigInteger(privateKeyBytes); publicKey = new BigInteger(publicKeyBytes); BigInteger clientMod = ((privateKey * publicKey) % mod); using (WardenStreamWriter writer = new WardenStreamWriter(handshakeRequest, true)) { writer.Write(mark); writer.Write(publicKeyBytes.Length); writer.Write(publicKeyBytes); byte[] clientModBytes = clientMod.ToByteArray(); writer.Write(clientModBytes.Length); writer.Write(clientModBytes); } status = DhStatus.WaitingForServerMod; }
void SetCipherKey() { if (commonKey == null) { throw new InvalidOperationException("Handshake sequence has not been completed. Couldn't set cipher key"); } if (cipher.KeySize == 128) { using (MD5 hash = MD5.Create()) cipher.SetKey(hash.ComputeHash(commonKey)); } else { throw new InvalidOperationException($"{nameof(DiffieHellmanImpl)} supports only ciphers with 128 bit keys"); } status = DhStatus.CommonKeySet; }