473,395 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

C# Rijndael encryption. When decrypting I get junk data in the firstblock

Hi.

I've got a strange problem...

I've made a simple program to test encryption/decryption.
I use Rijndael encryption and here are the most important properties.

RijndaelManaged cipher = new RijndaelManaged();
cipher.KeySize = 256;
cipher.BlockSize = 256;
cipher.Padding = PaddingMode.ISO10126;
cipher.Mode = CipherMode.CBC;

I read the source from a file.

I have one constructor of my cipherwrapper class that takes no
parameters and generates a key with GenerateKey function of the
RijndaelManaged class. And one that takes a key as a parameter.

I create the class, and init the properties values. I choose a 32
bytes (256bits) key and run my application. If I encrypt and decrypt
in the same program execution all is good. (Reading input file,
decrypting it and writing it back. Then decrypting the decrypted file
and writing it to file again).

BUT if I first encrypt in one program execution and then run the
program again to decrypt the first block (32 bytes) is junk... I also
(sometimes) get the error message that the padding is invalid and
cannot be removed.

The second strange thing is that when I manage to decrypt it with the
first block corrupted it manages to decrypt the rest of the file...

Has anyone had problems with this?
I most certainly can't be dependent of encrypting and decrypting in
the same program execution. I must encrypt a file, send it to a
customer where the customer must decrypt it again with the same key...

Can anyone help PLEASE...

Thanx,
Fritjolf
Dec 11 '07 #1
4 5632
Are you setting the IV?

Perhaps look at the MSDN example:
http://msdn2.microsoft.com/en-us/lib...elmanaged.aspx

Marc
Dec 11 '07 #2
On Tue, 11 Dec 2007 01:41:37 -0800 (PST), Fritjolf
<Mo*************@edb.comwrote:
>Hi.

I've got a strange problem...

I've made a simple program to test encryption/decryption.
I use Rijndael encryption and here are the most important properties.

RijndaelManaged cipher = new RijndaelManaged();
cipher.KeySize = 256;
cipher.BlockSize = 256;
cipher.Padding = PaddingMode.ISO10126;
cipher.Mode = CipherMode.CBC;

I read the source from a file.

I have one constructor of my cipherwrapper class that takes no
parameters and generates a key with GenerateKey function of the
RijndaelManaged class. And one that takes a key as a parameter.

I create the class, and init the properties values. I choose a 32
bytes (256bits) key and run my application. If I encrypt and decrypt
in the same program execution all is good. (Reading input file,
decrypting it and writing it back. Then decrypting the decrypted file
and writing it to file again).

BUT if I first encrypt in one program execution and then run the
program again to decrypt the first block (32 bytes) is junk... I also
(sometimes) get the error message that the padding is invalid and
cannot be removed.

The second strange thing is that when I manage to decrypt it with the
first block corrupted it manages to decrypt the rest of the file...

Has anyone had problems with this?
I most certainly can't be dependent of encrypting and decrypting in
the same program execution. I must encrypt a file, send it to a
customer where the customer must decrypt it again with the same key...

Can anyone help PLEASE...

Thanx,
Fritjolf
You are using CBC mode
(http://en.wikipedia.org/wiki/Block_c..._of_operation), which
requires an Initialisation Vector (IV). You do not appear to be
setting an IV in your code so I suspect that the system is setting up
a random IV for you. Hence the IV is the same when you use the same
run of the program and different if you use different runs.

CBC mode can recover from corrupted blocks, just losing the block
where the corruption occurs. By using a different IV for encryption
and decryption you are effectively corrupting the first block of the
message. This explains why the rest of your message decrypts
correctly after the garbled first block.

You need to either explicitly set the same IV for both encryption and
decryption, or you can use the default IV for encryption and copy it
to wherever you want decryption to run. There is no need to keep the
IV secret.

rossum

Dec 11 '07 #3
Hi and thank you very much for your quick response.

No, I didn't set the IV.
Why? I see that the IC must (can) be generated in the encryption
procedure and then that IV must be passed to the decryption procedure.
Then another problem is raised. In addition to sending the encryption
key to a customer, must I also send the IV ???

Another input is that if I have a file smaller than the blocksize
(lets say the file is 6 bytes), I get an error message trying to
decrypt the message telling me that "Padding is invalid and cannot be
removed". I can't understand why...

I will try and set the IV and see what happens...

Thanx,
Fritjolf
On Dec 11, 10:54 am, Marc Gravell <marc.grav...@gmail.comwrote:
Are you setting the IV?

Perhaps look at the MSDN example:http://msdn2.microsoft.com/en-us/lib...y.cryptography...

Marc
Dec 11 '07 #4
Marc and Rossum, thank you!

Now it all works like a charm. IV is configured and used - the IV is
made public in a bin file.
And I also tried with another Mode. It then worked without IV.
Also thanx for the explanation about the first corrupted block.

Thanx,
Fritjolf.


On Dec 11, 11:22 am, Fritjolf <Morten.Jacob...@edb.comwrote:
Hi and thank you very much for your quick response.

No, I didn't set the IV.
Why? I see that the IC must (can) be generated in the encryption
procedure and then that IV must be passed to the decryption procedure.
Then another problem is raised. In addition to sending the encryption
key to a customer, must I also send the IV ???

Another input is that if I have a file smaller than the blocksize
(lets say the file is 6 bytes), I get an error message trying to
decrypt the message telling me that "Padding is invalid and cannot be
removed". I can't understand why...

I will try and set the IV and see what happens...

Thanx,
Fritjolf

On Dec 11, 10:54 am, Marc Gravell <marc.grav...@gmail.comwrote:
Are you setting the IV?
Perhaps look at the MSDN example:http://msdn2.microsoft.com/en-us/lib...y.cryptography...
Marc- Hide quoted text -

- Show quoted text -
Dec 11 '07 #5

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

Similar topics

10
by: joshsackett | last post by:
I am starting an encryption project for my database and I'm performing some tests on decryption speed. A lot of my application queries use a LIKE parameter in the WHERE clause. To keep from...
2
by: Dave Bailey | last post by:
I have developed a web app using DPAPI to encrypt a connection string in the web.config file. The application works perfectly on the development machine but when deployed to the server when...
113
by: Bonj | last post by:
I was in need of an encryption algorithm to the following requirements: 1) Must be capable of encrypting strings to a byte array, and decyrpting back again to the same string 2) Must have the same...
4
by: Mantorok | last post by:
Hi I have a couple of encryption methods but when I call decrypt I get the string back but with a load \0 escape characters on the end? Any idea why? It is actually causing problems in some...
5
by: ~~~ .NET Ed ~~~ | last post by:
Anybody has any idea why this simple thing is not working? I pass a text file as input to encrypt it, then pass the encrypted version to the same function and get some garbled data not at all...
11
by: John Williams | last post by:
I've written a simple program to do XOR encryption as my first foray into understanding how encryption works. The code compiles fine, however it segmentation faults on every run. using gdb to...
0
by: Vayse | last post by:
I need to encrypt some strings in serveral programs I have. So I grabbed some code from the MS help. I wrote an app that would help me generate the encrypted strings. Its s form with 4 text boxes...
4
by: Sylvie | last post by:
http://www.obviex.com/samples/Encryption.aspx According to this link, I am using Rijndael Encryption & Decryption Algorithms, But I want my encrypted strings just CAPS string and just...
3
by: KBS Developer | last post by:
Hi, I can encrypt without any problem but while decrypting I got junk. I've read the other thread about getting junk but that is not my case. Here is the sample code: private Rijndael...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...

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.