public int CryptoSendMiniPacket(byte[] data, PacketType header) { if (data == null) { data = new byte[BlindNetConst.MINIDATASIZE]; } int realSendBytes = 0; BlindMiniPacket pack = new BlindMiniPacket(); pack.header = header; pack.data = new byte[BlindNetConst.MINIDATASIZE]; Array.Copy(data, 0, pack.data, 0, data.Length); byte[] encrypted = aes.Encryption(BlindNetUtil.StructToByte(pack)); realSendBytes = socket.Send(encrypted, BlindNetConst.MINIPACKSIZE, SocketFlags.None); new NetworkStream(socket).Flush(); byte[] result = new byte[BlindNetConst.MINIPACKSIZE]; int rcvNum = socket.Receive(result, BlindNetConst.MINIPACKSIZE, SocketFlags.None); if (result[0] != (byte)PacketType.OK) { return(0); } return(realSendBytes); }
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 byte[] CryptoReceiveMsg() { bool recvMode = false; BlindPacket packet = new BlindPacket(); byte[] result = null; do { packet = CryptoReceive(recvMode); if (packet.header == PacketType.Disconnect) { return(null); } result = BlindNetUtil.MergeArray <byte>(result, packet.data); Debug.WriteLine("Receive {0}/{1} bytes", packet.data.Length, result.Length); recvMode = true; } while (packet.header == PacketType.Sending); result = BlindNetUtil.ByteTrimEndNull(result); return(result); }
public BlindPacket CryptoReceive(bool isRecieving = false) { byte[] data = new byte[BlindNetConst.MINIPACKSIZE]; int rcvNum = 0; byte[] decrypted = null; BlindMiniPacket miniPacket = new BlindMiniPacket(); if (!isRecieving) { rcvNum = socket.Receive(data, BlindNetConst.MINIPACKSIZE, SocketFlags.None); new NetworkStream(socket).Flush(); if (rcvNum == 0) { BlindPacket end; end.data = null; end.header = PacketType.Disconnect; return(end); } byte[] temp = new byte[BlindNetConst.MINIPACKSIZE]; temp[0] = (byte)PacketType.OK; socket.Send(temp, BlindNetConst.MINIDATASIZE, SocketFlags.None); decrypted = aes.Decryption(data); miniPacket = BlindNetUtil.ByteToStruct <BlindMiniPacket>(decrypted); } if (isRecieving || miniPacket.header == PacketType.MSG) { data = new byte[0]; while (true) { byte[] tmp = new byte[BlindNetConst.PACKSIZE]; rcvNum = socket.Receive(tmp, BlindNetConst.PACKSIZE - data.Length, SocketFlags.None); using (NetworkStream stream = new NetworkStream(socket)) stream.Flush(); #if DEBUG if (tmp[tmp.Length - 1] == 0) { Console.WriteLine("Received less bytes"); } #endif if (rcvNum == 0) { BlindPacket end; end.data = null; end.header = PacketType.Disconnect; return(end); } data = BlindNetUtil.MergeArray <byte>(data, BlindNetUtil.ByteTrimEndNull(tmp)); if (data.Length == BlindNetConst.PACKSIZE) { byte[] temp = new byte[BlindNetConst.MINIPACKSIZE]; temp[0] = (byte)PacketType.OK; socket.Send(temp, BlindNetConst.MINIPACKSIZE, SocketFlags.None); using (NetworkStream stream = new NetworkStream(socket)) stream.Flush(); break; } } decrypted = aes.Decryption(data); } return(BlindNetUtil.ByteToStruct <BlindPacket>(decrypted)); }