CryptCreateHash returns zero, invalid parameter
-
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?
-
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?
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.
-
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.
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.
-
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.
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 -
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 Kreskowiakyep. 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.