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



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
.



Relevant Pages

  • RE: NTE_BAD_DATA
    ... They are NOT used DIRECTLY to encrypt / decrypt data; ... you should generate a RANDOM SESSION KEY and select a SYMMETRIC ENCRYPTION ... // imported from a BLOB read in from the source file or having ...
    (microsoft.public.platformsdk.security)
  • Re: Back Doors
    ... >> Design into the system a master key. ... Encrypt that with public key. ... Decrypt random symmetric key with private key. ...
    (sci.crypt)
  • Re: CAPI and RC4: can not decrypt when Final parameter is set to F
    ... to store ASYMMETRIC key pairs - never symmetric keys like RC4, ... Now when you need to encrypt at one place and decrypt at the other normally ... Get a HCRYPTPROV handle to a key container with CryptAcquireContext ...
    (microsoft.public.platformsdk.security)
  • Re: RSA - Public vs. Private Keys
    ... This is a common pattern for license software ... your client will send a unique machine hash to the ... will let us decrypt with a Public Key (or simply not ... |> RSA is intended to encrypt messages with public keys only. ...
    (microsoft.public.dotnet.security)
  • Re: .NET Crypto Classes Interoperability with Win32 Crypto APIs
    ... when i encrypt a string using .NET classes and try to ... > decrypt it using Win32 APIs, ... > UnicodeEncoding(); ...
    (microsoft.public.dotnet.security)