364,033 Members | 4781 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

DESCryptoServiceProvider - encryption question

Tom
P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


Gary Short
P: n/a
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
Nov 16 '05 #2

Tom
P: n/a
Tom
Gary,

this solved my problem - thank you very much for your help !!

Regards

Thomas

gary@computa.co.uk (Gary Short) wrote in message news:<1710107e.0503030233.ad062f2@posting.google.c om>...[color=blue]
> 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[/color]
Nov 16 '05 #3

Post your reply

Help answer this question



Didn't find the answer to your C# / C Sharp question?

You can also browse similar questions: C# / C Sharp