DES Decryption Bad Data error  
Author Message
John Perkins





PostPosted: .NET Base Class Library, DES Decryption Bad Data error Top

Hi,

I am working on a TCP server program which receives data via a socket from another system. The encrypted data trapped by a sniffer program is the same as is generated on the originating system using a DES CBC enrypt mode.

The c# decryption routine, which gives a bad data exception at the “string val = sr.ReadToEnd();”

Statement, is as follows –

private string DecryptString(byte[] Value, int strlen)

{

//ICryptoTransform ct;

//MemoryStream ms;

//CryptoStream cs;

string outstr;

byte[] byt;

byte[] IV = { 0x12, 0x34, 0xf3, 0x91, 0x54, 0x12, 0x94, 0x00 };

byte[] Key = { 0xfe, 0x55, 0x12, 0x65, 0xc3, 0x56, 0x99, 0x91 };

DES des = new DESCryptoServiceProvider();

des.Mode = CipherMode.CBC;

des.Key = Key;

des.IV = IV;

//mCSP.

ICryptoTransform transform = des.CreateDecryptor(des.Key, des.IV);

byt = (Value);

byte[] encdata = new byte[strlen];

for (int i = 0; i < strlen; i++)

encdataIdea = bytIdea;

MemoryStream ms = new MemoryStream(encdata);

ms.Write(encdata, 0, encdata.Length);

CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Read);

// Create a StreamReader for reading the stream.

StreamReader sr = new StreamReader(cs);

// Read the stream as a string.

string val = sr.ReadToEnd();

outstr = val;

return outstr;

}

If you can give me any clues as to where I am going wrong, it would be much appreciated.

Regards,

John Perkins



.NET Development35  
 
 
John Perkins





PostPosted: .NET Base Class Library, DES Decryption Bad Data error Top

Hi all,

Problem solved. I had to specify a padding mode type ( and decrypted the byte array without converting to a stream).

Thanks,

John