Beispiel #1
0
        private static byte[] DecryptWithPassword(SecureString password, string toDecrypt)
        {
            PasswordDerivedKey passwordDerivedKey = new PasswordDerivedKey(password);

            byte[] result;
            try
            {
                result = passwordDerivedKey.Decrypt(Convert.FromBase64String(toDecrypt));
            }
            catch (CryptographicException)
            {
                PasswordDerivedKey passwordDerivedKey2 = new PasswordDerivedKey(password, false);
                try
                {
                    result = passwordDerivedKey2.Decrypt(Convert.FromBase64String(toDecrypt));
                }
                finally
                {
                    passwordDerivedKey2.Clear();
                }
            }
            finally
            {
                passwordDerivedKey.Clear();
            }
            return(result);
        }
Beispiel #2
0
 private static byte[] GetPasswordDeriveBytes(SecureString password, bool newHashAlgorithm)
 {
     if (newHashAlgorithm)
     {
         return(PasswordDerivedKey.RmsPasswordDeriveBytes.GetBytes(password, 16));
     }
     byte[] bytes;
     using (PasswordDeriveBytes passwordDeriveBytes = new PasswordDeriveBytes(PasswordDerivedKey.ConvertFromSecureString(password), null))
     {
         bytes = passwordDeriveBytes.GetBytes(16);
     }
     return(bytes);
 }
Beispiel #3
0
 public static byte[] GetBytes(SecureString password, int numberOfBytes)
 {
     if (password == null)
     {
         throw new ArgumentException("password");
     }
     if (numberOfBytes > 32)
     {
         throw new ArgumentOutOfRangeException("numberOfBytes");
     }
     byte[] array = null;
     using (SHA256CryptoServiceProvider sha256CryptoServiceProvider = new SHA256CryptoServiceProvider())
     {
         array = sha256CryptoServiceProvider.ComputeHash(PasswordDerivedKey.ConvertFromSecureStringToByteArray(password));
     }
     byte[] array2 = new byte[numberOfBytes];
     for (int i = 0; i < numberOfBytes; i++)
     {
         array2[i] = array[i];
     }
     return(array2);
 }
Beispiel #4
0
 public PasswordDerivedKey(SecureString password, bool newHashAlgorithm) : this(PasswordDerivedKey.GetPasswordDeriveBytes(password, newHashAlgorithm))
 {
 }