0

CryptAcquireContext function returns FALSE with The Keyset is not defined error message. Here is my code.

<DllImport("advapi32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
Public Shared Function CryptAcquireContext(ByRef hProv As IntPtr, ByVal pszContainer As String, ByVal pszProvider As String, ByVal dwProvType As Int32, ByVal dwFlags As UInt32) As Boolean
End Function

<DllImport("advapi32.dll", SetLastError:=True)>
Public Shared Function CryptCreateHash(ByVal hProv As IntPtr, ByVal Algid As Int32, ByVal hKey As IntPtr, ByVal dwFlags As Int32, ByRef phHash As IntPtr) As Boolean
End Function

<DllImport("advapi32.dll", SetLastError:=True)>
Public Shared Function CryptHashData(ByVal hHash As IntPtr, ByVal pbData() As Byte, ByVal dwDataLen As Int32, ByVal dwFlags As Int32) As Boolean
End Function

Public Const PROV_RSA_FULL As Int32 = 1
Private Const CRYPT_VERIFYCONTEXT As UInt32 = &HF0000000UI

Private Function GetHash(ByVal text As String) As Boolean
    Dim bResult As Boolean = False
    Dim hProv As IntPtr
    Dim hHash As IntPtr

    If CryptAcquireContext(hProv, vbNull, vbNull, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) Then
        If CryptCreateHash(hProv, CALG_SHA1, 0, 0, hHash) Then
            If CryptHashData(hHash, Encoding.ASCII.GetBytes(text), text.Length, 0) Then
                Dim bHash(20) As Byte
                Dim dwHashLen As Int32 = Marshal.SizeOf(bHash)
                bResult = CryptGetHashParam(hHash, HP_HASHVAL, bHash, dwHashLen, 0)
                If bResult = True Then
                    //Rest of string operations
                End If
            End If
        End If
    End If
    Dim errorMessage As String = New Win32Exception(Marshal.GetLastWin32Error()).Message
    MsgBox(errorMessage)
    Return bResult
End Function

Do you have any idea about what i'm doing wrong?

Thanks

kenarsuleyman
  • 920
  • 1
  • 6
  • 26
  • i don't know vb, but may be problem in how you declare `CRYPT_VERIFYCONTEXT As Int32 = &HF0000000` - really this is **unsigned** int. (*UInt32* ? ) because *0xF0000000* is out of range int `[-0x80000000, +0x7fffffff]` - don't sure how it marshalled. may be MST bit is lost.. – RbMm May 09 '17 at 23:08
  • I changed decleration with `Public Const CRYPT_VERIFYCONTEXT As UInt32 = &HF0000000UI` and function parameter to `UInt32` but nothing changed. Actually your idea makes sense but didn't work either. – kenarsuleyman May 09 '17 at 23:15
  • *NTE_KEYSET_NOT_DEF( 0x80090019L ) - The key container specified by pszContainer does not exist, or the requested provider does not exist* but you use 0 as *pszContainer* and with *CRYPT_VERIFYCONTEXT* this mean that container not used at all. *PROV_RSA_FULL* not exist ?! can not be. in this case good way to binary debugging. usually this way problem is very fast understanded – RbMm May 09 '17 at 23:20
  • Yes, i read MSDN defination and this is what confuses me.As i know binary debugging refers to pick a random line from snippet and check if code works perfect until selected line but in my sitation this is beginning of whole function :) – kenarsuleyman May 09 '17 at 23:25
  • if you can put binary file here (with only this context) and it can be runned on any windows without heavy install (vb think require some installation or not ?) it will be very easy debug binary exe and exactly say where error – RbMm May 09 '17 at 23:31
  • I don't know if it is allowed to share compiled executable files but i'm putting exe file with only these context. I just removed messagebox because it is not necessery when debugging. All you need to do is clicking button. https://drive.google.com/open?id=0BzaM-gQ9rvNCMUxscTMxaDhjTVE – kenarsuleyman May 09 '17 at 23:54
  • you problem in 2 and 3 argument - you pass `"1"` string in both, but not 0 – RbMm May 10 '17 at 00:02
  • 1
    so in place *vbNull* passed `"1"` string (0x31 0x00 0x00 0x00 by bytes) – RbMm May 10 '17 at 00:04
  • Thank you so much. Yes that's why i hate VB. How the hell vbNull can pass 1? Replacing it with nothing did the trick. – kenarsuleyman May 10 '17 at 00:10
  • yes, when i zero string *CryptAcquireContext* is ok. also *CryptCreateHash* and *CryptHashData* is ok. but *CryptGetHashParam* already not called. [exception](http://i.imgur.com/LQPVV0t.png) in your app – RbMm May 10 '17 at 00:12
  • read [this](http://stackoverflow.com/a/26432518/6401656) about *vbNull* – RbMm May 10 '17 at 00:19
  • so *vbNull* is not 0 pointer and not empty string `""` but constant *VariantType* – RbMm May 10 '17 at 00:24

0 Answers0