private BlindSocket ECDH_Server(Socket socket) { if (socket == null) { return(null); } Cryptography.AES256 aes; BlindSocket clientSock; using (ECDiffieHellmanCng dh = new ECDiffieHellmanCng()) { dh.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash; dh.HashAlgorithm = CngAlgorithm.Sha256; byte[] publicKey = dh.PublicKey.ToByteArray(); socket.Send(publicKey, publicKey.Length, SocketFlags.None); byte[] sharekey = new byte[publicKey.Length]; socket.Receive(sharekey, publicKey.Length, SocketFlags.None); byte[] key = dh.DeriveKeyMaterial(CngKey.Import(sharekey, CngKeyBlobFormat.EccPublicBlob)); aes = new Cryptography.AES256(key); clientSock = new BlindSocket(socket, aes); } for (int i = 1; ; i++) { byte[] prevIv = aes.aes.IV; aes.aes.GenerateIV(); byte[] newIv = aes.aes.IV; aes.aes.IV = prevIv; clientSock.CryptoSend(newIv, PacketType.Info); byte[] iv = clientSock.CryptoReceiveMsg(); if (!newIv.SequenceEqual(iv)) { if (i < BlindNetConst.MAXRETRY) { clientSock.CryptoSend(null, PacketType.Retry); } else { clientSock.CryptoSend(null, PacketType.Fail); return(null); } } else { clientSock.CryptoSend(null, PacketType.OK); aes.aes.IV = newIv; break; } } return(clientSock); }
private BlindSocket ECDH_Server(Socket socket) { if (socket == null) { return(null); } Cryptography.AES256 aes; BlindSocket clientSock; using (ECDiffieHellmanCng dh = new ECDiffieHellmanCng()) { dh.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash; dh.HashAlgorithm = CngAlgorithm.Sha256; byte[] publicKey = dh.PublicKey.ToByteArray(); socket.Send(publicKey, publicKey.Length, SocketFlags.None); byte[] sharekey = new byte[publicKey.Length]; socket.Receive(sharekey, publicKey.Length, SocketFlags.None); byte[] key = dh.DeriveKeyMaterial(CngKey.Import(sharekey, CngKeyBlobFormat.EccPublicBlob)); aes = new Cryptography.AES256(key); clientSock = new BlindSocket(socket, aes); } for (int i = 1; ; i++) { string testTxt = BlindNetUtil.GetRandomString(BlindNetConst.MINRNDTXT, BlindNetConst.MAXRNDTXT); clientSock.CryptoSend(Encoding.UTF8.GetBytes(testTxt), PacketType.MSG); var pack = clientSock.CryptoReceive(); string recvTxt = Encoding.UTF8.GetString(pack.data).TrimEnd('\0'); if (recvTxt != testTxt) { if (i < BlindNetConst.MAXRETRY) { clientSock.CryptoSend(null, PacketType.Retry); } else { clientSock.CryptoSend(null, PacketType.Fail); return(null); } } else { break; } } clientSock.CryptoSend(null, PacketType.OK); return(clientSock); }
public BlindSocket(ref BlindSocket blindSocket) { socket = blindSocket.socket; aes = blindSocket.aes; }