By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,534 Members | 1,830 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,534 IT Pros & Developers. It's quick & easy.

DESCryptoServiceProvider - encryption question

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


P: n/a
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

P: n/a
Tom
Gary,

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

Regards

Thomas

ga**@computa.co.uk (Gary Short) wrote in message news:<17*************************@posting.google.c om>...
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 #3

This discussion thread is closed

Replies have been disabled for this discussion.