473,785 Members | 2,300 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 14014
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
3023
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
1957
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
9647
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
9489
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
10357
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10162
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...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7509
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
6744
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
5396
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.