473,797 Members | 3,079 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CryptoStream.Re ad Method error when decrypting

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.Len gth);

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.Re ad 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.
ICryptoTransfor m decryptor = myRijndael.Crea teDecryptor(key , IV);

encrypted=Conve rt.FromBase64St ring(Ink64Strin g);

MemoryStream msDecrypt = new MemoryStream(en crypted);
CryptoStream csDecrypt = new CryptoStream(ms Decrypt, decryptor,
CryptoStreamMod e.Read);

fromEncrypt = new byte[encrypted.Lengt h];
try
{
//problem is here!!
csDecrypt.Read( fromEncrypt, 0, fromEncrypt.Len gth);
}
catch (System.Excepti on exp)
{
MessageBox.Show (exp.Message,ex p.Source,Messag eBoxButtons.OK) ;
return String.Empty;
}
decryptedStr=te xtConverter.Get String(fromEncr ypt);

///remove \0 or null reference from the end of the string.
decryptedStr=de cryptedStr.Trim End(Char.Parse( "\0"));
//return the decrypted string.
return decryptedStr;
}
Nov 17 '05 #1
3 14017
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.Len gth);

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.Re ad 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.
ICryptoTransfor m decryptor = myRijndael.Crea teDecryptor(key , IV);

encrypted=Conve rt.FromBase64St ring(Ink64Strin g);

MemoryStream msDecrypt = new MemoryStream(en crypted);
CryptoStream csDecrypt = new CryptoStream(ms Decrypt, decryptor,
CryptoStreamMod e.Read);

fromEncrypt = new byte[encrypted.Lengt h];
try
{
//problem is here!!
csDecrypt.Read( fromEncrypt, 0, fromEncrypt.Len gth);
}
catch (System.Excepti on exp)
{
MessageBox.Show (exp.Message,ex p.Source,Messag eBoxButtons.OK) ;
return String.Empty;
}
decryptedStr=te xtConverter.Get String(fromEncr ypt);

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

Nov 17 '05 #2
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.Len gth);

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.Re ad 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.
ICryptoTransfor m decryptor = myRijndael.Crea teDecryptor(key , IV);

encrypted=Conve rt.FromBase64St ring(Ink64Strin g);

MemoryStream msDecrypt = new MemoryStream(en crypted);
CryptoStream csDecrypt = new CryptoStream(ms Decrypt, decryptor,
CryptoStreamMod e.Read);

fromEncrypt = new byte[encrypted.Lengt h];
try
{
//problem is here!!
csDecrypt.Read( fromEncrypt, 0, fromEncrypt.Len gth);
}
catch (System.Excepti on exp)
{
MessageBox.Show (exp.Message,ex p.Source,Messag eBoxButtons.OK) ;
return String.Empty;
}
decryptedStr=te xtConverter.Get String(fromEncr ypt);

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

Nov 17 '05 #3
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.Len gth);

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.Re ad 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.
ICryptoTransfor m decryptor = myRijndael.Crea teDecryptor(key , IV);

encrypted=Conve rt.FromBase64St ring(Ink64Strin g);

MemoryStream msDecrypt = new MemoryStream(en crypted);
CryptoStream csDecrypt = new CryptoStream(ms Decrypt, decryptor,
CryptoStreamMod e.Read);

fromEncrypt = new byte[encrypted.Lengt h];
try
{
//problem is here!!
csDecrypt.Read( fromEncrypt, 0, fromEncrypt.Len gth);
}
catch (System.Excepti on exp)
{
MessageBox.Show (exp.Message,ex p.Source,Messag eBoxButtons.OK) ;
return String.Empty;
}
decryptedStr=te xtConverter.Get String(fromEncr ypt);

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

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
9567
by: Stingray | last post by:
Are there any know problems with using a MemoryStream as a backing store for a CryptoStream? I'm trying to simply encrypt and decrypt text in memory. I'd like to create some simple methods to encrypt text before writing to a database and decrypt it when reading it back. I figured I'd use a MemoryStream that I can either store as a blob or convert to a string and store as a varchar. Anyway, I can't get the CryptoStream to write to the...
8
3025
by: MattP | last post by:
Ok, with the help of some examples found on the web and some minor modifications on our own, we have a simple and working encrypt and decrypt solution. It runs as a service, watches for files with a specific extension in a specific directory. The files are uploaded by FTP to this directory. The service then does the following steps: 1) Verify it can open the file (so we know it's fully uploaded). 2) Try to decrypt the file with known...
1
4785
by: Casey Watson | last post by:
Hi :) I'm having some major trouble with an XML Client/Server application that I am writing. I am using NetworkStream with CryptoStream to Read and Write XML between computers. Now, whenever I write the XML, the Server section which uses the XMLTextReader Class will not accept EndElements period, and neither the ReadStartElement or ReadEndElement will work properly. However, if I only send to WriteStartElements over the network, the...
5
12927
by: weixiang | last post by:
Hi, I want to use DES and CryptoStream to serialize a encrypted stream to a file with a header "CRYPT". And I wrote these code: To store: FileStream fileStream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None); byte signture = AE.GetBytes("CRYPT");
0
1222
by: Daniel Weber | last post by:
Hi! In my code I create a CryptoStream around another stream of my own, with CryptoStreamMode.Read. So I just can read from the CryptoStream. When I close that CryptoStream, however, the underlying stream's Write() method is called, with an empty buffer (offset and count-parameters are also 0). My streams' CanWrite property returns false, so the CryptoStream should not call Write(). It shouldn't call Write() at all, because I created it...
7
2771
by: pseudonym | last post by:
To keep this short, assume the function working_test() works perfectly (because it does), while failing_test() is not. My GetCodec function returns a Stream (CryptoStream) object wrapped around the Stream it is passed. private function working_test() { string f = "c:\\test.dat"; StreamWriter w; StreamReader r;
4
4584
by: Burke Atilla | last post by:
While encrypting data with DES through CryptoStream makes encrypted data bigger than original string. if we have 8 byte key and 8 byte of data then the mode is ECB. output encrypted data is 16 bytes long. first 8 bytes is out encrypted key but last 8 byte unknown. and while decrypting if we couldn't supply this 8 bytes we couldnt decrypt data. and get exception "Bad Data" What is this 8 bytes and how can i supply this data if i have only the...
9
1958
by: TC | last post by:
Hey All, I posted this to the Crypto users group and forgot to add the VB.Net users group. I apologize for any confusion. I have been testing a try / catch / finally block and purposely raising exceptions and I've noticed that if an exception of "Length of the data to decrypt is invalid." is raised with the CryptoStream object, later this exception will get raised a second time and thrown to the caller when trying to close the stream...
2
4134
by: Anil Gupte/iCinema.com | last post by:
I am getting the following message and no, it is not even going to that statement twice. Why is this happening? System.NotSupportedException was caught Message="FlushFinalBlock() method was called twice on a CryptoStream. It can only be called once." Source="mscorlib" StackTrace: at System.Security.Cryptography.CryptoStream.FlushFinalBlock()
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9536
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10245
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10205
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7559
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6802
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4131
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3748
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.