473,396 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

CryptDecrypt 'System.ExecutionEngineException'

115 100+
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?

Expand|Select|Wrap|Line Numbers
  1. [DllImport("advapi32.dll", EntryPoint = "CryptAcquireContextW", CharSet = CharSet.Unicode, SetLastError = true)]
  2. private static extern bool CryptAcquireContext(ref IntPtr hProv, string pszContainer, string pszProvider, uint dwProvType, uint dwFlags);
  3.  
  4. [DllImport("advapi32.dll", SetLastError = true)]
  5. private static extern bool CryptReleaseContext(IntPtr hProv, uint dwFlags);
  6.  
  7. [DllImport("advapi32.dll", SetLastError = true)]
  8. private static extern bool CryptDecrypt(IntPtr hKey, uint hHash, uint Final, uint dwFlags, [MarshalAs(UnmanagedType.LPWStr)]ref String pbData, ref uint pdwDataLen);
  9.  
  10. //Tried these as well..
  11. //[DllImport("advapi32.dll", SetLastError = true)]
  12. //private static extern bool CryptDecrypt(IntPtr hKey, uint hHash, uint Final, uint dwFlags, ref String pbData, ref uint pdwDataLen);
  13.  
  14. //[DllImport("advapi32.dll", SetLastError = true)]
  15. //private static extern bool CryptDecrypt(IntPtr hKey, uint hHash, uint Final, uint dwFlags, ref byte[] pbData, ref uint pdwDataLen);
  16.  
  17. //[DllImport("advapi32.dll", SetLastError = true)]
  18. //private static extern bool CryptDecrypt(IntPtr hKey, uint hHash, uint Final, uint dwFlags, IntPtr pbData, ref uint pdwDataLen);
  19.  
  20.  
  21. [DllImport("advapi32.dll", SetLastError = true)]
  22. private static extern bool CryptCreateHash(IntPtr hProv, uint Algid, IntPtr hKey, uint dwFlags, ref IntPtr phHash);
  23.  
  24. [DllImport("advapi32.dll", SetLastError = true)]
  25. private static extern bool CryptHashData(IntPtr hHash, String pbData, uint dwDataLen, uint dwFlags);
  26.  
  27. [DllImport("advapi32.dll", SetLastError = true)]
  28. private static extern bool CryptDestroyHash(IntPtr hHash);
  29.  
  30. [DllImport("advapi32.dll", SetLastError = true)]
  31. private static extern bool CryptDeriveKey(IntPtr hProv, uint Algid, IntPtr hBaseData, uint dwFlags, ref IntPtr phKey);
  32.  
  33. [DllImport("advapi32.dll", SetLastError = true)]
  34. private static extern bool CryptDestroyKey(IntPtr hKey);
Expand|Select|Wrap|Line Numbers
  1. ...
  2.  
  3. if (!CryptAcquireContext(ref hCryptProv, "JustTesting", "Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)", 24, 0))
  4.    if (!CryptAcquireContext(ref hCryptProv, "JustTesting", "Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)", 24, 8))
  5.          throw new Exception("Error creating CSP handle");
  6.  
  7. if (!CryptCreateHash(hCryptProv, 32772, IntPtr.Zero, 0, ref hHash))
  8.    throw new Exception("Error creating hash.");
  9.  
  10. if (!CryptHashData(hHash, ls_key, 256, 0))
  11.    throw new Exception("Error creating hash.");
  12.  
  13. if (!CryptDeriveKey(hCryptProv, 26128, hHash, 0, ref hKey))
  14.    throw new Exception("Error creating key.");
  15.  
  16. if (!CryptDestroyHash(hHash))
  17.    throw new Exception("Error destroying hash.");
  18.  
  19. hHash = IntPtr.Zero;
  20.  
  21. string ls = "<encrypted string>"
  22. uint length = (uint)ls.Length * 2; //Unicode
  23.  
  24. if (!CryptDecrypt(hKey, 0, 1, 0, ref ls, ref length))
  25.    throw new Exception("Unable to decrypt " + Marshal.GetLastWin32Error().ToString()); //never get here
  26.  
  27. //or here..
  28. ...
  29.  
Mar 18 '09 #1
5 3805
tlhintoq
3,525 Expert 2GB
Why are you doing all this through DLL imports instead of the native .NET cryptography services?
Mar 18 '09 #2
ShadowLocke
115 100+
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
Mar 18 '09 #3
PRR
750 Expert 512MB
Try using
Expand|Select|Wrap|Line Numbers
  1. catch
  2. {
  3. }
  4.  
This is for catching unmanaged exceptions... if i am right...
Check for right data types .Also look into unsafe for use of pointers...
Mar 24 '09 #4
ShadowLocke
115 100+
just tried using

Expand|Select|Wrap|Line Numbers
  1. //catch //Wants a try..
  2. //{
  3. //CryptDecrypt(lp_key, 0, 1, 0, ref ls_temp, ref lul_length)
  4. //}
  5.  
  6. try
  7. {
  8. CryptDecrypt(lp_key, 0, 1, 0, ref ls_temp, ref lul_length)
  9. }
  10. 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:
Expand|Select|Wrap|Line Numbers
  1. [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  2.             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.
Mar 24 '09 #5
PRR
750 Expert 512MB
My last guess would be using unsafe tags for your code....
Mar 25 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
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...
0
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...
1
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...
2
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...
1
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...
8
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...
3
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...
3
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...
0
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...
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
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...
0
tracyyun
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...
0
agi2029
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.