Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. CryptCreateHash returns zero, invalid parameter

CryptCreateHash returns zero, invalid parameter

Scheduled Pinned Locked Moved Visual Basic
cryptographyalgorithmshelpquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    garfield185
    wrote on last edited by
    #1

    Hi. I'm trying to encrypt text using advapi32.dll function CryptEncrypt, RC4. Before encrypting, y call CryptAcquireContext and CryptCreateHash, but this last one returns me zero. Err.LastDllError says it is an 87, so invalid parameter, but can't figure out where is the error.

        Private Const ALG\_CLASS\_HASH As Long = 32768             ' (4 < 13) 
        Private Const ALG\_TYPE\_ANY As Long = 0
        Private Const ALG\_SID\_MD5 As Long = 3
        Private Const CALG\_MD5 As Long = ALG\_CLASS\_HASH Or ALG\_TYPE\_ANY Or ALG\_SID\_MD5
    
        ....
    
        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 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
        ...
        
        'CryptAcquireContext works fine
        lResult = CryptAcquireContext(m\_lProvider, sKeyRoot, CRYPTO\_PROVIDER, PROV\_RSA\_FULL, 0)
        If lResult = 0 Then
            lResult = CryptAcquireContext(m\_lProvider, sKeyRoot, CRYPTO\_PROVIDER, PROV\_RSA\_FULL, CRYPT\_NEWKEYSET)
            If lResult = 0 Then
                MsgBox(ERROR\_AQUIRING\_CONTEXT)
            End If
        End If
    
    
        'Now this is where I get zero
        ' Create a handle to a hash object  using the MD5 algorithm 
        lResult = CryptCreateHash(m\_lProvider, CALG\_MD5, 0, 0, lHash)
        If lResult = 0 Then
            Dim a As Long = Err.LastDllError
            MsgBox(ERROR\_CREATING\_HASH)
            Return ""
        End If
    

    Any idea what I am missing?

    L 1 Reply Last reply
    0
    • G garfield185

      Hi. I'm trying to encrypt text using advapi32.dll function CryptEncrypt, RC4. Before encrypting, y call CryptAcquireContext and CryptCreateHash, but this last one returns me zero. Err.LastDllError says it is an 87, so invalid parameter, but can't figure out where is the error.

          Private Const ALG\_CLASS\_HASH As Long = 32768             ' (4 < 13) 
          Private Const ALG\_TYPE\_ANY As Long = 0
          Private Const ALG\_SID\_MD5 As Long = 3
          Private Const CALG\_MD5 As Long = ALG\_CLASS\_HASH Or ALG\_TYPE\_ANY Or ALG\_SID\_MD5
      
          ....
      
          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 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
          ...
          
          'CryptAcquireContext works fine
          lResult = CryptAcquireContext(m\_lProvider, sKeyRoot, CRYPTO\_PROVIDER, PROV\_RSA\_FULL, 0)
          If lResult = 0 Then
              lResult = CryptAcquireContext(m\_lProvider, sKeyRoot, CRYPTO\_PROVIDER, PROV\_RSA\_FULL, CRYPT\_NEWKEYSET)
              If lResult = 0 Then
                  MsgBox(ERROR\_AQUIRING\_CONTEXT)
              End If
          End If
      
      
          'Now this is where I get zero
          ' Create a handle to a hash object  using the MD5 algorithm 
          lResult = CryptCreateHash(m\_lProvider, CALG\_MD5, 0, 0, lHash)
          If lResult = 0 Then
              Dim a As Long = Err.LastDllError
              MsgBox(ERROR\_CREATING\_HASH)
              Return ""
          End If
      

      Any idea what I am missing?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      I didn't study that in any detail, however I noticed your prototype(s) is/are totally wrong; see here[^]. FWIW: if you need more information on P/Invoke, you may want to read this[^] first. :)

      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

      G 1 Reply Last reply
      0
      • L Luc Pattyn

        I didn't study that in any detail, however I noticed your prototype(s) is/are totally wrong; see here[^]. FWIW: if you need more information on P/Invoke, you may want to read this[^] first. :)

        Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

        Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

        G Offline
        G Offline
        garfield185
        wrote on last edited by
        #3

        Thanks, It's my first time calling api functions so I may be doing it totally wrong... I found that code and I was trying to make it work. At least, part of it did it. I'll study your reference. thanks.

        D 1 Reply Last reply
        0
        • G garfield185

          Thanks, It's my first time calling api functions so I may be doing it totally wrong... I found that code and I was trying to make it work. At least, part of it did it. I'll study your reference. thanks.

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          You found what appears to be VB6 code. The Long type in VB6 is a 32-bit signed integer. In VB.NET, it's a 64-bit signed integer. The two are not interchangable when P/Invoking API functions. In VB.NET, you replace the Longs with Integer.

          A guide to posting questions on CodeProject[^]
          Dave Kreskowiak

          L 1 Reply Last reply
          0
          • D Dave Kreskowiak

            You found what appears to be VB6 code. The Long type in VB6 is a 32-bit signed integer. In VB.NET, it's a 64-bit signed integer. The two are not interchangable when P/Invoking API functions. In VB.NET, you replace the Longs with Integer.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            yep. He also better use IntPtr for passing pointers, if Win64 is going to be used someday. :)

            Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

            Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups