【问题标题】:aes decryption not working properly sometimeaes 解密有时无法正常工作
【发布时间】:2015-07-08 06:27:39
【问题描述】:

我正在使用 aes 对文本进行加密/解密,但有时它会在解密后给我准确的值,而有时我会出错。我提到了不同的答案,但没有找到问题的根本原因。

private static string DecryptStringFromBytes(byte[] cipherText, byte[] key, byte[] iv)
    {
        // Declare the string used to hold the decrypted text.  
        string plaintext = null;

        // Create an RijndaelManaged object  
        // with the specified key and IV.  
        using (var rijAlg = new System.Security.Cryptography.RijndaelManaged())
        {
            //Settings  
            rijAlg.Mode = System.Security.Cryptography.CipherMode.CBC;
            rijAlg.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
            rijAlg.FeedbackSize = 128;

            rijAlg.Key = key;
            rijAlg.IV = iv;

            // Create a decrytor to perform the stream transform.  
            var decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

            try
            {
                // Create the streams used for decryption.  
                using (var msDecrypt = new System.IO.MemoryStream(cipherText))
                {
                    using (var csDecrypt = new System.Security.Cryptography.CryptoStream(msDecrypt, decryptor, System.Security.Cryptography.CryptoStreamMode.Read))
                    {

                        using (var srDecrypt = new System.IO.StreamReader(csDecrypt))
                        {
                            // Read the decrypted bytes from the decrypting stream  
                            // and place them in a string.  
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }
            }
            catch
            {
                plaintext = "keyError";
            }
        }
        return plaintext;
    }

它抛出错误“填充无效并且无法删除” 我看到了一些建议,比如删除填充,但它似乎不是正确的解决方案。 我无法找到这背后的原因,因为有时它运行完美而不会抛出错误。

非常感谢任何帮助或建议。

用于加密 - 在 js 中对客户端进行加密并将加密的文本传递给服务器。

var key = CryptoJS.enc.Utf8.parse("16 digit number here");
            var iv = CryptoJS.enc.Utf8.parse("16 digit number here");
            var EncryptedString = CryptoJS.AES.encrypt(CryptoJS.enc.Utf8.parse("entered string to encrypt"), key,
             { keySize: 128 / 8, iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 });

【问题讨论】:

  • 这个函数对我来说看起来还不错,所以问题很可能出在你没有显示的 encryption 函数中。请编辑您的问题以包含它。
  • 我添加了在客户端完成的加密部分。我们在客户端进行加密并在服务器端解密吗?@Iridium

标签: c# .net encryption cryptography aes


【解决方案1】:

通过在.NET 中使用与您提供的解密函数类似的加密例程,我能够成功地将明文往返于密文并返回到明文,因此看来解密函数本身是可以的。因此,您用于加密的密钥和/或 IV 很可能与您在解密时使用的值不匹配。

鉴于您的加密代码使用字符串值的 UTF-8 编码版本来形成密钥和 IV,因此值得在您的解密代码中执行相同操作(使用 Encoding.UTF8.GetBytes())。

然而,值得注意的是,虽然这可能会解决眼前的问题,但在没有某种形式的密钥派生过程(例如Rfc2898DeriveBytes)和 IV 的情况下直接将字符串值用于密钥本身是一种不好的做法应该为加密函数的每个应用随机生成。这些只是您使用密码学的一些问题(并且与代码是否有效无关)。

【讨论】:

  • 感谢您的建议,我使用的是 UTF-8 编码版本。同时解密 alos 。此外,对于 Key anf IV,我分配了相同的值(随机 16 位),正如您所提到的,以下结构也适用于我,但是在多次运行时,它几次抛出我上面提到的错误。 @铱
  • @user1260967 密文是如何在加解密函数之间传递的?您可能会在流程中的某处损坏数据。
  • 我在自己的代码中发现了问题,我错误地使用了不同的密钥进行加密和解密。谢谢你的帮助!
猜你喜欢
  • 1970-01-01
  • 2019-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-31
  • 1970-01-01
  • 2021-05-16
  • 1970-01-01
相关资源
最近更新 更多