2014-04-04 30 views
0

我使用相同的初始化向量和相同的密钥进行加密和解密。不过,我收到错误说“填充无效,无法删除”在Web应用程序中,我正在加密数据并保存在sql server表列(nvarchar(max))内的加密数据。我有Windows服务,它读取加密的数据和解密。有人能告诉我我在哪里做错了。填充无效,无法删除。 Rjindaal加密

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 
} 

callling这样

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

Rijndael与256位块 - 有趣的选择。在.NET和PHP之外,它不是很好的支持。我想知道选择的背后是什么? – ntoskrnl

+0

其使用256位加密的项目要求 – Akie

+0

您似乎基本上忽略了实际上可能包含该错误的所有代码 - “Encrypt”函数,“Decrypt”函数或将数据写入/从数据库读取数据(或其组合),但没有给出任何这些方法的相关代码。 – Iridium

回答

0

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

  1. 将解密方法设置为不需要填充。

  2. 解密邮件。您将不会收到填充错误,因为您的 未检查它。

  3. 看看解密后的消息。如果是一路通过, 那么你的问题不是填充,而是加密或解密,通常是解密。检查你的密钥和IV是否为字节相同的 字节。如果信息没有问题,最后加上一些额外的 字符,然后检查这些额外字符是否与匹配PKCS7填充。

  4. 当你已经确诊的问题,你必须设置解密 方法回到PKCS7填充。

相关问题