c++ - CryptStringToBinary API behavior -
i seeing strange behavior cryptstringtobinary cryptography api. please see below code (config: x64 debug):
#include "stdafx.h" #include <windows.h> #include <strsafe.h> #include <iostream> #include <exception> void main() { dword dwskip; dword dwflags; dword dwdatalen; //lpcwstr pszinput = l"jaaaaaecaaadzgaaakqaagdnnl1l56bwgfjdgr3rpxtqqqn6daw3usv2emkjym4t"; //this works fine lpcwstr pszinput = l"mytest"; //doesnt work, api returns false,error code 0x0000000d // determine size of byte array , allocate memory. if(! cryptstringtobinary( pszinput, _tcslen( pszinput ) + 1, crypt_string_base64, null, &dwdatalen, &dwskip, &dwflags ) ) { dword dw = getlasterror(); //0x0000000d: data invalid throw std::exception( "error computing byte length." ); } byte *pbytebyte = null; try { pbytebyte = new byte[ dwdatalen ]; if( !pbytebyte ) { dword m_dwerror = error_invalid_data; throw std::exception( "wrong array size." ); } } catch( std::exception &ex ) { throw ex; } catch(...) { throw std::exception( "out of memory." ); } return ; }
with first pszinput string (commented string above), cryptstringtobinary returns true if use l"mytest" pszinput string returns false error code 0x0000000d. see, there issue length of string passed api. when pass length without null terminated char (removed +1), api returns true always. in case, byte length returned correct?
could me understanding reason behind behavior? also, usage of length parameter in api correct?
thanks in advance!
Comments
Post a Comment