【问题标题】:Padding is invalid and cannot be removed. Rjindaal encryption填充无效且无法移除。 Rjindaal 加密
【发布时间】:2014-05-16 15:50:31
【问题描述】:

我使用相同的初始化向量和相同的密钥进行加密和解密。但是,我收到错误消息,提示“填充无效且无法删除” 在 Web 应用程序中,我正在加密数据并将加密数据保存在 sql server 表列 (nvarchar(max)) 中。我有读取加密数据和解密的 Windows 服务。有人可以告诉我我在哪里做错了。

public byte[] Encrypt(string clearText, string key, byte[] initialisationVector, int blockSizeInBits)
{//hidden logic
    rijndaelManaged.Mode = CipherMode.CBC;
    rijndaelManaged.Padding = PaddingMode.PKCS7;
//hidden logic
        return memoryStream.ToArray();

    }

这样打电话

    Dim encryptionKey As String = ConfigurationManager.AppSettings("Key")
    ' Arrange - need 32 byte IV for 256-bit
    Dim cryptographer3 As ICryptographer = New Cryptographer()
    Dim initialisationVector3 As Byte() = {&H26, &HDC, &HFF, &H0, &HAD, &HED, _
        &H7A, &HEE, &HC5, &HFE, &H7, &HAF, _
        &H4D, &H8, &H22, &H3C, &H26, &HDC, _
        &HFF, &H0, &HAD, &HED, &H7A, &HEE, _
        &HC5, &HFE, &H7, &HAF, &H4D, &H8, _
        &H22, &H3C}

    ' Act
    Dim encryptedString As Byte() = cryptographer3.Encrypt(strForEncryption, encryptionKey, initialisationVector3, 256)
    'Dim decrypt3 As String = cryptographer3.Decrypt(encryptedString, Key, initialisationVector3, 256)
    Return System.Text.Encoding.Unicode.GetString(encryptedString)

解密方法

public string Decrypt(byte[] cipherText, string key, byte[] initialisationVector, int blockSizeInBits)
{
  //hidden logic
    rijndaelManaged.Mode = CipherMode.CBC;
    rijndaelManaged.Padding = PaddingMode.PKCS7;
    //hidden logic
}

这样叫

if (encryptedIdentificationValue.Trim().Length > 0)
        {
            string decryptionKey = ConfigurationManager.AppSettings["Key"];
            // Arrange - need 32 byte IV for 256-bit
            ICryptographer cryptographer3 = new Cryptographer();
            byte[] initialisationVector3 =
                {
                    0x26, 0xdc, 0xff, 0x0, 0xad, 0xed,
                    0x7a, 0xee, 0xc5, 0xfe, 0x7, 0xaf,
                    0x4d, 0x8, 0x22, 0x3c, 0x26, 0xdc,
                    0xff, 0x0, 0xad, 0xed, 0x7a, 0xee,
                    0xc5, 0xfe, 0x7, 0xaf, 0x4d, 0x8,
                    0x22, 0x3c
                };

            return cryptographer3.Decrypt(encryptedIdentificationValue, decryptionKey, initialisationVector3, 256);
        }

【问题讨论】:

  • 具有 256 位块的 Rijndael - 有趣的选择。它在 .NET 和 PHP 之外没有得到很好的支持。我想知道选择背后的原因是什么?
  • 项目要求使用256位加密
  • 您似乎基本上遗漏了所有实际上可能包含错误的代码 - Encrypt 函数、Decrypt 函数或写入/读取数据时可能有问题数据库中的数据(或它们的某种组合),但没有给出任何这些方法的相关代码。
  • @AshifNataliya 您似乎将块大小与密钥大小混淆了。即使您使用的是 256 位密钥,标准 AES 始终使用 128 位块(和 IV)。 Rijndael 确实支持可调整的块大小,但在标准化为 AES 之前已被删除。对 256 位块的支持很少。

标签: c# asp.net sql-server encryption cryptography


【解决方案1】:

“填充无效”消息可能意味着许多不同的东西。这可能只是填充的问题,也可能是包括填充在内的整个加密的问题。您可以采取一些步骤来诊断问题。

  1. 将解密方法设置为无填充。

  2. 解密消息。你不会得到填充错误,因为你 不检查。

  3. 查看解密的消息。如果一路都是垃圾, 那么您的问题不是填充,而是加密或 解密,通常是解密。检查您的密钥和 IV 是否 字节对字节相同。如果消息很好,还有一些额外的 最后的字符,然后检查那些额外的字符是否匹配 您对PKCS7 padding 的期望。

  4. 当您诊断出问题后,您必须设置解密 方法返回到 PKCS7 填充。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 2011-12-25
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多