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); }
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); }
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); }
public PasswordDerivedKey(SecureString password, bool newHashAlgorithm) : this(PasswordDerivedKey.GetPasswordDeriveBytes(password, newHashAlgorithm)) { }