Reputation: 11975
There is a particular library (.net dll) we had used for cryptography. But it made use of Win32 routines. The issue here is we cannot use that for a 64-bit operating system. At least I think its so...we have code in the assembly written like this:
Friend Declare Function CryptAcquireContext Lib "advapi32.dll" Alias "CryptAcquireContextA" (ByRef phProv As Integer, ByVal pszContainer As String, ByVal pszProvider As String, ByVal dwProvType As Integer, ByVal dwFlags As Integer) As Integer
I see a 32 there; so I guess it is not meant for 64-bit machines. :(
Anyway, my ultimate goal is to convert that whole solution to a more portable code (so it can work on both 32 & 64 bit machines), but I can't seem to understand how to write the equivalent .net code. After a lot of googling I found a C++ implementation which is similar to how things were done in the old .net dll. That can be found here
Being a novice in this area it is quite difficult for me to understand the process. I suppose they use the RSA algorithm. And I guess this is a symmetric algorithm, because we pass only one key to do the encryption/decryption. If you need any other details please add a comment; I'll try to respond when I can...
Upvotes: 0
Views: 1104
Reputation: 3509
Unfortunately, the function you are calling is not a regular crypto function, its something used to get a cryptographic service provider(CSP)Generally, access to CSP's except via native code calls are unsupported in .net ≤ 4.0.
Absent your code,I can't figure out what you are doing with the service provider once you grab the handle to it. If its one of the standard symmetric or asymmetric functions or something similar, you can probably do it entirely from managed code.
Try looking at System.security.cryptography. There is also a Microsoft written set of wrappers around CNG/CSP data providers here that might directly expose expose the exact CSP and its functionality . Finally, if you want really really portable code, try searching for the bouncy castle library which would handle both win32, windows ce, win8, and mono.
Upvotes: 2
Reputation: 141638
That function should work fine on 64-bit windows if you change your pointers to be the same size at the platform using IntPtr
. Your pointers are fixed at 32-bits, which may (most likely) cause problems in a 64-bit environments. For example:
Friend Declare Function CryptAcquireContext Lib "advapi32.dll" Alias "CryptAcquireContextA" (ByRef phProv As IntPtr, ByVal pszContainer As String, ByVal pszProvider As String, ByVal dwProvType As Integer, ByVal dwFlags As Integer) As IntPtr
And that's it. The dwFlags
and dwProvType
stay Integer because they are 32-bit regardless of platform (DWORD). You will have to fix all platform invoke calls to use IntPtr
. For example, your call to CryptReleaseContext
should also take an IntPtr
as the first parameter rather than Integer.
Alternatively, you can change your application to always target the x86 platform instead of Any CPU and just let WOW64 handle it. Then, no code changes are needed.
If you can explain what you need this function for, there might be a more straight forward .NET implementation of what you are trying to do. For instance, if you are trying to work with x509 certificates, you may be more interested in the X509Certificate2
class.
Upvotes: 0
Reputation: 1151
advapi32.dll does exist on my win 7 x64 box so it should still work, otherwise how about System.Security does that not contain equivilent functionality?
Upvotes: 0
Reputation: 16007
For RSA there's the RSACryptoServiceProvider class, which is part of System.Security.Cryptography.
That will probably get you fairly portable on .NET (but .NET Compact Framework doesn't support all of this, unfortunately). If you want more portable, crypto++ is native C++.
Upvotes: 0