我正在尝试使用这个函数解密,但是我不确定是什么导致它失败的。
public string Decrypt(byte[] cipherText, byte[] IV, byte[] key)
{
using (AesCryptoServiceProvider AESDecrypt = new AesCryptoServiceProvider())
{
//here we set the key and IV instead having the class generate them.
AESDecrypt.IV = IV;
AESDecrypt.Key = key;
ICryptoTransform decryptor = AESDecrypt.CreateDecryptor(AESDecrypt.Key,
AESDecrypt.IV);
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt,
decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
csDecrypt.Flush();
plainText = srDecrypt.ReadToEnd();
return plainText;
}
}
}
}
}plainText返回一个空字符串。密钥和IV是在以前的函数中生成的系统,并且正在被正确地传递。
发布于 2014-12-13 16:13:01
我误读了第一个问题的答案,并使用.flushfinalblock让它返回“密文”,而没有真正解决根本的问题。当我移动密码= msEncrypt.ToArray();在密码流之外,它工作得很好。并删除了.flush()。.FacePalm()
https://stackoverflow.com/questions/27459964
复制相似问题