I am using C# 4.0 Windows Application.I want to give encryption key to protect my file using System.Text.ASCIIEncoding.So, Here my encryption key is SARAVANAN and key length of this is 9.
String enKey="SARAVANAN";
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key = ASCIIEncoding.ASCII.GetBytes(enKey);
it raised the exception like "Specified key is not a valid size for this algorithm".But it is working good at length of 8 only.So,i would like to know is there any restriction to this length due to this ASCII ENCODING...
Please guide me get out of this issue?
64 bits is the only valid key size for the DES encryption algorithm.
ASCII encoding uses 8 bits per character, therefore 8 ASCII characters == 64 bits.
It's got nothing to do with ASCIIEncoding
. It's to do with DESCryptoServiceProvider
. From the documentation:
This algorithm supports a key length of 64 bits.
In other words, the key you pass in should be 64 bits, or 8 bytes. How you get those 8 bytes is up to you - but the ASCII-encoded form of a 9-ASCII-letter string is going to be 9 bytes (72 bits).
If you take a look at MSDN you can see This algorithm supports a key length of 64 bits
. So the only valid length for a key is 8 characters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With