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;
        }
应该怎么改啊?

解决方案 »

  1.   

    你单步调试下看看fs.Length是多少?
      

  2.   


    调试后
    “fs.Length|112”
      

  3.   

    是不是内容超长,可以改下int类型int32或longcs.Read(decryptBytes, 0,(long)fs.Length)
      

  4.   


    不是的,改成long,int64,编译都不能通过,改成int32编译虽然能过,但还是原来的问题
      

  5.   

    没必要强制转换吧 (int)fs.Length    x
      

  6.   

             cs.Read(decryptBytes, 0,(int)fs.Length-1)
      

  7.   

    byte[] decryptBytes = new byte[fs.Length-1];
      

  8.   

    一般说来越界都是由于length没减一造成的。不过lz用的这个东西我没见过, 不敢妄下评论。
      

  9.   

    cs.Read()的第三个属性是什么??长度?  截止位置???cs.Read(decryptBytes, 0,(int)fs.Length-1)
      

  10.   


    cs.Read(decryptBytes, 0,(int)fs.Length-1); //索引从0开始长度从1开始
      

  11.   

    换成cs.Read(decryptBytes, 0,112)能通过不?
      

  12.   

    byte[] decryptBytes = new byte[fs.Length];
    这个可能有问题吧
      

  13.   

    你调式(int)fs.Length等于多少???
      

  14.   

    cs.Read(decryptBytes, 0,fs.Length-1); 
    改成这个
      

  15.   

    decryptBytes  这个有问题!!!!!1
      

  16.   

    cs.Read(decryptBytes, 0,fs.Length-96);“索引超出了数组界限。” 
    cs.Read(decryptBytes, 0,fs.Length-97); “要求非负数。”
    悲剧不?
      

  17.   

    http://home.fangte.com/space.php?uid=132652&do=blog&id=1346 看看吧 ~
      

  18.   

    public static bool AESDecrypt(string filePath, string strKey)
            {
             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();
               }
            }
      

  19.   



    当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)
      

  20.   

    楼上的说的也有可能,但是fs.length-1是不行的。因为第三个参数是要读取的字节数。但是当出现上述错误时肯定是数组越界了。就看看你的数组大小和你要读取的字节数是否不一致!不知道你的问题解决了没有、。
      

  21.   


    cs.Read(decryptBytes, 0,decryptBytes.Length);而且貌似楼主的decryptBytes是空的吧...
      

  22.   

    这种问题就别发帖了。单步跟一下,找出fs.Length和decryptBytes的维数一看不就知道哪里出问题了。
      

  23.   

    目测是都用fs.length出错,可能进入byte[] decryptBytes = new byte[fs.Length];的时候,fs.length是一个数,而进入cs.Read(decryptBytes, 0,(int)fs.Length);的时候,fs.length的值改变了,所以会报错