C ++ verifies Microsoft's Digital Signature

Source: Internet
Author: User

C ++ verifies Microsoft's digital signature BOOL CheckFileTrust (LPCWSTR lpFileName) {BOOL bRet = FALSE; WINTRUST_DATA wd = {0}; WINTRUST_FILE_INFO wfi = {0}; WINTRUST_CATALOG_INFO wci = {0 }; CATALOG_INFO ci = {0}; HCATADMIN hCatAdmin = NULL; if (! CryptCATAdminAcquireContext (& hCatAdmin, NULL, 0) {return FALSE;} HANDLE hFile = CreateFileW (lpFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL ); if (INVALID_HANDLE_VALUE = hFile) {CryptCATAdminReleaseContext (hCatAdmin, 0); return FALSE;} DWORD dwCnt = 100; BYTE byHash [100]; values (hFile, & dwCnt, byHash, 0); CloseHandle (hFile); LPWSTR pszM EmberTag = new WCHAR [dwCnt * 2 + 1]; for (DWORD dw = 0; dw <dwCnt; ++ dw) {wsprintfW (& pszMemberTag [dw * 2], L "% 02X", byHash [dw]);} HCATINFO hCatInfo = CryptCATAdminEnumCatalogFromHash (hCatAdmin, byHash, dwCnt, 0, NULL); if (NULL = hCatInfo) {wfi. cbStruct = sizeof (WINTRUST_FILE_INFO); wfi. pcwszFilePath = lpFileName; wfi. hFile = NULL; wfi. pgKnownSubject = NULL; wd. cbStruct = sizeof (WINTRUST_D ATA); wd. dwUnionChoice = WTD_CHOICE_FILE; wd. pFile = & wfi; wd. dwUIChoice = WTD_UI_NONE; wd. fdwRevocationChecks = WTD_REVOKE_NONE; wd. dwStateAction = WTD_STATEACTION_IGNORE; wd. dwProvFlags = WTD_SAFER_FLAG; wd. hWVTStateData = NULL; wd. pwszURLReference = NULL;} else {cryptcataloginfofromcontext (hCatInfo, & ci, 0); wci. cbStruct = sizeof (WINTRUST_CATALOG_INFO); wci. pcwszCatalogFilePath = ci. wszCatalogFile; Wci. pcwszMemberFilePath = lpFileName; wci. pcwszMemberTag = pszMemberTag; wd. cbStruct = sizeof (WINTRUST_DATA); wd. dwUnionChoice = WTD_CHOICE_CATALOG; wd. pCatalog = & wci; wd. dwUIChoice = WTD_UI_NONE; wd. fdwRevocationChecks = WTD_STATEACTION_VERIFY; wd. dwProvFlags = 0; wd. hWVTStateData = NULL; wd. pwszURLReference = NULL;} GUID action = WINTRUST_ACTION_GENERIC_VERIFY_V2; HRESULT hr = WinVerifyTrust (NULL ,& Action, & wd); bRet = SUCCEEDED (hr); if (NULL! = HCatInfo) {CryptCATAdminReleaseCatalogContext (hCatAdmin, hCatInfo, 0);} CryptCATAdminReleaseContext (hCatAdmin, 0); delete [] pszMemberTag; return bRet ;} this code was inadvertently searched on a foreigner's Forum and looks like a German (because his comments are not written in English, and German only guesses, the Delphi code written in Spain, Portugal, France, and Russia is also possible. It uses WinTrust. dll. Friends who use VS2005 can include WinTrust. h. SoftPub. h and Mscat. h, and add WinTrust. lib; if you are using VC6, you can refer to the function and struct Declaration on MSDN and call it using the function pointer. I will add some types to facilitate translation into other languages: typedef struct _ WINTRUST_DATA {DWORD cbStruct; LPVOID pPolicyCallbackData; LPVOID pSIPClientData; DWORD dwUIChoice; DWORD category; DWORD dwUnionChoice; union {struct character _ * pFile; struct character _ * pCatalog; struct WINTRUST_BLOB_INFO _ * pBlob; struct character _ * pSgnr; struct character _ * pCert ;}; DWORD dwStateAction; HANDLE hWVTStateData; WCHAR * pwszURLReference; DWORD success; DWORD dwUIContext;} WINTRUST_DATA, * break; typedef struct fail _ {DWORD cbStruct; LPCWSTR pcwszFilePath; HANDLE hFile; GUID * pgKnownSubject;} HANDLE, * HANDLE; typedef struct character _ {DWORD cbStruct; DWORD dwCatalogVersion; LPCWSTR character; LPCWSTR pcwszMemberTag; LPCWSTR character; HANDLE hMemberFile;} character, * character; typedef struct CATALOG_INFO _ {DWORD cbStruct; WCHAR wszCatalogFile [MAX_PATH];} CATALOG_INFO; I will add the API Declaration used: LONG WINAPI WinVerifyTrust (_ in HWND hWnd ,__ in GUID * pgActionID ,__ in lpvpwoid vtdata ); related

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.