Пример #1
0
        private byte[] ReceiveData(Socket handler)
        {
            RSACryptoServiceProvider cryptoProvider = InitializeAsymmetricCyphering();

            byte[] publicKey = cryptoProvider.ExportCspBlob(false);
            handler.Send(publicKey);
            byte[] encryptedSymmetricKey = new byte[512];
            handler.Receive(encryptedSymmetricKey);
            byte[] encryptedData = DataExtensions.ReceiveMessageBySocket(handler);
            byte[] symmetricKey  = cryptoProvider.Decrypt(encryptedSymmetricKey, RSAEncryptionPadding.Pkcs1);
            cryptoProvider.Dispose();
            return(encryptedData.Decrypt(symmetricKey).Decompress());
        }
Пример #2
0
        public override void Send(byte[] data)
        {
            IPEndPoint remoteEP = new IPEndPoint(serverIpAddress, serverPort);

            byte[] encryptedData = data.Compress().Encrypt(out byte[] symmetricKey);
            using (Socket client = new Socket(serverIpAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp)
            {
                ReceiveTimeout = 10000,
                SendTimeout = 10000
            })
            {
                client.Connect(remoteEP);
                byte[] publicKey = DataExtensions.ReceiveMessageBySocket(client);
                if (publicKey.Length == 0)
                {
                    throw new ExchangeException();
                }
                byte[] encryptedSymmetricKey = EncodeSymmetricKey(symmetricKey, publicKey);
                client.Send(encryptedSymmetricKey);
                client.Send(encryptedData);
                client.Shutdown(SocketShutdown.Both);
            }
        }