dotnet / dotnet/dotnet-api-docs
Cryptography.SymmetricAlgorithm.Key doesn't mention that it resets KeySize property
- Dominant language
- C#
- Stars
- 949
- Forks
- 1.7k
- Avg merge
- 3d 27m
- Merged PRs (30d)
- 49
Description
In the documentation here: https://docs.microsoft.com/en-us/dotnet/api/system.security.cryptography.symmetricalgorithm.key?view=netframework-4.8
It should mention that if you set the KeySize property and then set the Key property to a key with a different key size, it changes the KeySize property. This led to some confusion when I was setting 256 as the key size but then accidentally specified a shorter key. I would expect an exception to be thrown when I try to run the encryption/decryption because the key does not match the key size... but no exception is thrown and it turns out it just changes the KeySize property itself, which is not documented anywhere.
Example:
```
Dim EncryptorKey(15) As Byte '128 bits long
Dim Encryptor As Aes = System.Security.Cryptography.Aes.Create
Encryptor.KeySize = 256 'specify that we want to use 256 bit key length
Debug.WriteLine(Encryptor.KeySize) 'gives an output of 256 as expected
Encryptor.Key = EncryptorKey 'set to a key that is 128 bits long
Debug.WriteLine(Encryptor.KeySize) 'gives an output of 128, even though we set it to 256 earlier
```
Contributor guide
Assessment
This issue has not been assessed yet.