Thursday, 3 October 2013

Padding is invalid and cannot be removed in C# licensing method

Padding is invalid and cannot be removed in C# licensing method

in my project I'm Doing a licensing method where after the user enters the
license key his product will be activated. Im using the following code but
I'm getting an exception thrown saying "Padding is invalid and cannot be
removed". The following is my code
public void ValidateProductKey()
{
RSACryptoServiceProvider _cryptoService = new
RSACryptoServiceProvider();
string productKey = "G7MA4Z5VR5R3LG001AS1N5HA3YHX05";
byte[] keyBytes =
Base32Converter.FromBase32String(productKey);
//Base32Converter is my customized method which returns byte
of values;
byte[] signBytes = new byte[2];
byte[] hiddenBytes = new byte[16];
using (MemoryStream stream = new MemoryStream(keyBytes))
{
stream.Read(hiddenBytes, 0, 8);
stream.Read(signBytes, 0, 2);
stream.Read(hiddenBytes, 8, hiddenBytes.Length - 8);
keyBytes = stream.ToArray();
}
byte[] sign = _cryptoService.SignData(signBytes, new
SHA1CryptoServiceProvider());
byte[] rkey = new byte[32];
byte[] rjiv = new byte[16];
Array.Copy(sign, rkey, 32);
Array.Copy(sign, 32, rjiv, 0, 16);
SymmetricAlgorithm algorithm = new RijndaelManaged();
try
{
hiddenData = algorithm.CreateDecryptor(rkey,
rjiv).TransformFinalBlock(hiddenBytes,0,hiddenBytes.Length);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
When reaching the "hiddenData" variable I get an exception thrown as
"Padding is invalid and cannot be removed". Any help would be really
appreciated.

No comments:

Post a Comment