Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any restriction of my Key Length passed to encoding using ASCIIEncoding in .NET 4.0?

Tags:

c#

.net

.net-4.0

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?

like image 252
Saravanan Avatar asked Dec 12 '11 08:12

Saravanan


3 Answers

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.

like image 195
Roy Dictus Avatar answered Nov 17 '22 19:11

Roy Dictus


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).

like image 20
Jon Skeet Avatar answered Nov 17 '22 19:11

Jon Skeet


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.

like image 1
Fischermaen Avatar answered Nov 17 '22 19:11

Fischermaen