WCHAR szPasswod[] = {L"goodpasswod" }; HCRYPTHASH hHash; HCRYPTPROV hCryptProv; HCRYPTKEY hKey; DWORD dwHashLen = 0; DWORD dwhash = 4; BYTE byHash[] ={"123456789012345678"}; BYTE bySrc[] = {"123456789012345678901234567890"}; BYTE byDest[64] = {0}; memcpy(byDest,bySrc,32); int iret = -1 ; int idx = 0 ; WCHAR* wszProvider[] = {MS_ENH_RSA_AES_PROV,L"Microsoft Enhanced RSA and AES Cryptographic Provider (Prototype)"}; while(!(iret = CryptAcquireContext(&hCryptProv,NULL,wszProvider[idx],PROV_RSA_AES,CRYPT_SILENT))) { idx ++ ; idx %= 2 ; } // passwd to hash CryptCreateHash(hCryptProv,CALG_MD5,0,0,&hHash);
// use passwd to create hash //CryptHashData(hHash,(BYTE*)szPasswod,wcslen(szPasswod)*2,0);
// set hash CryptGetHashParam(hHash,HP_HASHSIZE,(BYTE*)&dwHashLen,&dwhash,0); // hashlen = 16 CryptSetHashParam(hHash,HP_HASHVAL ,byHash,dwHashLen); CryptDeriveKey(hCryptProv,CALG_AES_128,hHash,CRYPT_EXPORTABLE,&hKey); // CRYPT_EXPORTABLE DWORD dwLen =31; DWORD dwLen1 = 31 ; DWORD dwBufferLen = 48 ;
//get crypt need buffer-len CryptEncrypt(hKey,0,TRUE,0,byDest,&dwLen,0);
// alloc buffer that`s len should be dwlen CryptEncrypt(hKey,0,TRUE,0,byDest,&dwLen1,dwLen); MessageBoxA(NULL,(LPSTR)byDest,(LPSTR)byDest,0); CryptDecrypt(hKey,0,TRUE,0,byDest,&dwLen); MessageBoxA(NULL,(LPSTR)byDest,(LPSTR)byDest,0); CryptDestroyHash(hHash); CryptDestroyKey(hKey); CryptReleaseContext(hCryptProv,0);
测试发现兼容 xp,win7(x64)
LPBYTE GetHash(LPBYTE lpData, DWORD dwDataLen,LPDWORD lpdwOutLen) { HCRYPTPROV hCryptProv; HCRYPTHASH hHash; LPBYTE lpRetBuffer = NULL ; //-------------------------------------------------------------------- // Get a handle to a cryptography provider context. if(CryptAcquireContext( &hCryptProv, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) { // printf("CryptAcquireContext complete. "); //-------------------------------------------------------------------- // Acquire a hash object handle. if(CryptCreateHash( hCryptProv, CALG_MD5, 0, 0, &hHash)) { if(CryptHashData(hHash,lpData,dwDataLen,0)) { //CryptGetHashParam(hHash, HP_HASHSIZE, (BYTE*)(&dwSize), &dwLen, 0); CryptSetHashParam(hHash,HP_HASHSIZE,(BYTE*)lpdwOutLen,sizeof(DWORD)); lpRetBuffer =(LPBYTE) BPP_ReAlloc(NULL,0,*lpdwOutLen); CryptGetHashParam(hHash, HP_HASHVAL, lpRetBuffer, lpdwOutLen, 0); } } } if(hHash) CryptDestroyHash(hHash); if(hCryptProv) CryptReleaseContext(hCryptProv,0); return lpRetBuffer ; }
LPBYTE Encrypt(LPBYTE lpData,LPDWORD lpdwInOutLen) { HCRYPTPROV hCryptProv =NULL ; HCRYPTKEY hKey = NULL ; HCRYPTHASH hHash = NULL ; char* szId =NULL ; LPBYTE lpRetBuf = NULL ; DWORD dwLen ; dwLen = *lpdwInOutLen ; if (!CryptAcquireContextW(&hCryptProv,NULL,NULL,PROV_RSA_FULL,0)) { if (!CryptAcquireContextW(&hCryptProv,NULL,NULL,PROV_RSA_FULL,CRYPT_NEWKEYSET)) { goto EncryptEnd ; } } if (!CryptCreateHash(hCryptProv,CALG_MD5,0,0,&hHash)) { goto EncryptEnd ; } szId = GetIdStr(); // pswd if (!CryptHashData(hHash,(BYTE*)szId,strlen(szId),0)) { goto EncryptEnd ; } if (!CryptDeriveKey(hCryptProv,CALG_RC4,hHash,0x00800000,&hKey)) { dwLen = GetLastError(); goto EncryptEnd ; } // qu jia mi suo xu chang du // dwLen = ((dwLen%8 ?1:0) + dwLen/8)*8 ; lpRetBuf = (LPBYTE)malloc(dwLen);
memcpy(lpRetBuf,lpData,*lpdwInOutLen); if (!CryptEncrypt(hKey,0,TRUE,0,lpRetBuf,lpdwInOutLen,dwLen)) { goto EncryptEnd ; } EncryptEnd: if (szId) { Free((LPVOID&)szId); } if (hHash) { CryptDestroyHash(hHash); } if (hKey) { CryptDestroyKey(hKey); } if (hCryptProv) { CryptReleaseContext(hCryptProv,0); } return lpRetBuf ; } LPBYTE Decrypt(LPBYTE lpData,LPDWORD lpdwInOutLen) { HCRYPTPROV hCryptProv =NULL ; HCRYPTKEY hKey = NULL ; HCRYPTHASH hHash = NULL ; char* szId =NULL ; LPBYTE lpRetBuf = NULL ; DWORD dwLen ; dwLen = *lpdwInOutLen ; if (!CryptAcquireContextW(&hCryptProv,NULL,NULL,PROV_RSA_FULL,0)) { if (!CryptAcquireContextW(&hCryptProv,NULL,NULL,PROV_RSA_FULL,CRYPT_NEWKEYSET)) { goto DecryptEnd ; } } if (!CryptCreateHash(hCryptProv,CALG_MD5,0,0,&hHash)) { goto DecryptEnd ; } szId = GetIdStr(); if (!CryptHashData(hHash,(BYTE*)szId,strlen(szId),0)) { goto DecryptEnd ; } if (!CryptDeriveKey(hCryptProv,CALG_RC4,hHash,CRYPT_EXPORTABLE,&hKey)) { dwLen = GetLastError(); goto DecryptEnd ; } // qu jia mi suo xu chang du //dwLen = ((dwLen%8 ?1:0) + dwLen/8)*8 ; lpRetBuf = (LPBYTE)malloc(dwLen); memcpy(lpRetBuf,lpData,*lpdwInOutLen); //*lpdwInOutLen = dwLen ; if (!CryptDecrypt(hKey,0,TRUE,0,lpRetBuf,lpdwInOutLen)) { goto DecryptEnd ; } DecryptEnd: if (szId) { BPP_Free((LPVOID&)szId); } if (hHash) { CryptDestroyHash(hHash); } if (hKey) { CryptDestroyKey(hKey); } if (hCryptProv) { CryptReleaseContext(hCryptProv,0); } return lpRetBuf ; }