PKCS7 padding is invalid and cannot be removed.

  • Thread starter Thread starter Random
  • Start date Start date
R

Random

I'm creating a simple RinjdaelManaged class for some simple
encryption/decryption. I am not using a password to generate the Key and
IV, but am just hard-coding those values into the class. Yes, I know, but I
intend to build on it later for better security.

Problem right now is in the decryption, I am getting a "PKCS7 padding is
invalid and cannot be removed." error. What is the reason for this?

Public Class MySecurity
Private Shared KEY_128() As Byte = {--byte array--}
Private Shared IV_128() As Byte = {--byte array--}
Public cryptoObj As New RijndaelManaged


Public Shared Function Encrypt(ByVal value As String) As String
If value <> "" Then
Dim ms As MemoryStream = New MemoryStream
Dim cs As CryptoStream = New CryptoStream(ms, cryptoObj
..CreateEncryptor(KEY_128, IV_128), CryptoStreamMode.Write)
Dim sw As StreamWriter = New StreamWriter(cs)

sw.Write(value)
sw.Flush()
cs.FlushFinalBlock()
ms.Flush()

'convert back to a string
Return Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length)
Else
Return ""
End If
End Function

Public Shared Function Decrypt(ByVal value As String) As String
If value <> "" Then
'convert from string to byte array
Dim buffer As Byte() = Convert.FromBase64String(value)
Dim ms As MemoryStream = New MemoryStream(buffer)
Dim cs As CryptoStream = New CryptoStream(ms,
cryptoObj.CreateDecryptor(KEY_128, IV_128), CryptoStreamMode.Read)
Dim sr As StreamReader = New StreamReader(cs)

Return sr.ReadToEnd()
Else
Return ""
End If
End Function

End Class
 
I tried your code (after converting to C#) and it worked fine. How are you
calling the methods? Here is the test program that works for me:

private static System.Security.Cryptography.RijndaelManaged cryptObj = new
RijndaelManaged();
private static byte[] KEY_128 = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6};
private static byte[] IV_128 = {1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6};

[STAThread]
static void Main(string[] args)
{
string pt = "The big brown fox jumped over the moon";
Console.WriteLine(pt);
string ct = Encrypt(pt);
Console.WriteLine(ct);
string pt2 = Decrypt(ct);
Console.WriteLine(pt2);
}

public static string Encrypt(string PlainText)
{
if (PlainText != string.Empty)
{
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, cryptObj.CreateEncryptor(KEY_128,
IV_128), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(cs);
sw.Write(PlainText);
sw.Flush();
cs.FlushFinalBlock();
ms.Flush();
return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
}
else
return string.Empty;
}

public static string Decrypt(string CipherText)
{
if (CipherText != string.Empty)
{
byte [] buf = Convert.FromBase64String(CipherText);
MemoryStream ms = new MemoryStream(buf);
CryptoStream cs = new CryptoStream(ms, cryptObj.CreateDecryptor(KEY_128,
IV_128), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cs);
return sr.ReadToEnd();
}
else
return String.Empty;
}


(Sorry, I don't know VB.Net too much ...)

Sujit D'Mello
 
Back
Top