Example #1
0
 /// <summary>
 /// decrypt the response
 /// </summary>
 protected void ProcessResponse(ITransportHeaders headers, ref Stream stream)
 {
     if (headers[EncryptionRijndael.GetEncryptionName()] != null)
     {
         byte[] EncryptionIV = Convert.FromBase64String((String)headers[EncryptionRijndael.GetEncryptionName() + "IV"]);
         stream = EncryptionRijndael.Decrypt(FEncryptionKey, stream, EncryptionIV);
     }
 }
        public void EncipherTest()
        {
            uint[] keys =
            {
                12, 23, 34, 45, 56, 67, 78, 89,
            };

            byte[] expected = new byte[] { 96, 97, 98, 99 };

            var encrypter = new EncryptionRijndael();

            byte[] ciphertext = encrypter.Encrypt(expected, keys);
            byte[] actual     = encrypter.Decrypt(ciphertext, keys);

            CollectionAssert.AreEqual(expected, actual);
        }
        /// <summary>
        /// decrypt the request
        /// </summary>
        protected string ProcessRequest(ITransportHeaders headers, ref Stream stream, ref object state)
        {
            if (headers[EncryptionRijndael.GetEncryptionName()] != null)
            {
                string ClientGuid = headers["ClientGuid"].ToString();

                if (headers[EncryptionRijndael.GetEncryptionName() + "KEY"] != null)
                {
                    // read the symmetric key, which has been encrypted with our public key
                    RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
                    RSA.ImportParameters(FPrivateKey);
                    // this can overwrite the encryption key of another connection?
                    byte[] EncryptionKey = RSA.Decrypt(
                        Convert.FromBase64String((String)headers[EncryptionRijndael.GetEncryptionName() + "KEY"]), false);

                    if (!FEncryptionKeys.ContainsKey(ClientGuid))
                    {
                        FEncryptionKeys.Add(ClientGuid, EncryptionKey);
                    }
                    else
                    {
                        FEncryptionKeys[ClientGuid] = EncryptionKey;
                    }
                }

                byte[] EncryptionIV = Convert.FromBase64String((String)headers[EncryptionRijndael.GetEncryptionName() + "IV"]);
                stream = EncryptionRijndael.Decrypt(FEncryptionKeys[ClientGuid], stream, EncryptionIV);
                state  = true;

                return(ClientGuid);
            }
            else
            {
                throw new Exception("EncryptionServerSink: We cannot allow non encrypted traffic");
            }
        }