exe的md5没有给出预期的摘要

时间:2015-01-17 11:54:15

标签: c++ md5 wincrypt

我是新来的,我希望我做得对。

我制作了一个c ++应用程序,用于计算以下文件的MD5哈希摘要 此链接中的Microsoft实现http://msdn.microsoft.com/en-us/library/windows/desktop/aa382380%28v=vs.85%29.aspx 情况是md5哈希摘要对于任何类型文件与任何计算md5摘要的现成工具进行比较都是正确的。除非手中的文件是像cmd.exe这样的可执行文件。哈希摘要是不同的,取决于exe的位置。如果将cmd.exe移动到另一个位置,则摘要将再次变为不同。所以我继续使用openssl库实现相同的功能来面对同样的问题。我注意到微软实现和openssl的哈希摘要是一样的。所以我认为在传递文件以计算摘要之前,有些东西缺失了。但我搜索了很多,什么也没发现。 我尝试使用" createfile"读取文件。来自win API和" fopen"给出相同的结果...所以请帮助我,我缺少什么?

这是源代码

#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <Wincrypt.h>

#define BUFSIZE 1024
#define MD5LEN  16

int _tmain(int argc, _TCHAR* argv[])
{
    DWORD dwStatus = 0;
    BOOL bResult = FALSE;
    HCRYPTPROV hProv = 0;
    HCRYPTHASH hHash = 0;
    HANDLE hFile = NULL;
    BYTE rgbFile[BUFSIZE];
    DWORD cbRead = 0;
    BYTE rgbHash[MD5LEN];
    DWORD cbHash = 0;
    CHAR rgbDigits[] = "0123456789abcdef";
    LPCWSTR filename=L"C:\\Windows\\System32\\cmd.exe";
    // Logic to check usage goes here.

    hFile = CreateFile(filename,
        GENERIC_READ,
        FILE_SHARE_READ,
        NULL,
        OPEN_EXISTING,
        FILE_FLAG_SEQUENTIAL_SCAN,
        NULL);

    if (INVALID_HANDLE_VALUE == hFile)
    {
        dwStatus = GetLastError();
        printf("Error opening file %s\nError: %d\n", filename, 
            dwStatus); 
        return dwStatus;
    }

    // Get handle to the crypto provider
    if (!CryptAcquireContext(&hProv,
        NULL,
        NULL,
        PROV_RSA_FULL,
        CRYPT_VERIFYCONTEXT))
    {
        dwStatus = GetLastError();
        printf("CryptAcquireContext failed: %d\n", dwStatus); 
        CloseHandle(hFile);
        return dwStatus;
    }

    if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
    {
        dwStatus = GetLastError();
        printf("CryptAcquireContext failed: %d\n", dwStatus); 
        CloseHandle(hFile);
        CryptReleaseContext(hProv, 0);
        return dwStatus;
    }

    while (bResult = ReadFile(hFile, rgbFile, BUFSIZE, 
        &cbRead, NULL))
    {
        if (0 == cbRead)
        {
            break;
        }

        if (!CryptHashData(hHash, rgbFile, cbRead, 0))
        {
            dwStatus = GetLastError();
            printf("CryptHashData failed: %d\n", dwStatus); 
            CryptReleaseContext(hProv, 0);
            CryptDestroyHash(hHash);
            CloseHandle(hFile);
            return dwStatus;
        }
    }

    if (!bResult)
    {
        dwStatus = GetLastError();
        printf("ReadFile failed: %d\n", dwStatus); 
        CryptReleaseContext(hProv, 0);
        CryptDestroyHash(hHash);
        CloseHandle(hFile);
        return dwStatus;
    }

    cbHash = MD5LEN;
    if (CryptGetHashParam(hHash, HP_HASHVAL, rgbHash, &cbHash, 0))
    {
        printf("MD5 hash of file %s is: ", filename);
        for (DWORD i = 0; i < cbHash; i++)
        {
            printf("%c%c", rgbDigits[rgbHash[i] >> 4],
                rgbDigits[rgbHash[i] & 0xf]);
        }
        printf("\n");
    }
    else
    {
        dwStatus = GetLastError();
        printf("CryptGetHashParam failed: %d\n", dwStatus); 
    }

    CryptDestroyHash(hHash);
    CryptReleaseContext(hProv, 0);
    CloseHandle(hFile);

    return dwStatus; 


    return 0;
}

此代码计算哈希&#34; 59a1d4facd7b333f76c4142cd42d3aba&#34; 另一方面&#34;数字火山哈希工具&#34;和&#34; md5和sha校验和实用程序v2.1&#34;计算哈希&#34; fc0b4a626881d7c5980d757214db2d25&#34;

1 个答案:

答案 0 :(得分:3)

您的代码是正确的!你遇到了Windows的特质。

您正在阅读的文件位于C:\Windows\System32目录中。

在64位Windows上,如果32位应用程序尝试访问该目录it gets redirectedC:\Windows\SysWow64

由于cmd.exeC:\Windows\System32中都有C:\Windows\SysWow64个文件,但它们是不同的版本和不同的哈希值,检查C:\Windows\System32\cmd.exe哈希的程序会有所不同结果取决于它们是32位还是64位。

您可以通过构建32位和64位的示例程序并观察它们给出不同的答案来自行查看。