Click here to Skip to main content
15,040,743 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I'm programming a device (MPOS) in C/C++ and I need to send encrypted data via TCP/IP to a socket server written in C#.

Problem:
After encrypting the data (HelloWorld) and transmitting it to the server, I always get an exception with the message "Bad Data".

Encrypting on the device is straight forward:

C++
DoEncryption((char*)secBuffer,length,_3DES_ENCRYPTION,key);


From the C# side, I'm using the Generic Symmetric Algorithm Helper.
(Generic SymmetricAlgorithm Helper[^]).

C#
var keyString = "012345678901234567891234";
var key = Encoding.UTF8.GetBytes(keyString);
var sec = new Utilities.Crypto.SymmetricCryptography<TripleDESCryptoServiceProvider>(key, null);
//read encrypted data. i've saved it to a file.
var d1 = File.ReadAllBytes(@"c:\raw.txt");
var dec2 = sec.Decrypt(enc2); //always gives me "Bad Data"


For one thing, no IV is specified in the C encryption above. I modified the SymmetricCryptography's constructor a bit to auto generate the IV.

C#
public SymmetricCryptography(byte[] key, byte[] iv)
{
    _key = key;
    if (iv == null)
    {
         _provider.Key = key;
         _provider.GenerateIV();
         iv = _provider.IV;
    }
    _iv = iv;
}


How do I get this to work?

Regards,
Richard
Posted
Updated 23-Aug-10 8:12am
v2

1 solution

It looks as though the encrypted data from the device is invalid.
Perhaps try encrypting a small test string in C# and on the device and check that the results are the same.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900