首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >填充无效,无法移除。Rjindaal加密

填充无效,无法移除。Rjindaal加密
EN

Stack Overflow用户
提问于 2014-04-04 11:02:27
回答 1查看 870关注 0票数 0

我使用相同的初始化向量和相同的密钥进行加密和解密。但是,我在web应用程序中说“填充无效且不能删除”时出错,我在sql服务器表列(nvarchar(max))中加密数据和保存加密数据。我有窗口服务,读取加密数据和解密。有人能告诉我我哪里做错了吗。

代码语言:javascript
复制
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();

    }

像这样打电话

代码语言:javascript
复制
    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)

解密法

代码语言:javascript
复制
public string Decrypt(byte[] cipherText, string key, byte[] initialisationVector, int blockSizeInBits)
{
  //hidden logic
    rijndaelManaged.Mode = CipherMode.CBC;
    rijndaelManaged.Padding = PaddingMode.PKCS7;
    //hidden logic
}

像这样叫

代码语言:javascript
复制
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);
        }
EN

回答 1

Stack Overflow用户

发布于 2014-04-05 17:52:25

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

  1. 将解密方法设置为不需要填充。
  2. 解密消息。您将不会得到填充错误,因为您没有检查它。
  3. 看看解密的消息。如果整个过程都是垃圾,那么您的问题不是填充,而是加密或解密,通常是解密。检查您的键和IV的字节是否相同。如果消息没有问题,结尾有一些额外的字符,那么检查这些额外的字符是否符合您对PKCS7填充的期望。
  4. 诊断出问题后,必须将解密方法设置为PKCS7填充。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22860985

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档