By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,168 Members | 1,564 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,168 IT Pros & Developers. It's quick & easy.

CryptoStream.Read Method error when decrypting

P: n/a
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,MessageBoxB uttons.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;
}
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
I found the problem.
I passed wrong key and IV which caused an error.
"James" wrote:
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,MessageBoxB uttons.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;
}

Nov 17 '05 #2

P: n/a
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" wrote:
I found the problem.
I passed wrong key and IV which caused an error.
"James" wrote:
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,MessageBoxB uttons.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;
}

Nov 17 '05 #3

P: n/a
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" wrote:
I found the problem.
I passed wrong key and IV which caused an error.
"James" wrote:
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,MessageBoxB uttons.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;
}

Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.