Re: RijndaelManaged problem...

From: Arun Moorthy[MS] (amoorthy@online.microsoft.com)
Date: 02/10/03


From: "Arun Moorthy[MS]" <amoorthy@online.microsoft.com>
Date: Mon, 10 Feb 2003 14:23:56 -0800


Pieter,

If you are using v1 of the .Net framework, this is a bug with the padding
mode that has since been fixed (fix available in Everett).

If you wish to pad with zeros, the temporary workaround is to do the padding
manually. Paddingmode.None should really throw an exception since the data
is not a multiple of the block size (which it does in Everett).

Hope this helps.
Thanks,
-Arun [MS]

"Pieter Philippaerts" <Pieter.nospam@mentalis.org> wrote in message
news:#jqlg6dzCHA.2592@TK2MSFTNGP10...
> I'm working with the RijndaelManaged class, and I'm experiencing a problem
> with it. I've written a small example [see below] that explains my
problem:
>
> <BEGIN CODE>
> using System;
> using System.Text;
> using System.Security.Cryptography;
>
> RijndaelManaged rijnman = new RijndaelManaged();
> rijnman.Padding = PaddingMode.PKCS7;
> byte[] toEncrypt = Encoding.ASCII.GetBytes("Hello World! This is a test");
> byte[] encrypted =
rijnman.CreateEncryptor().TransformFinalBlock(toEncrypt,
> 0, toEncrypt.Length);
> byte[] decrypted =
rijnman.CreateDecryptor().TransformFinalBlock(encrypted,
> 0, encrypted.Length);
> Console.WriteLine("Encrypting " + toEncrypt.Length + " bytes");
> Console.WriteLine("Encrypted output = " + encrypted.Length + " bytes");
> Console.WriteLine("Decrypted bytes = " +
> Encoding.ASCII.GetString(decrypted));
> <END CODE>
>
> If I run this code, I get the correct output:
> Encrypting 27 bytes
> Encrypted output = 32 bytes
> Decrypted bytes = Hello World! This is a test
>
> However, when I change the padding mode to PaddinMode.None or
> PaddingMode.Zeros, the output appears to be wrong:
> Encrypting 27 bytes
> Encrypted output = 16 bytes
> Decrypted bytes = Hello World! Thi
>
> Notice that the number of encrypted bytes is smaller than the number of
> unencrypted bytes [which is, of course, impossible].
> Does anyone know what's wrong with it? Why are the final bytes cut off?
>
> My first guess was that the ICryptoTransform returned by
> rijnman.CreateEncryptor() was unable to encrypt more than one block at
once
> if the PaddingMode was set to None or Zeros. However a call to
> CanTransformMultipleBlocks returns 'true', so it should be able to
transform
> multiple blocks at once.
>
> Regards,
> Pieter Philippaerts
> http://www.mentalis.org/
>
>