索引超出了数组的范围。 Rijndael 加密

发布于 2024-12-26 10:53:08 字数 2439 浏览 2 评论 0原文

在花时间阅读本文之前,请参阅下面的自我回答。问题是输入无效。


当我尝试解密 some 字符串时,它会抛出此异常:

    "   at System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode paddingMode, Boolean fLast)
       at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
       at System.Security.Cryptography.CryptoStream.FlushFinalBlock()
       at System.Security.Cryptography.CryptoStream.Dispose(Boolean disposing)
       at System.IO.Stream.Close()
       at System.IO.Stream.Dispose()
       at EBookReader.cryptography.DecryptString(String message, String KeyString, String IVString) in C:\\Users\\XWare\\Documents\\Visual Studio 2008\\Projects\\EBookReader\\EBookReader\\cryptography.cs:line 94"

在调试下,它会在这一行抛出异常:

    byte[] messageBytes = Convert.FromBase64String(message);

我认为当要解密的字符串太大时,就会出现此问题,因为当我尝试加密和解密时像“嗨,我是 X-Ware”这样的短字符串可以正常工作,

    public static string DecryptString(string message, string KeyString, string IVString)
    {
        byte[] Key = ASCIIEncoding.UTF8.GetBytes(KeyString);
        byte[] IV = ASCIIEncoding.UTF8.GetBytes(IVString);

        string decrypted = null;
        RijndaelManaged rj = new RijndaelManaged();
        rj.BlockSize = 256;
        rj.Key = Key;
        rj.IV = IV;
        rj.Mode = CipherMode.CBC;
        rj.Padding = PaddingMode.PKCS7;
        try
        {
            MemoryStream ms = new MemoryStream();
            Encoding enc = new ASCIIEncoding();
            using (CryptoStream cs = new CryptoStream(ms, rj.CreateDecryptor(Key, IV), CryptoStreamMode.Write))
            {
                byte[] messageBytes = Convert.FromBase64String(message);
                cs.Write(messageBytes, 0, messageBytes.Length);
                cs.FlushFinalBlock();  
            }// This is line 94
            byte[] encoded = ms.ToArray();
            decrypted = enc.GetString(encoded);

            ms.Close();
        }
        catch (Exception e)
        {
            Console.WriteLine("An error occurred: {0}", e.Message);
        }
        finally
        {
            rj.Clear();
        }

        return decrypted;
    }

有什么建议吗?!

PS我写了一条评论来告诉你第94行//这是第94行

Before spending any time reading this, see the self-answer below. The problem was invalid input.


When I try to decrypt some strings it throws this exception:

    "   at System.Security.Cryptography.RijndaelManagedTransform.DecryptData(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount, Byte[]& outputBuffer, Int32 outputOffset, PaddingMode paddingMode, Boolean fLast)
       at System.Security.Cryptography.RijndaelManagedTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
       at System.Security.Cryptography.CryptoStream.FlushFinalBlock()
       at System.Security.Cryptography.CryptoStream.Dispose(Boolean disposing)
       at System.IO.Stream.Close()
       at System.IO.Stream.Dispose()
       at EBookReader.cryptography.DecryptString(String message, String KeyString, String IVString) in C:\\Users\\XWare\\Documents\\Visual Studio 2008\\Projects\\EBookReader\\EBookReader\\cryptography.cs:line 94"

Under debugging it throws the exception on this line :

    byte[] messageBytes = Convert.FromBase64String(message);

I think this problem just appears when string to decrypt is too big because when I try to encrypt and decrypt short strings like "Hi there I am X-Ware" it works fine

    public static string DecryptString(string message, string KeyString, string IVString)
    {
        byte[] Key = ASCIIEncoding.UTF8.GetBytes(KeyString);
        byte[] IV = ASCIIEncoding.UTF8.GetBytes(IVString);

        string decrypted = null;
        RijndaelManaged rj = new RijndaelManaged();
        rj.BlockSize = 256;
        rj.Key = Key;
        rj.IV = IV;
        rj.Mode = CipherMode.CBC;
        rj.Padding = PaddingMode.PKCS7;
        try
        {
            MemoryStream ms = new MemoryStream();
            Encoding enc = new ASCIIEncoding();
            using (CryptoStream cs = new CryptoStream(ms, rj.CreateDecryptor(Key, IV), CryptoStreamMode.Write))
            {
                byte[] messageBytes = Convert.FromBase64String(message);
                cs.Write(messageBytes, 0, messageBytes.Length);
                cs.FlushFinalBlock();  
            }// This is line 94
            byte[] encoded = ms.ToArray();
            decrypted = enc.GetString(encoded);

            ms.Close();
        }
        catch (Exception e)
        {
            Console.WriteLine("An error occurred: {0}", e.Message);
        }
        finally
        {
            rj.Clear();
        }

        return decrypted;
    }

any suggestions ?!

P.S. I wrote a comment to show you line 94 // This is line 94

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ㄖ落Θ余辉 2025-01-02 10:53:08

FlushFinalBlock 方法仅用于刷新最后一个完整或部分字节块。因此,如果您编写的块大小大于单个块大小,则这不是正确的方法调用。在尝试从内存流中读取数据之前,请尝试简单地关闭加密流。

要在代码上下文中执行此操作,只需删除行 cs.FlushFinalBlock()...

The FlushFinalBlock method is only intended to flush the last complete or partial block of bytes. Thus if you write more than a single block size it is not the right method call to make. Try instead simply closing the crypto stream before you attempt to read from the memory stream.

To do this in the context of your code, simple remove the line cs.FlushFinalBlock()...

记忆で 2025-01-02 10:53:08

问题是我作为 Base64 字符串传递的字符串无效,问题出在输入文件中,这使得解密

不可能

The problem is that the string I pass as Base64 string is invalid the problem is just in the input file which makes decryption impossible

Thanx all

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文