为什么我CryptSignHash调用中老是有问题?试过无数的例子,甚至用了win32 sdk里面的例子
Example#include <wincrypt.h>
  
HCRYPTPROV hProv = 0;
#define BUFFER_SIZE 256
BYTE pbBuffer[BUFFER_SIZE];
HCRYPTHASH hHash = 0;
BYTE *pbSignature = NULL;
DWORD dwSigLen;
LPTSTR szDescription = TEXT("Test Data");
DWORD i;
  
// Get handle to the default provider.
if(!CryptAcquireContext(&hProv, NULL, NULL, PROV_RSA_FULL, 0)) {
    printf("Error %x during CryptAcquireContext!\n", GetLastError());
    goto done;
}// Fill buffer with test data.
for(i = 0 ; i < BUFFER_SIZE ; i++) {    pbBuffer[i] = (BYTE)i;
}// Create hash object.
if(!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash)) {
    printf("Error %x during CryptCreateHash!\n", GetLastError());
    goto done;
}// Hash buffer.
if(!CryptHashData(hHash, pbBuffer, BUFFER_SIZE, 0)) {
    printf("Error %x during CryptHashData!\n", GetLastError());
    goto done;
}// Determine size of signature and allocate memory.
dwSigLen = 0;
if(!CryptSignHash(hHash, AT_SIGNATURE, TEXT(""), 0, NULL, &dwSigLen)) {    printf("Error %x during CryptSignHash!\n", GetLastError());
    if(GetLastError()!=NTE_BAD_LEN) goto done;
}
if((pbSignature = malloc(dwSigLen)) == NULL) {
    printf("Out of memory!\n");
    goto done;
}
  
// Sign hash object.
if(!CryptSignHash(hHash, AT_SIGNATURE, szDescription, 0, pbSignature, &dwSigLen)) {
    printf("Error %x during CryptSignHash!\n", GetLastError());
    goto done;
}
  
// Store or transmit the signature, test buffer, and description string....
  
done:// Free memory used to store signature.
if(pbSignature != NULL) free(pbSignature);// Destroy hash object.
if(hHash != 0) CryptDestroyHash(hHash);// Release provider handle.
if(hProv != 0) CryptReleaseContext(hProv, 0);
其他都没有问题,就是在CryptSignHash调用中出现程序调用失败。
还有CryptGetUserKey也会出现问题,这两个函数中都使用了AT_SIGNATURE这个参数。
我在XP和98下都调式过,不知道什么原因,在用AT_SIGNATURE参数前是否要自己生成密钥对呢?