المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : c++, CryptGetHashParam works in XP, Vista, but not Windows 7



C++ Programming
05-13-2012, 10:50 PM
I'm baffled by this. My MD5 Hash function works in Windows XP and Windows Vista, but fails in windows 7 with an Error 87.

So I rewrote the function to use HP_HASHSIZE to properly get the size, but nothing I did on Windows 7 made a difference.

I went back to XP, modified my code with the new changes, ran it, and produced the correct hash value result.

My Triple DES works fine on Windows 7, and my DES, so I'm going to check my crypt files on Windows 7 now, and see if I can find anything.

BYTE* CA_Encryption::_create_MD5_Hash( WCHAR *pzInputW, LPDWORD dwOutput ) { BOOL bResult = FALSE; HCRYPTPROV hProv = 0; HCRYPTHASH hHash = 0; BYTE *szBuffer = NULL; DWORD dwHashLen = 0; // Get the size of the conversion int iCharA = ( WideCharToMultiByte(CP_UTF8, 0, pzInputW, -1, NULL, 0, NULL, NULL) - 1 ); char *szInputA = new char[ iCharA ]; WideCharToMultiByte( CP_UTF8, 0, pzInputW, -1, szInputA, iCharA, NULL, NULL ); DWORD dwCount = sizeof( DWORD ); DWORD dwPasswordLen = iCharA; bResult = CryptAcquireContextW( &hProv, NULL, MS_STRONG_PROV, PROV_RSA_FULL, 0); bResult = CryptCreateHash( hProv, CALG_MD5, 0, 0, &hHash ); bResult = CryptHashData( hHash, (BYTE*)szInputA, dwPasswordLen, 0 ); delete [] szInputA; // The line below does nothing and produces an error 87, if(CryptGetHashParam( hHash, HP_HASHSIZE, (BYTE*)&dwHashLen, &dwCount, 0 ) ) { if (( *dwOutput > 0 ) && ( *dwOutput < 4096)) { szBuffer = new BYTE[dwHashLen+1]; CryptGetHashParam( hHash, HP_HASHVAL, szBuffer, &dwHashLen, 0 ); *dwOutput = dwHashLen; szBuffer[dwHashLen] = 0; } else { *dwOutput = dwHashLen+1; szBuffer = L'\0'; } } else { *dwOutput = 1; szBuffer = new BYTE[ *dwOutput ]; szBuffer[ 0 ] = L'\0'; } CryptDestroyHash(hHash); CryptReleaseContext(hProv, 0); return szBuffer; }