我的加密代码是
{
AesManaged aes = null;
MemoryStream memoryStream = null;
CryptoStream cryptoStream = null;
try
{
Rfc2898DeriveBytes rfc2898 = new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes(salt), 10000);
aes = new AesManaged();
aes.Key = rfc2898.GetBytes(32);
aes.IV = rfc2898.GetBytes(16);
memoryStream = new MemoryStream();
cryptoStream = new CryptoStream(memoryStream, aes.CreateEncryptor(), CryptoStreamMode.Write);
byte[] data = Encoding.UTF8.GetBytes(dataToEncrypt);
cryptoStream.Write(data, 0, data.Length);
cryptoStream.FlushFinalBlock();
return Convert.ToBase64String(memoryStream.ToArray());
}
finally
{
if (cryptoStream != null)
cryptoStream.Close();
if (memoryStream != null)
memoryStream.Close();
if (aes != null)
aes.Clear();
}
}
我刚试过Code Analysis它给了我
CA2202
Do not dispose objects multiple times
Object 'memoryStream' can be disposed more than once in method 'EncryptDecrypt.Encrypt(string, string, string)'.
To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object.
但我的代码工作正常我创建了memoryStream& cryptoStream&在......之后关闭它们但是我无法理解它为什么多次告诉我多个对象
答案 0 :(得分:1)
IDisposable
指出两次处理同一对象的准则应该第二次没有效果。
但是,并非所有实现都遵循此指南,因此Code Analysis会告诉您不要依赖它。
您的特定物品在这方面是安全的,因此您没有实际问题。