-2
  1. 我编写的一小段代码列出了一个虚拟目录中的文件,然后计算哈希值。
  2. 但是当我将文件名传递给calc_hash函数时,程序在计算两个文件的哈希值后终止......出现错误Unhandled exception at 0x77406850 in hashtest3-cpp.exe: 0xC0000005: Access violation writing location 0x64333782.

有人可以指出错误....代码片段::

#include <stdio.h>
#include <windows.h>
#include <Wincrypt.h>
#include <strsafe.h>
#include <string.h>
#include <tchar.h>
#include <iostream>

using namespace std;

#define BUFSIZE 1024
#define MD5LEN  16

//======================================================VARIABLE INITIALIZATION
DWORD Status = 0;
BOOL bResult = FALSE;

ULONG_PTR hProv = 0;
ULONG_PTR hHash = 0;
HANDLE hFile = NULL;
DWORD i;
DWORD j=0;


BYTE rgbFile[BUFSIZE];
DWORD cbRead = 0;
BYTE rgbHash[MD5LEN];
DWORD cbHash = 0;
LPTSTR filename;
CHAR resultstream[33];

HANDLE hFind;
WIN32_FIND_DATA data;

TCHAR filepath[260] = _T("");
LPWSTR FNAME;
LPWSTR FULLPATH = L"c:\\test\\";
LPWSTR dir = L"c:\\test\\*.*";
LPWSTR DEFAULt = L"";

CHAR rgbDigits[] = "0123456789abcdef";


//=====================================================FUNCTION DECLARATION

void list_files(void);

void calc_hash(void);


//======================================================MAIN START

int main()
{
list_files();
//calc_hash();





getchar();
return 0; 
}


//======================================================LISTING FILES IN A DIRECTORY

void list_files()
{
    hFind = FindFirstFile(dir, &data);
if (hFind != INVALID_HANDLE_VALUE)
{

    do 
    {

        StringCchCopy(filepath, 260, DEFAULt);
        resultstream[33] = '\0';
        DWORD j=0;

        FNAME = data.cFileName;
        StringCchCat(filepath, 260, FULLPATH);
        StringCchCat(filepath, 260, FNAME);
        filename = (LPTSTR)filepath;

        printf("\n%ws", filename);

        /*calc_hash();
        getchar();*/

        continue;

    }while (FindNextFile(hFind, &data));

}
getchar();
}

//======================================================HASH OF A FILE
void calc_hash()
{

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

if (INVALID_HANDLE_VALUE == hFile)
{
    Status = GetLastError();
//printf("\nerror opening file::%d", Status);

goto end;
}

// Get handle to the crypto provider
if (!CryptAcquireContext(   
            &hProv,
            NULL,
            NULL,
            PROV_RSA_FULL,
            CRYPT_VERIFYCONTEXT))
{
    Status = GetLastError();
    printf("CryptAcquireContext error:: %d\n", Status); 
    CloseHandle(hFile);
getchar();
goto end;

}

if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash))
{
    Status = GetLastError();
    printf("CryptAcquireContext error:: %d\n", Status); 
    CloseHandle(hFile);
    CryptReleaseContext(hProv, 0);
getchar();
goto end;

}

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

    if (!CryptHashData(hHash, rgbFile, cbRead, 0))
    {
        Status = GetLastError();
        printf("CryptHashData error:: %d\n", Status); 
        CryptReleaseContext(hProv, 0);
        CryptDestroyHash(hHash);
        CloseHandle(hFile);
    getchar();
    goto end;

    }
}

//================================GENERATING RESULT=============================
if (!bResult)
{
    Status = GetLastError();
    printf("ReadFile error:%S:: %d\n",filename, Status); 
    CryptReleaseContext(hProv, 0);
    CryptDestroyHash(hHash);
    CloseHandle(hFile);
getchar();
goto end;

}

cbHash = MD5LEN;

if (CryptGetHashParam(hHash, HP_HASHVAL, rgbHash, &cbHash, 0))
{
    printf("MD5 hash of file %ws is:- ", filename);
    for (i = 0; i < cbHash; i++)
    {
    resultstream[j]=rgbDigits[rgbHash[i] >> 4];
    j++;
    resultstream[j]=rgbDigits[rgbHash[i] & 0xf];
    j++;


    }
resultstream[j] = '\0'; 
printf("\n%s", resultstream );
    printf("\n");

}
else
{
    Status = GetLastError();
    printf("CryptGetHashParam error:: %d\n", Status); 
}

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




end:;

//getchar();
}
4

1 回答 1

2

问题是你声明resultstream

CHAR resultstream[33];

但是你在你的代码中使用

resultstream[33] = '\0';

数组索引为 0 ,resultstream因此有效值为 0-32,您正在访问未为该数组分配的内存(因此出现“访问冲突”)

于 2013-06-17T21:39:57.360 回答