Does setting Aes KeySize generate a new key?

52 Views Asked by At

According to Microsoft's documentation on System.Security.Cryptography.Aes, the key size can simply be changed by setting the KeySize property.

Does changing the key size automatically generate a new random key of the new key size? The documentation is not clear.

1

There are 1 best solutions below

0
On

Yes, or no, depending.

Setting KeySize makes the object forget the key. If a new one is provided by setting Key no new key was technically generated. But when the object has no key, but needs one, a new key (of size KeySize) is created and remembered.

In general, you should either set KeySize (to make a random one) or Key (which adjusts KeySizeValue), not both.