I'm trying to send an encrypted string in a UDP packet from my C++ application, and recieve and decrypt it in my C# application. The encryption code in the C++ application works fine, I have no problem encrypting and decrypting it in my C++ app. However, I have no idea if I can or how to decrypt the data in my C# app using the crypo classes C# has. So I decided to try making a DLL in C++ with the encrypt/decrypt functions, and try to import and use it in my C# app. However, when I try to call the functions, the app crashes for me.

Here is the import code I'm using:
Code:
        // import EncryptData()
        [DllImport("Rjindael.dll", CharSet = CharSet.Auto, EntryPoint = "EncryptData")]
        public static extern void EncryptData( string key, string input, string result, int n, int mode );

        // import DecryptData()
        [DllImport("Rjindael.dll", CharSet = CharSet.Auto, EntryPoint = "DecryptData")]
        public static extern void DecryptData( string key, string input, string result, int n, int mode);
I know the functions are being exporter properly, I checked with PE explorer. I'm also using a module definition file to export the function names correctly.

My testing function:
Code:
        private void button1_Click(object sender, EventArgs e)
        {
            string final_key = "12345678901234567890123456789012"; // 32 bytes
            string src = "asd 123"; // input

            string encrypted = ""; // output
            try
            {
                EncryptData(final_key, src, encrypted, src.Length, 1);
                txtLog.AppendText(encrypted);
            }
            catch (Exception ex) { txtLog.AppendText("Error: " + ex.ToString() + "\n"); }

        }
I don't know much about encryption or how to import C++ code in to C#.

The C++ functions I'm exporting are:
EncryptData:
Code:
RJINDAEL_API void EncryptData( char const* key, char const* input, char const* result, size_t n, int mode )
{
	try {
		// initialize the crypto key
		CRijndael crypt;
		crypt.MakeKey(key, CRijndael::sm_chain0, 32, 32);

		char szDataIn [ENCRYPTION_BUFFERSZ  ];
		char szDataOut[ENCRYPTION_BUFFERSZ+1];

		// validate the mode
		if( mode < 0 || mode > 2 )
			mode = CRijndael::CBC;

		// copy data into the buffer
		strncpy( szDataIn, input, sizeof( szDataIn ) );

		// clear the output buffer
		memset( szDataOut, 0, sizeof(szDataOut) );

		// encrypt the data
		crypt.Encrypt( szDataIn, szDataOut, ENCRYPTION_BUFFERSZ-1, mode );

		// copy data to the output buffer
		strncpy( (char*)result, szDataOut, n );
	}
	catch( exception& roEx)
	{
		result = NULL;
		return;
	}
}
DecryptData:
Code:
RJINDAEL_API void DecryptData( char const* key, char const* input, char const* result, size_t n, int mode )
{
	try {
		// initialize the crypto key
		CRijndael crypt;
		crypt.MakeKey(key, CRijndael::sm_chain0, 32, 32);

		char szDataIn [ENCRYPTION_BUFFERSZ  ];
		char szDataOut[ENCRYPTION_BUFFERSZ+1];

		// validate the mode
		if( mode < 0 || mode > 2 )
			mode = CRijndael::CBC;

		// copy data into the buffer
		strncpy( szDataIn, input, sizeof( szDataIn ) );

		// clear the output buffer
		memset( szDataOut, 0, sizeof(szDataOut) );

		// decrypt the data
		crypt.ResetChain();
		crypt.Decrypt( szDataIn, szDataOut, ENCRYPTION_BUFFERSZ-1, mode );

		// copy data to the output buffer
		strncpy( (char*)result, szDataOut, n );
	}
	catch( exception& roEx)
	{
		result = NULL;
		return;
	}
}
The dll code can be found in the following links:
Rjindael.cpp:
http://pastebin.com/vQ1z2Dnf

Rjindael.h:
http://pastebin.com/2nRtLErH

The functions I'm exporting / trying to import are at the bottom of the Rjindael.cpp. Also, the class isn't mine, I found it on the web and modified it a bit. I don't remember the link or I'd just post it instead. Sorry. :\



So, what am I doing wrong? What am I missing? How do I make the C# app use the DLL properly?


Thanks in advance!