Home > Software engineering >  Crypt32 call CryptStringToBinaryA wrong.. We help to find the reason...
Crypt32 call CryptStringToBinaryA wrong.. We help to find the reason...

Time:09-30

 Private Declare Function CryptBinaryToStringA Lib "crypt32. DLL" (ByRef pbBinary () As Byte, ByVal cbBinary As Long, ByVal dwFlags As Long, ByVal pszString As String, ByVal pcchString As String) As Long 
Private Declare Function CryptBinaryToStringW Lib "crypt32. DLL" (ByRef pbBinary () As Byte, ByVal cbBinary As Long, ByVal dwFlags As Long, ByVal pszString As String, ByVal pcchString As String) As Long
Private Declare Function CryptStringToBinaryA Lib "crypt32. DLL" (ByVal pszString As Long, ByVal cchString As Long, ByVal dwFlags As Long, ByVal pbBinary As Long, ByVal pcbBinary As Long, ByVal pdwSkip As Long, ByVal pdwFlags As Long) As Long
Private Const CRYPT_STRING_BASE64=& amp; H1

'(v=vs. 85 https://msdn.microsoft.com/en-us/library/aa379887. Aspx
'the static CStringA Encode (const void * pBuffer, dwords nSize)
'{
'DWORD nNeed=0;
'CryptBinaryToStringA ((const BYTE *) pBuffer, nSize, CRYPT_STRING_BASE64, NULL, & amp; NNeed);

'CStringA s;
'if (nNeed)
'{
'the STD: : vector Buf (nNeed);
'CryptBinaryToStringA ((const BYTE *) pBuffer, nSize, CRYPT_STRING_BASE64, & amp; Buf [0], & amp; NNeed);
's=& amp; Buf [0].
'}
's.R eplace (" \ r \ n ", "");//the default will add CR/LF
'return s;
'}

'the static void Decode (const char * sBase64, STD: : vector & Decoded_data)
'{
'decoded_data. The clear ();

'if (sBase64)
'{
'DWORD nNeed=0;
'CryptStringToBinaryA (sBase64, 0, CRYPT_STRING_BASE64, NULL, & amp; NNeed, NULL, NULL);
'if (nNeed)
'{
'decoded_data. Resize (nNeed);
'CryptStringToBinaryA (sBase64, 0, CRYPT_STRING_BASE64, & amp; Decoded_data [0], & amp; NNeed, NULL, NULL);
'}
'}
'}
Private Sub Command1_Click ()
Decode "5 oir5piv5zwk
"End Sub
The Public Function Decode (sBase64 As String)
Dim nNeed As Long
Call CryptStringToBinaryA (ByVal StrPtr (sBase64), 0, CRYPT_STRING_BASE64, 0 & amp; NNeed& , 0 & amp; , 0 & amp;) 'the wrong operation.
'MsgBox nNeed&
End the Function




with CryptStringToBinaryW returns 0 - -!



CodePudding user response:

Private Declare Function CryptStringToBinary W Lib "crypt32. DLL" (ByVal pszString As Long, ByVal cchString As Long, ByVal dwFlags As Long, ByVal pbBinary As Long, Byref pcbBinary As Long, ByVal pdwSkip As Long, ByVal pdwFlags As Long As Long)
Using the W

CodePudding user response:

reference 1st floor wy24789 response:
Private Declare Function CryptStringToBinary W Lib "crypt32. DLL" (ByVal pszString As Long, ByVal cchString As Long, ByVal dwFlags As Long, ByVal pbBinary As Long, Byref pcbBinary As Long, ByVal pdwSkip As Long, ByVal pdwFlags As Long As Long)
Use W version



Use the W version. Vice versa, CryptBinaryToStringW went wrong again.

 Private Declare Function CryptBinaryToStringW Lib "crypt32. DLL" (ByRef pbBinary As Long, ByVal cbBinary As Long, ByVal dwFlags As Long, ByVal pszString As Long, ByVal pcchString As Long) As Long 
Private Declare Function CryptStringToBinaryW Lib "crypt32. DLL" (ByVal pszString As Long, ByVal cchString As Long, ByVal dwFlags As Long, ByVal pbBinary As Long, ByRef pcbBinary As Long, ByVal pdwSkip As Long, ByVal pdwFlags As Long) As Long
Private Const CRYPT_STRING_BASE64=& amp; H1

'(v=vs. 85 https://msdn.microsoft.com/en-us/library/aa379887. Aspx
'the static CStringA Encode (const void * pBuffer, dwords nSize)
'{
'DWORD nNeed=0;
'CryptBinaryToStringA ((const BYTE *) pBuffer, nSize, CRYPT_STRING_BASE64, NULL, & amp; NNeed);

'CStringA s;
'if (nNeed)
'{
'the STD: : vector Buf (nNeed);
'CryptBinaryToStringA ((const BYTE *) pBuffer, nSize, CRYPT_STRING_BASE64, & amp; Buf [0], & amp; NNeed);
's=& amp; Buf [0].
'}
's.R eplace (" \ r \ n ", "");//the default will add CR/LF
'return s;
'}

'the static void Decode (const char * sBase64, STD: : vector & Decoded_data)
'{
'decoded_data. The clear ();

'if (sBase64)
'{
'DWORD nNeed=0;
'CryptStringToBinaryA (sBase64, 0, CRYPT_STRING_BASE64, NULL, & amp; NNeed, NULL, NULL);
'if (nNeed)
'{
'decoded_data. Resize (nNeed);
'CryptStringToBinaryA (sBase64, 0, CRYPT_STRING_BASE64, & amp; Decoded_data [0], & amp; NNeed, NULL, NULL);
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • API
  • Related