public static bool AESDecrypt(string filePath, string strKey)
{
SymmetricAlgorithm aes = Rijndael.Create();
FileStream fs = File.OpenRead(filePath);
aes.Key = Encoding.UTF8.GetBytes(strKey);
aes.IV = _key1;
byte[] decryptBytes = new byte[fs.Length];
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Read);
cs.Read(decryptBytes, 0,(int)fs.Length);//这里出错了,说“索引超出了数组界限。”
cs.FlushFinalBlock();
fs.Close();
cs.Close();
ms.Close();
return true;
}
应该怎么改啊?
{
SymmetricAlgorithm aes = Rijndael.Create();
FileStream fs = File.OpenRead(filePath);
aes.Key = Encoding.UTF8.GetBytes(strKey);
aes.IV = _key1;
byte[] decryptBytes = new byte[fs.Length];
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Read);
cs.Read(decryptBytes, 0,(int)fs.Length);//这里出错了,说“索引超出了数组界限。”
cs.FlushFinalBlock();
fs.Close();
cs.Close();
ms.Close();
return true;
}
应该怎么改啊?
调试后
“fs.Length|112”
不是的,改成long,int64,编译都不能通过,改成int32编译虽然能过,但还是原来的问题
cs.Read(decryptBytes, 0,(int)fs.Length-1); //索引从0开始长度从1开始
这个可能有问题吧
改成这个
cs.Read(decryptBytes, 0,fs.Length-97); “要求非负数。”
悲剧不?
{
Try
{
SymmetricAlgorithm aes = Rijndael.Create();
FileStream fs = File.OpenRead(filePath);
aes.Key = Encoding.UTF8.GetBytes(strKey);
aes.IV = _key1;
byte[] decryptBytes = new byte[fs.Length];
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Read);
cs.Read(decryptBytes, 0,(int)fs.Length);//这里出错了,说“索引超出了数组界限。”
cs.FlushFinalBlock();
fs.Close();
cs.Close();
ms.Close();
return true;
}
catch(Exeception)
{
messageBox.show();
}
}
当fs.length=0时,也就是decryptBytes长度为0cs.Read(decryptBytes, 0,fs.Length-1)和cs.Read(decryptBytes, 0,fs.Length)都会超出范围,首先应判断fs.length>0然后cs.Read(decryptBytes, 0,fs.Length-1)
cs.Read(decryptBytes, 0,decryptBytes.Length);而且貌似楼主的decryptBytes是空的吧...