Rijndael加密文本导致数据解密长度无效错误 - C#

时间:2015-05-19 18:22:45

标签: c# exception encryption cryptography

我在网上搜索但未能找到解决问题的方法。

我使用以前编写的方法使用Rijndael类加密和生成文本。

我使用这些功能来加密和解密我一直在处理的Web应用程序的用户名和电子邮件。

加密/解密工作完美,但每隔一段时间我就会收到此错误:

System.Security.Cryptography.CryptographicException: Length of the data to decrypt is invalid.

目前,我使用特定的电子邮件地址收到此错误,即使我更换了电子邮件中的某些字母,也无法重现错误。

以下是加密/解密功能。 IV和Key被定义为只读字符串。

    static public string Encrypting(string Source)
{
    byte[] bytIn = System.Text.ASCIIEncoding.ASCII.GetBytes(Source);
    // create a MemoryStream so that the process can be done without I/O files
    System.IO.MemoryStream ms = new System.IO.MemoryStream();

    byte[] IVBytes = Encoding.ASCII.GetBytes(IV);
    byte[] KEYBytes = Encoding.ASCII.GetBytes(KEY);

    Rijndael rijndael = Rijndael.Create();
    rijndael.IV = IVBytes;
    rijndael.Key = KEYBytes;

    // create Crypto Stream that transforms a stream using the encryption
    CryptoStream cs = new CryptoStream(ms, rijndael.CreateEncryptor(), CryptoStreamMode.Write);

    // write out encrypted content into MemoryStream
    cs.Write(bytIn, 0, bytIn.Length);
    cs.FlushFinalBlock();

    // get the output and trim the '\0' bytes
    byte[] bytOut = ms.GetBuffer();
    int i = 0;
    for (i = 0; i < bytOut.Length; i++)
        if (bytOut[i] == 0)
            break;

    // convert into Base64 so that the result can be used in xml
    return System.Convert.ToBase64String(bytOut, 0, i);
}

static public string Decrypting(string Source)
{
    // convert from Base64 to binary
    byte[] bytIn = System.Convert.FromBase64String(Source);
    // create a MemoryStream with the input
    System.IO.MemoryStream ms = new System.IO.MemoryStream(bytIn, 0, bytIn.Length);

    byte[] IVBytes = Encoding.ASCII.GetBytes(IV);
    byte[] KEYBytes = Encoding.ASCII.GetBytes(KEY);

    Rijndael rijndael = Rijndael.Create();
    rijndael.IV = IVBytes;
    rijndael.Key = KEYBytes;

    // create Crypto Stream that transforms a stream using the decryption
    CryptoStream cs = new CryptoStream(ms, rijndael.CreateDecryptor(), CryptoStreamMode.Read);

    // read out the result from the Crypto Stream
    System.IO.StreamReader sr = new System.IO.StreamReader(cs);
    return sr.ReadToEnd();
}

仅供参考 - 我对密码学和安全性一无所知。

可以修复这些功能以避免导致错误的特殊情况,还是应该废弃这些功能并使用RijndaelManaged类?

我发现使用RijndaelManaged的网站: SeeSharp

TekEye

2 个答案:

答案 0 :(得分:1)

问题几乎肯定与RijndaelRijndaelManaged(或任何其他此类实施)无关,而是因为加密数据包含0x00,而且您输入错误假设密文在第一个0x00字节结束。由于密文可以合法地包含任何字节值,因此您应该使用流的Length属性来确定密文的长度。

删除您评论过的部分:&#34;获取输出并修剪&#39; \ 0&#39;字节&#34;并将return ...语句替换为:

return System.Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length);

应该注意的是,您在此处使用加密技术还存在许多其他问题,例如:使用直接从字符串的ASCII编码生成的密钥,以及您使用固定IV的事实都会对安全性产生负面影响。

答案 1 :(得分:0)

错误的规范是填充问题。您使用的是什么版本的.NET?使用AES类(AES或高级加密标准,即Rijndael)更为常见。您可以找到许多AES实现作为样本。

如果你需要一些证据AES是Rijndael:http://en.wikipedia.org/wiki/Advanced_Encryption_Standard