Re: RijndaelManaged problem...

From: Arun Moorthy[MS] (
Date: 02/10/03

From: "Arun Moorthy[MS]" <>
Date: Mon, 10 Feb 2003 14:23:56 -0800


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.
-Arun [MS]

"Pieter Philippaerts" <> wrote in message
> 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
> 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 =
> 0, toEncrypt.Length);
> byte[] decrypted =
> 0, encrypted.Length);
> Console.WriteLine("Encrypting " + toEncrypt.Length + " bytes");
> Console.WriteLine("Encrypted output = " + encrypted.Length + " bytes");
> Console.WriteLine("Decrypted bytes = " +
> Encoding.ASCII.GetString(decrypted));
> 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
> if the PaddingMode was set to None or Zeros. However a call to
> CanTransformMultipleBlocks returns 'true', so it should be able to
> multiple blocks at once.
> Regards,
> Pieter Philippaerts

Relevant Pages

  • gnupg / rsa padding question
    ... how large must the padding be to maintain security against a known ... specifically, with regard to gnupg/pgp messages done with a 4k rsa key, ... encrypting to the same 4096 rsa key ... Bob using the following gnupg command: ...
  • Re: Encryption ??
    ... What do you think about my idea to add pad handling ... although I was actually only encrypting data that fit into 8 ... It is mentioned for the padding type I used (Method 3 - nulls plus ... in test.prg adds a whole 8 character block when needed. ...
  • Re: Removing extra padding.
    ... While Encrypting a file the original data gets padded as per the ... I remove this extra padding in the output, ... in bits of the padded message becomes congruent to 448, modulo 512. ... This method can only be used to pad messages which are a whole number ...
  • Re: Interoperability problem in encrypting and decrypting files between 2K & XP
    ... is assuming that the cipher data size is the same as the original plaintext. ... Usually the ciphertext is longer because a padding has to be added by the ... If I am encrypting and decrypting on the same OS, ...