Re: C# Rijndael encryption. When decrypting I get junk data in the first block



On Tue, 11 Dec 2007 01:41:37 -0800 (PST), Fritjolf
<Morten.Jacobsen@xxxxxxx> wrote:

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_cipher_modes_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

.



Relevant Pages

  • How do I Use DPAPI to Encrypt and Decrypt Data (C#/VB.NET)?
    ... Use DPAPI to Encrypt and Decrypt Data ... The code below demonstrates how to call Data Protection API (DPAPI) ... In addition to encryption and decryption, ... public static string Encrypt ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: Byte array to string and back - newbie question
    ... // Create a symmetric algorithm. ... This is done to make encryption more ... // Encrypt a string into a string using a password ... // Decrypt a byte array into a byte array using a key and an IV ...
    (microsoft.public.dotnet.framework.aspnet.security)
  • Re: CryptAPI(encryption/decryption)
    ... It seems like you're missing the Base64 decode step when trying to decrypt ... I misspelled the Private Key as Primary Key. ... Is there any variation in the encryption format in openssl compared to ... "Dylan DSilva " wrote: ...
    (microsoft.public.pocketpc.developer)
  • Re: Writing spaghetti code for obfuscation/encryption
    ... > undocumented opcodes and simple encryption? ... do the protection and distribution (I'll justify this later, ... This key was used to decrypt the main program code. ... So here's the scheme in action: Put disk in, ...
    (comp.lang.asm.x86)
  • Re: Which is more secure RC2 or RC4 ?
    ... Credit card info... ... If your application can decrypt the information (in order to send it ... one goofy solution may be to use public key encryption on ... decryption key on a separate computer (laptop). ...
    (sci.crypt)