예제 #1
0
 public EncryptedClientDataContainer()
 {
     RSAHandler              = new RSACryptographyHandler();
     AESHandler              = new AESCryptographyHandler();
     EncryptNextMessageSync  = true;
     EncryptNextMessageAsync = true;
 }
예제 #2
0
        public EncryptedTcpServer(string serverPublicPrivateKey, int dataPort = DefaultDataPort, int eventPort = DefaultEventPort)
            : base(dataPort, eventPort)
        {
            _serverCryptographyHandler = new RSACryptographyHandler();

            if (serverPublicPrivateKey != null)
            {
                _serverCryptographyHandler.PrivateKey = serverPublicPrivateKey;
            }

            _clients = new Dictionary <TcpConnectedHost, EncryptedClientDataContainer>();

            Register(HandleRSAKeyExchangeRequest);
            Register(HandleAESKeyExchangeRequest);
            Register(HandleAESInitializationVectorExchangeRequest);
        }
예제 #3
0
        private void Init(string serverPublicKey = null)
        {
            _asyncServerCryptographyHandler = new RSACryptographyHandler();
            _asyncClientCryptographyHandler = new RSACryptographyHandler {
                IsReady = true
            };
            _syncCryptographyHandler = new AESCryptographyHandler();

            // Send client's RSA public key and request the server's RSA public key
            var request = new TcpMessage("rsakey")
            {
                Content = _asyncClientCryptographyHandler.PublicKey
            };
            var response = SendAndReceive(request);

            if (response.HasType("rsakey"))
            {
                var key = response.Content;
                if (!string.IsNullOrEmpty(serverPublicKey) && !serverPublicKey.Equals(key))
                {
                    throw new Exception("Server did not return correct public key");
                }
                _asyncServerCryptographyHandler.PublicKey = key;
            }

            // Request the AES key from the server
            request  = new TcpMessage("aeskey");
            response = SendAndReceive(request);
            if (response.HasType("aeskey"))
            {
                _syncCryptographyHandler.KeyAsString = response.Content;
            }

            // Request the AES initialization vector from the server
            request  = new TcpMessage("aesiv");
            response = SendAndReceive(request);
            if (response.HasType("aesiv"))
            {
                _syncCryptographyHandler.InitializationVectorAsString = response.Content;
            }
        }