I have a problem with the CryptGenRandom function either within itself or what it relies upon as I am getting a 'The parameter is incorrect' error.
I've read the SDK docs and checked out a lot of C code got from a Google Search Code and cannot figure where the problem is.
I am assuming that the problem is with my code but after kicking it around for a few days, trying various pointer methods and declarations I am now wondering about the function itself; found in the NT kernel starting at Win2000. There is a security issue here which Microsoft will be fixing in XP SP3 but that is not a problem within the context of my use.
I've put CryptGenRandom into a code snippet, below, to illustrate the error generated.
Perhaps someone can spot my mistake?
The code should work with Vista so perhaps someone could try it out with that for me?
The intended output, in this case, is a 16 byte hex string.
I've read the SDK docs and checked out a lot of C code got from a Google Search Code and cannot figure where the problem is.
I am assuming that the problem is with my code but after kicking it around for a few days, trying various pointer methods and declarations I am now wondering about the function itself; found in the NT kernel starting at Win2000. There is a security issue here which Microsoft will be fixing in XP SP3 but that is not a problem within the context of my use.
I've put CryptGenRandom into a code snippet, below, to illustrate the error generated.
Perhaps someone can spot my mistake?
The code should work with Vista so perhaps someone could try it out with that for me?
The intended output, in this case, is a 16 byte hex string.
Code:
#Compile Exe #Register None #Dim All %NOMMIDS = 1 %NOGDI = 1 #Include "WIN32API.inc" ' 27 January 2005 $App = "CGR_Problem" $MS_DEF_PROV = "Microsoft Base Cryptographic Provider v1.0" %PROV_RSA_FULL = 1 %CRYPT_VERIFYCONTEXT = &hF0000000 Macro SysErrMsg(API, Number) = MsgBox SystemErrorMessage( WinErr, API, FuncName$, Number ), %MB_ICONERROR Or %MB_TOPMOST, $App Declare Function crGetDefaultRSAHandle( ByVal Flags As Dword ) As Long Declare Function CryptAcquireContext Lib "advapi32.dll" Alias "CryptAcquireContextA" _ ( hProv As Long, zContainer As Asciiz, zProvider As Asciiz, _ ByVal dwProvType As Dword, ByVal dwFlags As Dword ) As Long Declare Function CryptReleaseContext Lib "advapi32.dll" Alias "CryptReleaseContext" _ ( ByVal hProv As Long, ByVal dwFlags As Dword ) As Long Declare Function CryptGenRandom Lib "advapi32.dll" Alias "CryptGenRandom" _ ( hProv As Long, ByVal dwLen As Dword, ByVal pbBuffer As Byte Ptr ) As Long Declare Function SystemErrorMessage( ByVal Long, String, String, ByVal Long ) As String Function PBMain Local hProv, WinErr, i As Long, hexSize As Dword Dim hexByte( ) As Byte Dim strHex As String hProv = crGetDefaultRSAHandle( %CRYPT_VERIFYCONTEXT ) If hProv = 0 Then Exit Function hexSize = 16 ' Will actually vary but 16 is one such value ReDim hexByte( hexSize - 1 ) As Byte If CryptGenRandom( hProv, ByVal hexSize, hexByte( 0 ) )= 0 Then WinErr = GetLastError : SysErrMsg( "CryptGenRandom", 10 ) GoTo TidyUp End If strHex = "" For i =0 To hexSize - 1 strHex = strHex + Hex$( hexByte(i),2 ) Next MsgBox strHex TidyUp: CryptReleaseContext hProv, 0 End Function Function crGetDefaultRSAHandle( ByVal Flags As Dword ) As Long Local hProv, WinErr As Long If CryptAcquireContext( hProv, ByVal %Null, ByVal %Null, %PROV_RSA_FULL, Flags ) = %FALSE Then If CryptAcquireContext( hProv, ByVal %Null, $MS_DEF_PROV, %PROV_RSA_FULL, Flags ) = %FALSE Then WinErr = GetLastError : SysErrMsg( "CryptAcquireContext", 10 ) Function = %FALSE Else Function = hProv End If Else Function = hProv End If End Function Function SystemErrorMessage( ByVal WinErr As Long, API As String, sFunction As String, ByVal Number As Long ) As String Local pBuffer As Asciiz Ptr, ncbBuffer As Dword Dim sText As String ncbBuffer = FormatMessage( %FORMAT_MESSAGE_ALLOCATE_BUFFER Or %FORMAT_MESSAGE_FROM_SYSTEM Or %FORMAT_MESSAGE_IGNORE_INSERTS, _ ByVal %Null, WinErr, %Null, ByVal VarPtr(pBuffer), 0, ByVal %Null ) If ncbBuffer Then sText = Peek$(pBuffer, ncbBuffer) sText = Remove$(sText, Any Chr$(13) + Chr$(10)) Function = $CrLf + "Error" + Format$(WinErr, " #####") + " during " + API + $CrLf + $CrLf + _ Chr$(34) + sText + Chr$(34) + $CrLf + $CrLf + "Location : " + sFunction + IIf$(Number, Format$(Number, " ###"), "") LocalFree pBuffer Else Function = "Unknown error - Code:" + Format$(WinErr, " #####") End If End Function
Comment