简体   繁体   English

填充无效,无法删除。 Rjindaal加密

[英]Padding is invalid and cannot be removed. Rjindaal encryption

I am using same initialization vector and same key for encryption and decryption. 我使用相同的初始化向量和相同的密钥进行加密和解密。 However I am getting error saying "Padding is invalid and cannot be removed" In web application I am encrypting data and saving encrypted data inside sql server table column (nvarchar(max)). 但是,我收到错误消息,说“填充无效且无法删除”,在Web应用程序中,我正在加密数据并将加密的数据保存在sql server表列(nvarchar(max))中。 I have windows service which reads encrypted data and decrypt. 我有Windows服务,可读取加密数据并解密。 Can someone please tell me where am I doing wrong. 有人可以告诉我我在哪里做错了。

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();

    }

calling like this 这样叫

    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)

decryption method 解密方法

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

callling like this 像这样

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);
        }

The "Padding is invalid" message can mean many different things. “填充无效”消息可能意味着很多不同的含义。 It might be a problem with just the padding, or it might be a problem with the whole encryption including the padding. 仅填充可能是一个问题,或者包括填充在内的整个加密都可能是一个问题。 You can take some steps to diagnose the problem. 您可以采取一些步骤来诊断问题。

  1. Set the decryption method to expect no padding. 将解密方法设置为不希望填充。

  2. Decrypt a message. 解密消息。 You will not get the padding error, since you aren't checking it. 您将不会得到填充错误,因为您没有检查它。

  3. Look at the decrypted message. 查看解密的消息。 If it is junk all the way through, then your problem is not padding but either encryption or decryption, usually decryption. 如果它一直都是垃圾,那么您的问题不是填充,而是加密或解密,通常是解密。 Check that your key and IV are byte for byte the same. 检查您的密钥和IV是否字节对字节相同。 If the message is fine, with some extra characters at the end, then check that those extra characters match what you would expect from PKCS7 padding. 如果消息很好,但末尾还有一些额外的字符,请检查这些额外的字符是否与您从PKCS7填充中期望的字符匹配。

  4. When you have diagnosed the problem, you must set the decryption method back to PKCS7 padding. 诊断出问题后, 必须将解密方法设置回PKCS7填充。

暂无
暂无

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

相关问题 不断收到“CryptographicException: Padding is invalid and cannot be removed”。 即使有填充,AES CBC 加密也会出错 - Keep getting “CryptographicException : Padding is invalid and cannot be removed.” errors with AES CBC encryption even with padding “填充无效,无法删除。”出现错误 - “Padding is invalid and cannot be removed.” error with a twist 使用Crypto-JS和C#解密的AES加密-避免“填充无效且无法删除。” - AES encryption with Crypto-JS and C# Decryption - avoiding “Padding is invalid and cannot be removed.” TRIPLEDES 填充无效且无法删除。 c#解密 - TRIPLEDES padding is invalid and cannot be removed. c# decrypt c# AES 解密错误并显示“填充无效且无法删除”。 - c# AES decryption errors with “Padding is invalid and cannot be removed.” 解密AES给我“填充无效,无法删除。”错误 - Decrypting AES gives me “Padding is invalid and cannot be removed.” error Rijndael加密| 填充无效,无法删除 - Rijndael Encryption | Padding is invalid and cannot be removed AES加密错误:填充无效,无法删除 - AES Encryption Error: Padding is invalid and cannot be removed C#:AES错误:填充无效,无法删除。 相同的钥匙和一切,帮助 - C#: AES error: Padding is invalid and cannot be removed. Same key and everything, help 填充无效,无法删除。 c#解密AES Rijndael,AES托管 - Padding is invalid and cannot be removed. c# decrypt AES Rijndael, AES Managed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM