/// <summary>
        /// Decrypts a secret using the configured <c>SymmetricAlgorithm</c>.
        /// <seealso cref="ISymmetricCryptoProvider.Decrypt"/>
        /// </summary>
        /// <param name="ciphertext"><para>The cipher text to be decrypted.</para></param>
        /// <returns><para>The resulting plain text. It is the responsibility of the caller to clear the returned byte array
        /// when finished.</para></returns>
        /// <seealso cref="ISymmetricCryptoProvider.Decrypt"/>
        public byte[] Decrypt(byte[] ciphertext)
        {
            if (ciphertext == null)
            {
                throw new ArgumentNullException("ciphertext");
            }
            if (ciphertext.Length == 0)
            {
                throw new ArgumentException(Resources.ExceptionByteArrayValueMustBeGreaterThanZeroBytes, "ciphertext");
            }

            byte[] output = null;

            try
            {
                using (SymmetricCryptographer crypto = new SymmetricCryptographer(algorithmType, key))
                {
                    output = crypto.Decrypt(ciphertext);
                }
            }
            catch (Exception e)
            {
                InstrumentationProvider.FireCyptographicOperationFailed(Resources.DecryptionFailed, e);
                throw;
            }
            InstrumentationProvider.FireSymmetricDecryptionPerformed();

            return(output);
        }
Example #2
0
        /// <summary>
        /// Decrypts a secret using the configured <c>SymmetricAlgorithm</c>.
        /// <seealso cref="ISymmetricCryptoProvider.Decrypt"/>
        /// </summary>
        /// <param name="ciphertext"><para>The cipher text for which you want to decrypt.</para></param>
        /// <returns><para>The resulting plain text.</para></returns>
        /// <seealso cref="ISymmetricCryptoProvider.Decrypt"/>
        public byte[] Decrypt(byte[] ciphertext)
        {
            ArgumentValidation.CheckForNullReference(ciphertext, "encryptedText");
            ArgumentValidation.CheckForZeroBytes(ciphertext, "encryptedText");

            byte[] output = null;

            SymmetricAlgorithmProviderData data = GetSymmetricAlgorithmProviderDataFromCursor();

            SymmetricCryptographer crypto = new SymmetricCryptographer(data.AlgorithmType, data.Key);

            output = crypto.Decrypt(ciphertext);
            SecurityCryptoSymmetricDecryptionEvent.Fire(string.Empty);
            return(output);
        }
        public void EncryptAndDecryptWithType()
        {
            byte[] key = new byte[16];
            CryptographyUtility.GetRandomBytes(key);
            ProtectedKey protectedKey = ProtectedKey.CreateFromPlaintextKey(key, DataProtectionScope.LocalMachine);

            SymmetricCryptographer symm = new SymmetricCryptographer(typeof(RijndaelManaged), protectedKey);

            byte[] plainText = new byte[12];
            CryptographyUtility.GetRandomBytes(plainText);

            byte[] cipherText = symm.Encrypt(plainText);
            Assert.IsFalse(CryptographyUtility.CompareBytes(cipherText, plainText));

            byte[] decryptedText = symm.Decrypt(cipherText);
            Assert.IsTrue(CryptographyUtility.CompareBytes(plainText, decryptedText));
        }
        /// <summary>
        /// Decrypts a secret using the configured <c>SymmetricAlgorithm</c>.
        /// <seealso cref="ISymmetricCryptoProvider.Decrypt"/>
        /// </summary>
        /// <param name="ciphertext"><para>The cipher text to be decrypted.</para></param>
        /// <returns><para>The resulting plain text. It is the responsibility of the caller to clear the returned byte array
        /// when finished.</para></returns>
        /// <seealso cref="ISymmetricCryptoProvider.Decrypt"/>
        public byte[] Decrypt(byte[] ciphertext)
        {
            if (ciphertext == null) throw new ArgumentNullException("ciphertext");
            if (ciphertext.Length == 0) throw new ArgumentException(Resources.ExceptionByteArrayValueMustBeGreaterThanZeroBytes, "ciphertext");

            byte[] output = null;

            try
            {
                using (SymmetricCryptographer crypto = new SymmetricCryptographer(algorithmType, key))
                {
                    output = crypto.Decrypt(ciphertext);
                }
            }
            catch (Exception e)
            {
                InstrumentationProvider.FireCyptographicOperationFailed(Resources.DecryptionFailed, e);
                throw;
            }
            InstrumentationProvider.FireSymmetricDecryptionPerformed();

            return output;
        }