/// <summary>
 /// Initializes a new instance of the StackableEncryptor, with a single <see cref="IEncryptorConfiguration"/>.
 /// </summary>
 /// <param name="encryptorConfiguration">The <see cref="IEncryptorConfiguration"/> to implement during encryption and decryption.</param>
 public StackableEncryptor(IEncryptorConfiguration encryptorConfiguration)
     : this()
 {
     if (encryptorConfiguration == null)
     {
         throw new ArgumentNullException("encryptorConfiguration");
     }
     _EncryptorConfigurations = new List <IEncryptorConfiguration> {
         encryptorConfiguration
     };
 }
Example #2
0
        /// <summary>
        /// Will decrypt the <paramref name="source"/> byte array using the given <paramref name="encryptorConfiguration"/>.
        /// </summary>
        /// <param name="source">The byte array to decrypt.</param>
        /// <param name="encryptorConfiguration">The encryption configuration used to decrypt the <paramref name="source"/> byte array.</param>
        /// <returns>The <paramref name="source"/> byte array decrypted using the given <paramref name="encryptorConfiguration"/>.</returns>
        public static byte[] Decrypt(byte[] source,
                                     IEncryptorConfiguration encryptorConfiguration)
        {
            // TODO: Fix This. Problem Id= 3333E15BFE8746CEAA1D5664BFFD0E18
            //      What's happening here is that I'm using the IEncryptorConfiguration.PasswordSaltHash as the PASSWORD for the PasswordDeriveBytes type; that's not right!
            //      According to Microsoft documentation, it is using SHA1 by default.
            // Must Rethink This!

            if ((source == null) || (source.Length == 0))
            {
                return(source);
            }
            if (encryptorConfiguration == null)
            {
                return(source);
            }
            System.Security.Cryptography.PasswordDeriveBytes passwordDerivedBytes = null;
            using (var ec = encryptorConfiguration.SymmetricAlgorithm)
            {
                passwordDerivedBytes = new System.Security.Cryptography.PasswordDeriveBytes(encryptorConfiguration.SaltedPassword.PasswordSaltHash,
                                                                                            encryptorConfiguration.SaltedPassword.Salt);
                ec.Key = passwordDerivedBytes.GetBytes(ec.KeySize / 8);
                ec.IV  = passwordDerivedBytes.GetBytes(ec.BlockSize / 8);
                using (var ms = new System.IO.MemoryStream())
                {
                    using (var cs = new System.Security.Cryptography.CryptoStream(ms,
                                                                                  ec.CreateDecryptor(),
                                                                                  System.Security.Cryptography.CryptoStreamMode.Write))
                    {
                        cs.Write(source, 0, source.Length);
                        cs.Close();
                        return(ms.ToArray());
                    }
                }
            }
        }