Is there a limit to my key length passed to encoding using ASCIIEncoding in .NET 4.0?

I am using C # 4.0 Windows Application.I want to provide an encryption key to protect my file with System.Text.ASCIIEncoding.So, here my encryption key is SARAVANAN and the key length is 9.

String enKey="SARAVANAN"; DESCryptoServiceProvider des = new DESCryptoServiceProvider(); des.Key = ASCIIEncoding.ASCII.GetBytes(enKey); 

he threw an exception like "The specified key is not a valid size for this algorithm." But it works well only with a length of 8. So I would like to know if there are any restrictions on this length due to this ASCII ENCODING ...

Please help me out of this problem?

+7
source share
3 answers

64 bits is the only valid key size for the DES encryption algorithm.

ASCII encoding uses 8 bits per character, so 8 ASCII characters == 64 bits.

+11
source

It has nothing to do with ASCIIEncoding . This is due to DESCryptoServiceProvider . From the documentation :

This algorithm supports a key length of 64 bits.

In other words, the key you are passing must be 64 bits or 8 bytes. How you get these 8 bytes is up to you, but the form of the ASCII encoding with a 9-ASCII letter will have 9 bytes (72 bits).

+6
source

If you look at MSDN , you can see This algorithm supports a key length of 64 bits . Thus, the only valid key length is 8 characters.

+1
source

All Articles