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);
}
Rijndael與256位塊 - 有趣的選擇。在.NET和PHP之外,它不是很好的支持。我想知道選擇的背後是什麼? – ntoskrnl
其使用256位加密的項目要求 – Akie
您似乎基本上忽略了實際上可能包含該錯誤的所有代碼 - 「Encrypt」函數,「Decrypt」函數或將數據寫入/從數據庫讀取數據(或其組合),但沒有給出任何這些方法的相關代碼。 – Iridium