Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 481
  • Last Modified:

Rijndael BlockSize vs GetBytes?

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);
0
pointeman
Asked:
pointeman
  • 2
  • 2
2 Solutions
 
mlanCommented:
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.

0
 
pointemanAuthor Commented:
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.
[IV]
aes.IV = keyGen.GetBytes(aes.BlockSize / 8);  // BlockSize(default 128) / 8 = 16
- OR -
aes.IV = keyGen.GetBytes(16);
[Key]
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....
0
 
mlanCommented:
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 ;-)
0
 
pointemanAuthor Commented:
Thanks :)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now