CryptoStream.Read Method error when decrypting

G

Guest

Hi,
I am developing a ActiveX Control which will be used in a web page.
The control will encrypt some value then decrypt it when the web page opens
next time.
I tested the control in a windows application and it works fine and no error
jumps.
However, when I tried to use it in a web page, problems came.
There is no problem for encrypting but the decrypting can't be finished.
The error jumped from the line below

csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);

and the error message reads as below:
PKCS7 padding is invalid and cannot be removed.

I tested again in the windows application but I couldn't trigger the error.
Does any one know why this CryptoStream.Read Method behaves differently
in a web browser?


Please see code details below.

protected string DecryptInk(byte[] key,byte[] IV,String Ink64String)
{
ASCIIEncoding textConverter= new ASCIIEncoding();
RijndaelManaged myRijndael=new RijndaelManaged();
byte[] fromEncrypt;
byte[] encrypted;
string decryptedStr;

//Get a decryptor that uses the same key and IV as the encryptor.
ICryptoTransform decryptor = myRijndael.CreateDecryptor(key, IV);

encrypted=Convert.FromBase64String(Ink64String);

MemoryStream msDecrypt = new MemoryStream(encrypted);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor,
CryptoStreamMode.Read);

fromEncrypt = new byte[encrypted.Length];
try
{
//problem is here!!
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
}
catch (System.Exception exp)
{
MessageBox.Show(exp.Message,exp.Source,MessageBoxButtons.OK);
return String.Empty;
}
decryptedStr=textConverter.GetString(fromEncrypt);

///remove \0 or null reference from the end of the string.
decryptedStr=decryptedStr.TrimEnd(Char.Parse("\0"));
//return the decrypted string.
return decryptedStr;
}
 
G

Guest

I'm not sure if you are having these problems, but I have solved a similar
problem that has been troubling me all afternoon....

If you are converting bytes to text using an ASCIIEncoder, or any other
8-bit encoder, it will only convert using 8-bits, i.e. a byte value of 129
will become 1!!

I had this problem because the MSDN sample uses an ASCII encoder! How
annoying!!!!! I hope this helps.

James said:
I found the problem.
I passed wrong key and IV which caused an error.


James said:
Hi,
I am developing a ActiveX Control which will be used in a web page.
The control will encrypt some value then decrypt it when the web page opens
next time.
I tested the control in a windows application and it works fine and no error
jumps.
However, when I tried to use it in a web page, problems came.
There is no problem for encrypting but the decrypting can't be finished.
The error jumped from the line below

csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);

and the error message reads as below:
PKCS7 padding is invalid and cannot be removed.

I tested again in the windows application but I couldn't trigger the error.
Does any one know why this CryptoStream.Read Method behaves differently
in a web browser?


Please see code details below.

protected string DecryptInk(byte[] key,byte[] IV,String Ink64String)
{
ASCIIEncoding textConverter= new ASCIIEncoding();
RijndaelManaged myRijndael=new RijndaelManaged();
byte[] fromEncrypt;
byte[] encrypted;
string decryptedStr;

//Get a decryptor that uses the same key and IV as the encryptor.
ICryptoTransform decryptor = myRijndael.CreateDecryptor(key, IV);

encrypted=Convert.FromBase64String(Ink64String);

MemoryStream msDecrypt = new MemoryStream(encrypted);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor,
CryptoStreamMode.Read);

fromEncrypt = new byte[encrypted.Length];
try
{
//problem is here!!
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
}
catch (System.Exception exp)
{
MessageBox.Show(exp.Message,exp.Source,MessageBoxButtons.OK);
return String.Empty;
}
decryptedStr=textConverter.GetString(fromEncrypt);

///remove \0 or null reference from the end of the string.
decryptedStr=decryptedStr.TrimEnd(Char.Parse("\0"));
//return the decrypted string.
return decryptedStr;
}
 
G

Guest

I'm not sure if you are having these problems, but I have solved a similar
problem that has been troubling me all afternoon....

If you are converting bytes to text using an ASCIIEncoder, or any other
8-bit encoder, it will only convert using 8-bits, i.e. a byte value of 129
will become 1!!

I had this problem because the MSDN sample uses an ASCII encoder! How
annoying!!!!! I hope this helps.

James said:
I found the problem.
I passed wrong key and IV which caused an error.


James said:
Hi,
I am developing a ActiveX Control which will be used in a web page.
The control will encrypt some value then decrypt it when the web page opens
next time.
I tested the control in a windows application and it works fine and no error
jumps.
However, when I tried to use it in a web page, problems came.
There is no problem for encrypting but the decrypting can't be finished.
The error jumped from the line below

csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);

and the error message reads as below:
PKCS7 padding is invalid and cannot be removed.

I tested again in the windows application but I couldn't trigger the error.
Does any one know why this CryptoStream.Read Method behaves differently
in a web browser?


Please see code details below.

protected string DecryptInk(byte[] key,byte[] IV,String Ink64String)
{
ASCIIEncoding textConverter= new ASCIIEncoding();
RijndaelManaged myRijndael=new RijndaelManaged();
byte[] fromEncrypt;
byte[] encrypted;
string decryptedStr;

//Get a decryptor that uses the same key and IV as the encryptor.
ICryptoTransform decryptor = myRijndael.CreateDecryptor(key, IV);

encrypted=Convert.FromBase64String(Ink64String);

MemoryStream msDecrypt = new MemoryStream(encrypted);
CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor,
CryptoStreamMode.Read);

fromEncrypt = new byte[encrypted.Length];
try
{
//problem is here!!
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
}
catch (System.Exception exp)
{
MessageBox.Show(exp.Message,exp.Source,MessageBoxButtons.OK);
return String.Empty;
}
decryptedStr=textConverter.GetString(fromEncrypt);

///remove \0 or null reference from the end of the string.
decryptedStr=decryptedStr.TrimEnd(Char.Parse("\0"));
//return the decrypted string.
return decryptedStr;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top