Error 87 During Cryptcreatehash
Contents |
Languages , Windows Desktop Development > Visual C++ Question 0 Sign in crypthashdata to vote Igor Sorry to bother you I'm wrapping up my cryptcreatehash example program, and my md5 hash function is producing an error 87 on CryptGetHashParam in Windows 7 cryptgethashparam only. The hHash is 7. Works fine in Vista and XP. I added the GetLastError to pull an error code. Not sure what to make of
Cryptsethashparam
it at this moment. It's late, going home to crash right now. If you have any insight, I could use it right now, I did make a code change and changed DWORD dwOutput to LPDWORD dwOutput, maybe that's the problem. Thanks Igor,hope your stillout there! BYTE* CA_Encryption::_create_MD5_Hash( WCHAR *pzInputW, LPDWORD dwOutput ) { BOOL cryptcreatehash calg_sha_256 bResult = FALSE; HCRYPTPROV hProv; HCRYPTHASH hHash; BYTE *szBuffer = NULL; DWORD dwHashLen = 0; DWORD dwErrorCode = 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 dwBufferSize = 0; 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 ); if(CryptGetHashParam( hHash, HP_HASHVAL, NULL, &dwHashLen, 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 { dwErrorCode = GetLastError(); *dwOutput = 1; szBuffer = L '\0'; } delete [] szInputA; CryptDestroyHash(hHash); CryptReleaseContext(hProv, 0); return szBuffer; } Scode that encrypts passwords before storing them itan access database. It works fine on my pc, however on my client cryptderivekey theyget the following error on the last line of
Cryptcreatehash C#
code when entering apassword.Error - "run-time error '87' application-defined or object-definederror"Code Where Error is
Cryptsignhash
generated is the last line -If lRes = 0 Then Err.Raise Err.LastDllErrorI call the function using a simple calltxtUSERPWD_hash = HashString(txtUSERPWD)Any idea why https://social.msdn.microsoft.com/Forums/vstudio/en-US/8d5680c1-c836-406f-8306-b35734e4e604/md5-hash-not-working-in-windows-7-only-error-87?forum=vcgeneral this is not working? I am assuming it may have somethingto do with the way the pc is locked down.I either need to fix this or come up with a differnt encryption scheme.Thanks in advance..Function HashString( _ByVal Str As String, _Optional ByVal Algorithm As HashAlgorithm = http://comp.lang.basic.visual.misc.narkive.com/7nF6RC4L/cryptacquirecontext-advapi32-dll-run-time-error-87-application-defined-or-object-defined-error MD5) As String'' THIS IS THE PASSWORD CRYPTO MODULEDim hCtx As LongDim hHash As LongDim lRes As LongDim lLen As LongDim lIdx As LongDim abData() As BytelRes = CryptAcquireContext(hCtx, vbNullString, _vbNullString, PROV_RSA_FULL, 0)If lRes <> 0 ThenlRes = CryptCreateHash(hCtx, Algorithm, 0, 0, hHash)If lRes <> 0 ThenlRes = CryptHashData(hHash, ByVal Str, Len(Str), 0)If lRes <> 0 ThenlRes = CryptGetHashParam(hHash, HP_HASHSIZE, lLen, 4, 0)If lRes <> 0 ThenReDim abData(0 To lLen - 1)lRes = CryptGetHashParam(hHash, HP_HASHVAL, abData(0),lLen, 0)If lRes <> 0 ThenHashString = StrConv(abData, vbUnicode)End IfEnd IfEnd IfCryptDestroyHash hHashEnd IfEnd IfCryptReleaseContext hCtx, 0If lRes = 0 Then Err.Raise Err.LastDllErrorEnd Function'-----------------------<< end code >>----------------------------The other block of code that has the delcare statementsPrivate Declare Function CryptAcquireContext Lib "advapi32.dll" _Alias "CryptAcquireContextA" ( _ByRef phProv As Long, _ByVal pszContainer As String, _ByVal pszProvider As String, _ByVal dwProvType As Long, _ByVal
is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that http://www.vbforums.com/showthread.php?774745-Crypto-API-giving-me-unexplained-problems you want to visit from the selection below. Results 1 to 3 of 3 Thread: Crypto API giving me unexplained problems. Tweet Thread Tools Show Printable Version Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode Switch to Threaded Mode Aug 27th, 2014,01:38 AM #1 Ben321 View Profile View Forum Posts Thread Starter Fanatic Member Join Date Oct 2008 Posts 829 Crypto API giving me unexplained problems. Ok, so I have this program, error 87 just to test my ability to get the Crypto API started, and closed properly. And I'm already having troubles. Here's my test code. Code: Private Declare Function CryptAcquireContext Lib "advapi32.dll" Alias "CryptAcquireContextA" (ByRef phProv As Long, ByVal pszContainer As String, ByVal pszProvider As String, ByVal dwProvType As Long, ByVal dwFlags As Long) As Long Private Declare Function CryptReleaseContext Lib "advapi32.dll" (ByRef hProv As Long, ByRef dwFlags As Long) As Long Private Const PROV_RSA_FULL As Long = error 87 during &H1 Private Const CRYPT_VERIFYCONTEXT As Long = &HF0000000 Private Sub Form_Load() Dim hProv As Long Dim RetVal As Long RetVal = CryptAcquireContext(hProv, vbNullString, "Microsoft Base Cryptographic Provider v1.0", PROV_RSA_FULL, CRYPT_VERIFYCONTEXT) If RetVal = 0 Then Print "CryptAcquireContext Error: " & CStr(GetLastError) Print "Crypto Provider Handle: " & CStr(hProv) RetVal = CryptReleaseContext(hProv, 0) If RetVal = 0 Then Print "CryptReleaseContext Error: " & CStr(GetLastError) End Sub Here's what I'm seeing when running the program. If there is any error, it should be first relating to CryptAcquireContext, which results in an invalid Crypto Provider Handle (the value 0), which then results in another error when using that handle with CryptReleaseContext. What's happening is the program works PERFECTLY up to the part where there it tries to release that handle, and only THEN there is an error. Releasing something should not be the first time an error appears, since it IS a valid crypto handle, and it's being used for NOTHING ELSE. Therefore it shouldn't be a situation where it can't release it due to it using it for something else. And the error it generates (error 87) corresponds to the error handle ERROR_INVALID_PARAMETER. This means that one of the parameters I gave it is incorrect. How can it be incorrect. The hProv (Provider Handle) is valid, as it was generated by CryptAcquireContext. And the other parameter f