Пример #1
0
        /// <summary>
        /// encrypt the request
        /// </summary>
        protected void ProcessRequest(ITransportHeaders headers, ref Stream stream)
        {
            if (FEncryptionKey == null)
            {
                // create a symmetric key
                Rijndael alg = new RijndaelManaged();
                alg.GenerateKey();
                FEncryptionKey = alg.Key;
                SendKeyAgain   = true;
            }

            if (SendKeyAgain)
            {
                // tell the server the symmetric key,
                // but encrypt with the public key of the server.
                // this means that only the server can read the secret key.
                RSACryptoServiceProvider serverRSA = new RSACryptoServiceProvider();
                serverRSA.ImportParameters(FPublicKeyServer);
                string encryptedSymmetricKey = Convert.ToBase64String(serverRSA.Encrypt(FEncryptionKey, false));
                headers[EncryptionRijndael.GetEncryptionName() + "KEY"] = encryptedSymmetricKey;
                SendKeyAgain = false;
            }

            headers["ClientGuid"] = CurrentClientGuid;

            byte[] EncryptionIV;
            stream = EncryptionRijndael.Encrypt(FEncryptionKey, stream, out EncryptionIV);
            headers[EncryptionRijndael.GetEncryptionName()] = "Yes";

            // the initialisation vector is no secret, but we need to generate it for each encryption, and it is needed for decryption
            headers[EncryptionRijndael.GetEncryptionName() + "IV"] = Convert.ToBase64String(EncryptionIV);
        }
Пример #2
0
        /// <summary>
        /// encrypt the response
        /// </summary>
        protected void ProcessResponse(ITransportHeaders headers, ref Stream stream, object state, string AClientGuid)
        {
            if (state != null)
            {
                byte[] EncryptionIV;
                stream = EncryptionRijndael.Encrypt(FEncryptionKeys[AClientGuid], stream, out EncryptionIV);
                headers[EncryptionRijndael.GetEncryptionName()] = "Yes";

                // the initialisation vector is no secret, but we need to generate it for each encryption, and it is needed for decryption
                headers[EncryptionRijndael.GetEncryptionName() + "IV"] = Convert.ToBase64String(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);
        }