繁体   English   中英

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

[英]Rijndael encrypted text causes length of data to decrypt is invalid error - C#

我已经在网上搜索,但无法找到解决我问题的任何方法。

我正在使用以前编写的方法使用Rijndael类对文本进行加密和加密。

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

加密/解密工作正常,但是偶尔我会收到此错误:

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

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

这是加密/解密功能。 IV和密钥定义为只读字符串。

    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类?

我发现使用Rijndael的网站托管SeeSharp

TekEye

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

删除您已评论的部分:“获取输出并修剪'\\ 0'字节”,并将return ...语句替换为:

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

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

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

如果您需要证明AES是Rijndael: http//en.wikipedia.org/wiki/Advanced_Encryption_Standard

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM