views:

253

answers:

2

I need to create a Windows Mobile Application (WinMo 6.x - C#) which is used to encrypt/decrypt files. However it is my duty to write the encryption algorithm which is AES-128 along with XTS as the mode of operation. RijndaelManaged just doesn't cut it :( Very much slower than DES and 3DES CryptoServiceProviders :O

I know it all depends on how good I am at writing the algorithm in the most efficient way. (And yes I my self have to write it from scratch but i can take a look @ other implementations)

Nevertheless, does writing a C++ .NET DLL to create the encryption/decryption algorithm + all the file handling and using it from C# have a significant performance advantage OVER writing the encryption algorithm + file handling in completely managed C# code?

If I use C++ .NET to create the encryption algorithm, should I use MFC Smart Device DLL or ATL? What is the difference and is there any impact on which one I choose? And can i just add a reference to the C++ DLL from C# or should I use P/Invoke?

I am fairly competent with C# than C++ but performance plays a major role as I have convinced my lecturers that AES is a very efficient cryptographic algorithm for resource constrained devices.

Thanx a bunch :)

+1  A: 

Writing a "managed" program will have equal performance in C++ or C# or VB, since they all compile to IL anyway.

I don't know, but if you write an unmanaged C++ class library and invoke it from managed C# app you may loose some performance during the p/invoke but your speed increase (from going unmanaged) may be enough to justify it. There's an equal chance the p/invoke might cancel it out any potential performance gain from going unmanaged.

I don't think there's anyway to know for sure without doing it both ways and testing.

Nate Bross
Thanx :) So even though i write in C++ (MFC or ATL) it still gets converted to IL? Is there anyway that i can write a native C++ DLL for the encryption + file handling which can be used from C#?
Ranhiru Cooray
@Ranhiru: Yes, but you have to P/Invoke across the managed/unmanaged code boundry.
Billy ONeal
So a non-native C++ .DLL (MFC or ATL) can be just added as a reference as it can be done for a C# DLL? And a Native C++ DLL has to be used by P/Invoke.. Am i correct? What kind of project should i use to write a native C++ DLL for smart devices? Sorry if this is confusing :(
Ranhiru Cooray
+1  A: 

Actually, your managed code will be JIT compiled at the first run and then cached by the operating system, so there is no point in worrying about it.
Also, no matter which language you choose, as long as it is .NET, it will compile to CIL and will require JIT compilation.
If you write plain managed code in C++, that may be faster, but not necessarily.

P/Invoke can slow down your code, as Nate said.

You could either write the entire application in managed code or native code, but if all the algorythms are the same, the only performance difference will be in the (first) startup time. And that is usually negligible. (The CLR is optimized to do JIT compilation very-VERY fast.)

Side note: during JIT compilation, the CLR will optimize your code. Sometimes the JITted code runs faster than plain native code, simply because the JIT compiler optimizes better.

Venemo