DESCryptoServiceProvider - encryption question

T

Tom

Hi experts,

I have the following code, which works fine:

######################### C# snippet
########################################
string k = "12345678";
string input = "ABCDEFGH";

DES des_dec = new DESCryptoServiceProvider();
byte [] inbuff = ASCIIEncoding.ASCII.GetBytes(input);
byte [] bytesKey = Encoding.ASCII.GetBytes(k);
des_dec.Mode = CipherMode.ECB;
des_dec.Key = bytesKey;

MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des_dec.CreateEncryptor(),
CryptoStreamMode.Write);
cs.Write(inbuff, 0, inbuff.Length);
cs.Close();
byte [] encryptedData = ms.ToArray();
######################### end C# snippet
####################################

After executing the snippet, the byte array encryptedData contains 16
Bytes - although I just gave 8 bytes with inbuff. WHY ????

I am working on a class to communicate with an external device, which
en- and decrypts data using DES ECB standard (the simplest one).

The problem is: Encrypting by the same key and standards like showed
in the code snippet just generates an 8 byte encrypted array. This
result (generated by firmware of device which is programmed in ANSI-C)
can not be decrypted using the DESCryptoServiceProvider, it always
returns an exception with the meassage "Invalid Data".

Is there anything I did wrong ? Why does DESCryptoServiceProvider
generate a 16 byte array out of a 8 byte array input ??

Please help.

Thanks to all - greets from germany

Tom
 
G

Gary Short

Hello Tom,

The extra 8 bytes are basically padding. Use the line,

des_dec.Padding = PaddingMode.None

to fix the problem, though realise that this is very insecure.

HTH,
Gary Short
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top