Solved

Problems with advApi32.dll and HASHING

Posted on 2003-11-09
3
1,648 Views
Last Modified: 2013-12-26
Hello:

I am trying to ENCRYPT code with the ShaEncrypt.bas module, but in some computers the CryptAquireContext returns me a ZERO and in other don't,  why?

I copy the complete code of the ShaEncrypt

***************************************************************
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

Enum HashAlgorithm
   MD2 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD2
   MD4 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD4
   MD5 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_MD5
   SHA1 = ALG_CLASS_HASH Or ALG_TYPE_ANY Or ALG_SID_SHA1
End Enum

Private Const HP_HASHVAL = 2
Private Const HP_HASHSIZE = 4

Private Declare Function GetLastError Lib "Kernel32" () As Long

Function HashString( _
   ByVal Str As String, _
   Optional ByVal Algorithm As HashAlgorithm = SHA1) As String
Dim hCtx As Long
Dim hHash As Long
Dim lRes As Long
Dim lLen As Long
Dim lIdx As Long
Dim abData() As Byte

   ' Get default provider context handle
   lRes = CryptAcquireContext(hCtx, vbNullString, _
           vbNullString, PROV_RSA_FULL, 0)

   MsgBox "Error: " & CStr(GetLastError)
   
   If lRes <> 0 Then

      ' Create the hash
      lRes = CryptCreateHash(hCtx, Algorithm, 0, 0, hHash)

      If lRes <> 0 Then

         ' Hash the string
         lRes = CryptHashData(hHash, ByVal Str, Len(Str), 0)

         If lRes <> 0 Then
           
            ' Get the hash lenght
            lRes = CryptGetHashParam(hHash, HP_HASHSIZE, lLen, 4, 0)

            If lRes <> 0 Then

                ' Initialize the buffer
                ReDim abData(0 To lLen - 1)

                ' Get the hash value
                lRes = CryptGetHashParam(hHash, HP_HASHVAL, abData(0), lLen, 0)

                If lRes <> 0 Then

                    ' Convert value to hex string
                    For lIdx = 0 To UBound(abData)
                        HashString = HashString & _
                                     Right$("0" & Hex$(abData(lIdx)), 2)
                    Next

                End If

            End If

         End If

         ' Release the hash handle
         CryptDestroyHash hHash

      End If
     
   End If

   ' Release the provider context
   CryptReleaseContext hCtx, 0

   ' Raise an error if lRes = 0
   If lRes = 0 Then Err.Raise Err.LastDllError

End Function
***************************************************************
Thanks in advance
Diego.





0
Comment
Question by:DiegodeVicente
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 5

Expert Comment

by:g0rath
ID: 9715181
Are the requirements met?

Here are the requirements from the MSDN page:

Requirements
Client: Included in Windows XP, Windows 2000 Professional, Windows NT Workstation 4.0, Windows Me, Windows 98, Windows 95 OSR2 and later.
Server: Included in Windows Server 2003, Windows 2000 Server, Windows NT Server 4.0.
Redistributable: Requires Internet Explorer 3.02 or later on Windows 95.
Unicode: Implemented as Unicode and ANSI versions on all platforms.
Header: Declared in Wincrypt.h.
Library: Use Advapi32.lib.


What does GetLastError return?
0
 

Author Comment

by:DiegodeVicente
ID: 9715819
GetlasteError = -2146893802

How do I know if my computer meet the requirements?

Many thanks.
0
 
LVL 5

Accepted Solution

by:
g0rath earned 250 total points
ID: 9715945
-2146893802 (80090016)    Keyset does not exist.

Have a look here

"Microsoft Cryptography API May Not Work If the Default CSP Has Been Set Incorrectly"
http://support.microsoft.com/default.aspx?scid=%2Fservicedesks%2Fbin%2Fkbsearch.asp%3FArticle%3D313494
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

717 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