Rijndael BlockSize vs GetBytes?

Posted on 2009-12-28
Last Modified: 2012-06-27
What's the difference between using BlockSize or GetBytes?

            var keyGen = new Rfc2898DeriveBytes(pass, 8)        
            var aes = Rijndael.Create(); //Rijndael
            aes.Padding = PaddingMode.PKCS7;
            aes.IV = keyGen.GetBytes(aes.BlockSize / 8);
            aes.Key = keyGen.GetBytes(aes.KeySize / 8);

  - VS -
            aes.IV = keyGen.GetBytes(16);
            aes.Key = keyGen.GetBytes(32);
Question by:pointeman
    LVL 1

    Accepted Solution

    In the Microsoft Framework 2.0 implementation of Rijndael the BlockSize and KeySize can be 256, 128 or 64 bits.
    The default values are KeySize=256 and BlockSize=128.
    You can override the default sizes by setting the BlockSize of KeySize properties.

    By reading the BlockSize and KeySize properties you will be ensured of the correct sizes used for the encryption.

    In your case the GetBytes(16) and GetBytes(32) will work fine, but only because they are based the default values.


    Author Comment

    Okay, so BlockSize is measured in bits and / 8 converts to needed bytes for GetBytes. I fine most postings on the Internet using BlockSize. It gets a little confusing when I find two ways of returning the same result.
    aes.IV = keyGen.GetBytes(aes.BlockSize / 8);  // BlockSize(default 128) / 8 = 16
    - OR -
    aes.IV = keyGen.GetBytes(16);
    aes.Key = keyGen.GetBytes(aes.KeySize / 8); // BlockSize(default 256) / 8 = 32
    - OR -
    aes.Key = keyGen.GetBytes(32);

    Note: I experimented by encrypting a string message using aes.KeySize/8 and successfully decrypted using GetBytes(32)...
    I wasn't sure if there is an advantage....
    LVL 1

    Assisted Solution

    The advantage of using KeySize / 8 and BlockSize / 8 is that you always get the correct byte size.
    What if, hypothetically, the default bit size changes? Or when you set the BlockSize of KeySize yourself to a different length?

    I would suggest to always use the KeySize / 8 and BlockSize / 8 to determine the length in bytes.

    "Better safe than sorry gives no worry"  is my motto ;-)

    Author Closing Comment

    Thanks :)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
    Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now