使用一段别人做示例的cryptoapi函数做MD5
#include "Wincrypt.h"
char code[32];
DWORD size = 32;
HCRYPTPROV hProv = 0;
HCRYPTHASH hHash = 0;
BOOL result = FALSE;
try
{
if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
return false; if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
return false; if (!CryptHashData(hHash, (const BYTE*)LPCTSTR(szPin), strlen(szPin), 0))
return false; if (!CryptGetHashParam(hHash, HP_HASHVAL, (BYTE*)code, &size, 0))
return false; memcpy(szCode, code, min(size, dwSize));
result = TRUE;
}
finally
{
if (hHash)
CryptDestroyHash(hHash);
if (hProv)
CryptReleaseContext(hProv, 0);
}
return result;为什么会提示
'HCRYPTPROV' : undeclared identifier
'HCRYPTHASH' : undeclared identifier
'CryptAcquireContext' : undeclared identifier
#include "Wincrypt.h"
char code[32];
DWORD size = 32;
HCRYPTPROV hProv = 0;
HCRYPTHASH hHash = 0;
BOOL result = FALSE;
try
{
if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
return false; if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
return false; if (!CryptHashData(hHash, (const BYTE*)LPCTSTR(szPin), strlen(szPin), 0))
return false; if (!CryptGetHashParam(hHash, HP_HASHVAL, (BYTE*)code, &size, 0))
return false; memcpy(szCode, code, min(size, dwSize));
result = TRUE;
}
finally
{
if (hHash)
CryptDestroyHash(hHash);
if (hProv)
CryptReleaseContext(hProv, 0);
}
return result;为什么会提示
'HCRYPTPROV' : undeclared identifier
'HCRYPTHASH' : undeclared identifier
'CryptAcquireContext' : undeclared identifier
#define WINVER 0x0400
#endif别忘了加crypt32.lib
错啦,应该是它#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0400
#endif
而在wincrypt.h中有
#if (_WIN32_WINNT >= 0x0400)
...
#endif
我有char source[29];的字符进行MD5加密啊BOOL CMd5testDlg::Encrypt(char *szPin, BYTE *szCode, DWORD dwSize)
{
char code[32];
DWORD size = 32;
HCRYPTPROV hProv = 0;
HCRYPTHASH hHash = 0;
BOOL result = FALSE;
__try
{
if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
return false; if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
return false; if (!CryptHashData(hHash, (const BYTE*)LPCTSTR(szPin), strlen(szPin), 0))
return false; if (!CryptGetHashParam(hHash, HP_HASHVAL, (BYTE*)code, &size, 0))
return false; memcpy(szCode, code, min(size, dwSize));
result = TRUE;
}
__finally
{
if (hHash)
CryptDestroyHash(hHash);
if (hProv)
CryptReleaseContext(hProv, 0);
}
return result;}
调用成功后哈希值在code中(还没有试验...)。
unsgiend char code[m]; // 调用成功后 hash值就在这个数组中了