我正在尝试使用microsoft Security.Cryptography.Debug.dll
调试加密函数我在项目的下载中引用了3个Dll。我的加密函数按预期填充SymmetricEncryptionState
对象,但我的解密函数抛出CryptographicException
而不是我期待的CryptographicDiagnosticException
。
SymmetricEncryptionState encryptionState = null; //set by encrypt function
public MemoryStream DecryptFileStream(byte[] cypherStream, string password, string salt)
{
var output = new MemoryStream();
using (SymmetricAlgorithm AESProvider = new AesManaged().EnableDecryptionVerification(encryptionState))
{
var key = new Rfc2898DeriveBytes(password, Encoding.ASCII.GetBytes(salt));
AESProvider.KeySize = 256;
AESProvider.BlockSize = 128;
AESProvider.Key = key.GetBytes(AESProvider.KeySize / 8);
AESProvider.IV = key.GetBytes(AESProvider.BlockSize / 8);
try
{
CryptoStream plainTextStream = new CryptoStream(output, AESProvider.CreateDecryptor(), CryptoStreamMode.Write);
byte[] buffer = new byte[cypherStream.Length];
plainTextStream.Write(buffer, 0, buffer.Length);
plainTextStream.FlushFinalBlock(); // this line throws the wrong exception type
}
catch (CryptographicDiagnosticException ex)
{
throw new Exception(ex.Message);
}
}
return output;
}
我收到'填充无效且无法删除。' FlushFinalBlock()
来电时出错。
如果有帮助,我的项目目标是.net 4.5
有没有人对此有任何想法?