CryptAPI Not Working in ActiveX DLL

Posted on 2004-09-16
Last Modified: 2012-08-13

I am trying to write an ActiveX DLL that makes a call to the CryptAcquireContext API.  Everything works perfectly when I cut and paste the code directly into an executable and run it on the same machine.

The ActiveX DLL is being called from an asp script on a web server and it is failing with code 87

The API call is:
lRet = CryptAcquireContext(hCtx, "", "", 1, 0)


PS: Please don't anybody give me a link to a class or dll that does encryption for me.  I specifically want to hash a string from within this DLL and not using massive code overhead that I don't need.  I'm only interested in why this isn't working and how to get around it (or in an easy to implement hash algorithm if anyone has one)
Question by:Drackon
  • 2
  • 2

Expert Comment

ID: 12084356
Error 87 is "The parameter is incorrect."

In looking over your parameters, I would have expected you to use vbNullString instead of "".  The 1 (PROV_RSA_FULL) looks fine, although I might explicitly qualify it with a typechar (i.e., 1&) just to make sure it was full size.  Ditto for the Flag parameter (0).  I can only assume that the hCtx is Dim'ed properly.

Other than that, you might post the Declare statement that you're using.  It's probably fine, since you say that it's working in an EXE.  Along those lines, since the Container and Provider parameters are pointers to strings, if I were in your shoes and it still wasn't working, I might alter the Declare to change them from String to Long and pass zeros (since it appears you're trying to pass NULL for those parameters).

Honestly, given the error message and the line of code, I'd really suspect the empty string as being the culprit.  If you can, give vbNullString a try and, if that doesn't work, post back here with your Declare.

And just to make sure: the error 87 is the value of lRet which is in the DLL, not in the ASP, right?


Author Comment

ID: 12089771
I have tried the vbNullString and the qualifying as suggested and altering the Declare.  the Error of 87 is actually what is returned when you call Err.LastDllError.  when I call the code below from the Active X DLL this is what is returned:


I have pasted the declares and the function below, I have added in a few extra lines to test where the code is breaking and it seems that it is the first APi call (CryptAcquireContext).

Private Declare Function CryptAcquireContext Lib "advapi32.dll" Alias "CryptAcquireContextA" (ByRef phProv As Long, ByVal pszContainer As String, ByVal pszProvider As String, ByVal dwProvType As Long, ByVal dwFlags As Long) As Long

Private Declare Function CryptReleaseContext Lib "advapi32.dll" (ByVal hProv As Long, ByVal dwFlags As Long) As Long

Private Declare Function CryptCreateHash Lib "advapi32.dll" (ByVal hProv As Long, ByVal Algid As Long, ByVal hKey As Long, ByVal dwFlags As Long, ByRef phHash As Long) As Long

Private Declare Function CryptDestroyHash Lib "advapi32.dll" (ByVal hHash As Long) As Long

Private Declare Function CryptHashData Lib "advapi32.dll" (ByVal hHash As Long, pbData As Any, ByVal dwDataLen As Long, ByVal dwFlags As Long) As Long

Private Declare Function CryptGetHashParam Lib "advapi32.dll" (ByVal hHash As Long, ByVal dwParam As Long, pbData As Any, pdwDataLen As Long, ByVal dwFlags As Long) As Long

Private Const PROV_RSA_FULL = 1
Private Const ALG_CLASS_HASH = 32768
Private Const ALG_TYPE_ANY = 0
Private Const ALG_SID_MD2 = 1
Private Const ALG_SID_MD4 = 2
Private Const ALG_SID_MD5 = 3
Private Const ALG_SID_SHA1 = 4
Private Const HP_HASHVAL = 2
Private Const HP_HASHSIZE = 4

Public Function HashString(ByVal Str As String) As String
  Dim Algorithm: Algorithm = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD5
  Dim hCtx As Long
  Dim hHash As Long
  Dim lRes As Long
  Dim lLen As Long
  Dim lIdx As Long
  Dim abData() As Byte

   HashString = HashString & "TEST1_" & lRes & "<BR>"
   lRes = CryptAcquireContext(hCtx, vbNullString, vbNullString, 1&, 0&)
   If lRes <> 0 Then
      HashString = HashString & "TEST2_" & lRes & "<BR>"
      lRes = CryptCreateHash(hCtx, Algorithm, 0, 0, hHash)
      If lRes <> 0 Then
         HashString = HashString & "TEST3_" & lRes & "<BR>"
         lRes = CryptHashData(hHash, ByVal Str, Len(Str), 0)
         If lRes <> 0 Then
            HashString = HashString & "TEST4_" & lRes & "<BR>"
            lRes = CryptGetHashParam(hHash, HP_HASHSIZE, lLen, 4, 0)
            If lRes <> 0 Then
                HashString = HashString & "TEST5_" & lRes & "<BR>"
                ReDim abData(0 To lLen - 1)
                lRes = CryptGetHashParam(hHash, HP_HASHVAL, abData(0), lLen, 0)
                If lRes <> 0 Then
                    HashString = HashString & "TEST6_" & lRes & "<BR>"
                    For lIdx = 0 To UBound(abData)
                        HashString = HashString & Right$("0" & Hex$(abData(lIdx)), 2)
                End If
            End If
         End If
         CryptDestroyHash hHash
      End If
   End If
   CryptReleaseContext hCtx, 0
   If lRes = 0 Then HashString = HashString & Err.LastDllError
End Function

Accepted Solution

aParser earned 500 total points
ID: 12091809
Try doing your call to Err.LastDllError *before* CryptReleaseContext.  The error 87 at that point is probably because you don't have a valid context, therefore you're asking the API to release an invalid context.

Author Comment

ID: 12093896

I see, I must have had a brain freeze not to see that one.  Thankyou so much!

The real error was #2 which I fixed by just adding the flag CRYPT_VERIFYCONTEXT as per the microsoft article:

Thanks Again!

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now