|
-
Jun 6th, 2011, 01:55 PM
#1
Thread Starter
New Member
Having problems with encryption..
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!
-
Jun 6th, 2011, 02:33 PM
#2
PowerPoster
Re: Having problems with encryption..
when you say "it crashes" - what error do you get?
why dont you do all the encryption/decryption in C#? There are plenty of encryption classes in .NET. Reading the API docs on MSDN or simply googling for the algo you want will present you with results
-
Jun 6th, 2011, 03:19 PM
#3
Thread Starter
New Member
Re: Having problems with encryption..
Hmm, I modified the function a bit so it returns the data instead.
C# code:
Code:
// import EncryptData()
[DllImport("Rjindael.dll", CharSet = CharSet.Auto, EntryPoint = "EncryptData")]
public static extern string EncryptData( string key, string input, int mode );
// import DecryptData()
[DllImport("Rjindael.dll", CharSet = CharSet.Auto, EntryPoint = "DecryptData")]
public static extern string DecryptData(string key, string input, int mode);
Code:
private void button1_Click(object sender, EventArgs e)
{
string final_key = "12345678901234567890123456789012"; // 32 bytes
string src = "asd 123"; // input
try
{
string encrypted = EncryptData(final_key, src, 1);
txtLog.AppendText(encrypted);
}
catch (Exception ex) { txtLog.AppendText("Error: " + ex.ToString() + "\n"); }
}
DLL code:
Code:
RJINDAEL_API char* EncryptData( char const* key, char const* input, 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 );
return (char*)szDataOut;
}
catch( exception& roEx)
{
// return NULL if something happens
return NULL;
}
}
RJINDAEL_API char* DecryptData( char const* key, char const* input, 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 );
return (char*)szDataOut;
}
catch( exception& roEx)
{
// return NULL if something happens
return NULL;
}
}
Now it gives me the following error in the log window:
Code:
Error: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at njka_testing.frmMain.EncryptData(String key, String input, Int32 mode)
at njka_testing.frmMain.button1_Click(Object sender, EventArgs e) in C:\Users\bored\Documents\Visual Studio 2010\Projects\njka_testing\njka_testing\Form1.cs:line 484
I tried changing the return data type to byte[] instead of string, but got this error:
Code:
Error: System.Runtime.InteropServices.MarshalDirectiveException: Cannot marshal 'return value': Invalid managed/unmanaged type combination.
Am I using a wrong datatype somewhere? What am I doing wrong now? ...
-
Jun 6th, 2011, 03:22 PM
#4
PowerPoster
Re: Having problems with encryption..
problem maybe in your encryption/decryption code in C++
here is the MSDN doc FULL with code on encryption/decryption in C#:
http://msdn.microsoft.com/en-us/libr...elmanaged.aspx
-
Jun 6th, 2011, 03:42 PM
#5
Thread Starter
New Member
Re: Having problems with encryption..
The encryption code works fine. I encrypted a string, displayed it in hex, and decrypted it successfully. Will I be able to decrypt the data using the Rijndael Managed class you linked me? All the examples and other stuff I found while googling it show that I need an IV key, and the class I've been using in C++ doesn't mention an IV anywhere.
Is it possible to use that RijndaelManaged class in C++ as well?
-
Jun 6th, 2011, 04:04 PM
#6
PowerPoster
Re: Having problems with encryption..
yes it is but it would be managed code and use .NET Framework. what difference would it make if you used the .NET Framework and develop in either C++ or C#? it wouldnt really as you would be using the .NET Libraries.
have you tried using the example and forgetting about the idea of the fact that there is an IV key?
http://msdn.microsoft.com/en-us/libr....rijndael.aspx
it uses the Generated IV key so you dont need to worry about creating your own and storing it somewhere
-
Jun 6th, 2011, 06:18 PM
#7
Re: Having problems with encryption..
It's been a while since I had to look at a crypto implementation, but I think the IV is what the C++ class calls the "chain"? (passed in to the MakeKey method)
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|