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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

911 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

25 Experts available now in Live!

Get 1:1 Help Now