Hi!
I'm having a problem calling CryptDecrypt from .Net.
Even thoug I call from inside a try catch block execution stops on the call with error "Exception of type 'System.ExecutionEngineException' was thrown." with no extra information after that..
Has anyone run into a problem like this before? - [DllImport("advapi32.dll", EntryPoint = "CryptAcquireContextW", CharSet = CharSet.Unicode, SetLastError = true)]
-
private static extern bool CryptAcquireContext(ref IntPtr hProv, string pszContainer, string pszProvider, uint dwProvType, uint dwFlags);
-
-
[DllImport("advapi32.dll", SetLastError = true)]
-
private static extern bool CryptReleaseContext(IntPtr hProv, uint dwFlags);
-
-
[DllImport("advapi32.dll", SetLastError = true)]
-
private static extern bool CryptDecrypt(IntPtr hKey, uint hHash, uint Final, uint dwFlags, [MarshalAs(UnmanagedType.LPWStr)]ref String pbData, ref uint pdwDataLen);
-
-
//Tried these as well..
-
//[DllImport("advapi32.dll", SetLastError = true)]
-
//private static extern bool CryptDecrypt(IntPtr hKey, uint hHash, uint Final, uint dwFlags, ref String pbData, ref uint pdwDataLen);
-
-
//[DllImport("advapi32.dll", SetLastError = true)]
-
//private static extern bool CryptDecrypt(IntPtr hKey, uint hHash, uint Final, uint dwFlags, ref byte[] pbData, ref uint pdwDataLen);
-
-
//[DllImport("advapi32.dll", SetLastError = true)]
-
//private static extern bool CryptDecrypt(IntPtr hKey, uint hHash, uint Final, uint dwFlags, IntPtr pbData, ref uint pdwDataLen);
-
-
-
[DllImport("advapi32.dll", SetLastError = true)]
-
private static extern bool CryptCreateHash(IntPtr hProv, uint Algid, IntPtr hKey, uint dwFlags, ref IntPtr phHash);
-
-
[DllImport("advapi32.dll", SetLastError = true)]
-
private static extern bool CryptHashData(IntPtr hHash, String pbData, uint dwDataLen, uint dwFlags);
-
-
[DllImport("advapi32.dll", SetLastError = true)]
-
private static extern bool CryptDestroyHash(IntPtr hHash);
-
-
[DllImport("advapi32.dll", SetLastError = true)]
-
private static extern bool CryptDeriveKey(IntPtr hProv, uint Algid, IntPtr hBaseData, uint dwFlags, ref IntPtr phKey);
-
-
[DllImport("advapi32.dll", SetLastError = true)]
-
private static extern bool CryptDestroyKey(IntPtr hKey);
- ...
-
-
if (!CryptAcquireContext(ref hCryptProv, "JustTesting", "Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)", 24, 0))
-
if (!CryptAcquireContext(ref hCryptProv, "JustTesting", "Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)", 24, 8))
-
throw new Exception("Error creating CSP handle");
-
-
if (!CryptCreateHash(hCryptProv, 32772, IntPtr.Zero, 0, ref hHash))
-
throw new Exception("Error creating hash.");
-
-
if (!CryptHashData(hHash, ls_key, 256, 0))
-
throw new Exception("Error creating hash.");
-
-
if (!CryptDeriveKey(hCryptProv, 26128, hHash, 0, ref hKey))
-
throw new Exception("Error creating key.");
-
-
if (!CryptDestroyHash(hHash))
-
throw new Exception("Error destroying hash.");
-
-
hHash = IntPtr.Zero;
-
-
string ls = "<encrypted string>"
-
uint length = (uint)ls.Length * 2; //Unicode
-
-
if (!CryptDecrypt(hKey, 0, 1, 0, ref ls, ref length))
-
throw new Exception("Unable to decrypt " + Marshal.GetLastWin32Error().ToString()); //never get here
-
-
//or here..
-
...
-
5 3805
Why are you doing all this through DLL imports instead of the native .NET cryptography services?
Its been implemented in an older application that it needs to be compatible with and I dont know how to translate the above into the .NET equalvalent
PRR 750
Expert 512MB
Try using
This is for catching unmanaged exceptions... if i am right...
Check for right data types .Also look into unsafe for use of pointers...
just tried using - //catch //Wants a try..
-
//{
-
//CryptDecrypt(lp_key, 0, 1, 0, ref ls_temp, ref lul_length)
-
//}
-
-
try
-
{
-
CryptDecrypt(lp_key, 0, 1, 0, ref ls_temp, ref lul_length)
-
}
-
catch { }
It gives the same error and does not catch.
Im pretty sure I've got the right datatypes there.. http://msdn.microsoft.com/en-us/libr...13(VS.85).aspx
BOOL WINAPI CryptDecrypt(
__in HCRYPTKEY hKey,
__in HCRYPTHASH hHash,
__in BOOL Final,
__in DWORD dwFlags,
__inout BYTE *pbData,
__inout DWORD *pdwDataLen
);
IntPtr for the first two, boolean for the second (Not reflected in post but was tried)
Such that..defining CryptDecrypt as: - [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
-
private static extern bool CryptDecrypt(IntPtr hKey, IntPtr hHash, bool Final, uint dwFlags, ref Byte[] pbData, ref uint pdwDataLen);
Still gives the same uncatchable error.
PRR 750
Expert 512MB
My last guess would be using unsafe tags for your code....
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Prasad |
last post by:
We are invoking a SQL DTS component (lets call it Comp1)
built by us in another component (Comp2).Comp1 was built
by creating the DTS package using the SQL DTS wizard and
then saving it as a VB...
|
by: michael |
last post by:
So I have a fairly large VB.Net application, lots of objects, lots of
yucky OLEDB code. One day I started converting some Singles to Decimals
because I wanted to make some calculations as precise...
|
by: John O'Neill |
last post by:
Hi
I have been having a strange problem with a c# application I have been
developing. My application detects when a Pocket PC device has been
connected (via ActiveSyc) and queries the registry...
|
by: Einar Værnes |
last post by:
Hi
I'm new to C#, and have just started to make a aspx-program.
The program runs fine until i try to include a two-dimensional array:
int myArray = new int {{1,2}, {3,4}, {5,6}, {7,8}};
The...
|
by: Tim Mulholland |
last post by:
I have an application that uses an external C(?) Dll and it seems like
after I make a specific call to that dll the application will crash at a
random call to the dll in the future. It sometimes...
|
by: John |
last post by:
Hi
I am getting this error when running a project;
An unhandled exception of type 'System.ExecutionEngineException' occurred in
system.dll
What can I do to fix this problem? Using vs.net...
|
by: NormD |
last post by:
An exception 'System.ExecutionEngineException' has occurred in servername
I have deployed a windows application on a server; I'm running the same on
my pc using...
|
by: Stephen Flynn |
last post by:
If I run my program on another machine I get the following error
An unhandled exception of type 'System.ExecutionEngineException' occurred in
System.Data.dll
This happens with I try to open an...
|
by: selvialagar |
last post by:
i've done a calibration tool. In that, there will be list of parameters and the value for the parameters are coming from the external device using TCP communication. The Screen will be refreshed once...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |