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)). I have windows service which reads encrypted data and decrypt. 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.
Set the decryption method to expect no padding.
Decrypt a message. You will not get the padding error, since you aren't checking it.
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. 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.
When you have diagnosed the problem, you must set the decryption method back to PKCS7 padding.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.