小弟写一个程序,其中用到了VC提供的一个头文件,但是不知道什么原因,用到头文件中的类型,报错为类型没有定义,用到头文件中的函数,保存函数没有声明。
-------------------------------------
#ifndef MAIN_H
#define MAIN_H#include "stdio.h"
#include "windows.h"
#include "wincrypt.h"#define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING|X509_ASN_ENCODING)#define ENCRYPT_ALGORITHM CALG_3DES
#define ENCRYPT_BLOCK_SIZE 8extern "C" __declspec(dllexport) int fnEncryptDLL(void);extern "C" __declspec(dllexport) BOOL MyEncryptFile(PCHAR szSource,PCHAR szDestination,PCHAR szPassword);extern "C" __declspec(dllexport) void HandleError(char *s);extern "C" __declspec(dllexport) BOOL MyDecryptFile(PCHAR szSource,PCHAR szDestination,PCHAR szPassword);#endif
-------------------------------------
#ifndef MAIN_H
#define MAIN_H#include "stdio.h"
#include "windows.h"
#include "wincrypt.h"#define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING|X509_ASN_ENCODING)#define ENCRYPT_ALGORITHM CALG_3DES
#define ENCRYPT_BLOCK_SIZE 8extern "C" __declspec(dllexport) int fnEncryptDLL(void);extern "C" __declspec(dllexport) BOOL MyEncryptFile(PCHAR szSource,PCHAR szDestination,PCHAR szPassword);extern "C" __declspec(dllexport) void HandleError(char *s);extern "C" __declspec(dllexport) BOOL MyDecryptFile(PCHAR szSource,PCHAR szDestination,PCHAR szPassword);#endif
{
return 42;
}void HandleError(char *s)
{
fprintf(stderr,"An error occurred in running the program.\n");
fprintf(stderr,"%s\n",s);
fprintf(stderr,"Error number %x.\n",GetLastError());
fprintf(stderr,"Program terminating.\n");
exit(1);
}BOOL MyEncryptFile(PCHAR szSource,PCHAR szDestination,PCHAR szPassword)
{
FILE * hSource;
FILE * hDestination;
HCRYPTPROV hCryptProv;
HCRYPTKEY hKey;
HCRYPTKEY hXchgKey;
HCRYPTHASH hHash; PBYTE pbBuffer;
DWORD dwBlockLen;
DWORD dwBufferLen;
DWORD dwCount; if(hSource=fopen(szSource,"rb"))
printf("The source plaintext file,%s,is open.\n",szSource);
else
HandleError("Error opening source plaintext file!"); if(hDestination=fopen(szDestination,"wb"))
printf("Destination file%s is open.\n",szDestination);
else
HandleError("Error opening destination ciphertext file!"); if(CryptAcquireContext(&hCryptProv, "黄军", NULL,PROV_RSA_FULL,0))
printf("A cryptographic provider has been acquired.\n");
else
HandleError("Error during CryptAcquireContext!"); if(!szPassword)
{
if(CryptGenKey(hCryptProv,ENCRYPT_ALGORITHM,CRYPT_EXPORTABLE,&hKey))
printf("A session key has been created.\n");
else
HandleError("Error during CryptGenKey.\n"); if(CryptGetUserKey(hCryptProv,AT_KEYEXCHANGE,&hXchgKey))
printf("The user public key has been retrieved.\n");
else
HandleError("User public key is not a available and may not exist.");
if(CryptExportKey(hKey,hXchgKey,SIMPLEBLOB,0,NULL,&dwKeyBlobLen))
printf("The key blob is %d bytes long.\n",dwKeyBlobLen);
else
HandleError("Error computing blob length!\n"); if(pbKeyBlob=(BYTE *)malloc(dwKeyBlobLen))
printf("Memory is allocated for the key blob.\n");
else
HandleError("Out of memory.\n");
if(CryptExportKey(hKey,hXchgKey,SIMPLEBLOB,0,pbKeyBlob,&dwKeyBlobLen))
printf("The key has been exported.\n");
else
HandleError("Error during CryptExportKey!\n");
CryptDestroyKey(hXchgKey);
hXchgKey=0;
fwrite(&dwKeyBlobLen,sizeof(DWORD),1,hDestination);
if(ferror(hDestination))
HandleError("Error writing header.");
else
printf("A file header has been written.\n"); fwrite(pbKeyBlob,1,dwKeyBlobLen,hDestination);
if(ferror(hDestination))
HandleError("Error writing header");
else
printf("The key blob has been written to the file.\n");
}
else
{
if(CryptCreateHash(hCryptProv,CALG_MD5,0,0,&hHash))
printf("A hash object has been created.\n");
else
HandleError("Error during CryptCreateHash!\n"); if(CryptHashData(hHash,(BYTE *)szPassword,strlen(szPassword),0))
printf("The password has been added to the hash.\n");
else
HandleError("Error during CryptHashData.\n"); if(CryptDeriveKey(hCryptProv,ENCRYPT_ALGORITHM,hHash,0,&hKey))
printf("An encryption key is derived from the password hash.\n");
else
HandleError("Error during CryptDeriveKey!\n");
CryptDestroyHash(hHash);
hHash=0;
} dwBlockLen=1000 - 1000 % ENCRYPT_BLOCK_SIZE;
if(ENCRYPT_BLOCK_SIZE>1)
dwBufferLen=dwBlockLen+ENCRYPT_BLOCK_SIZE;
else
dwBufferLen=dwBlockLen; if(pbBuffer=(BYTE *)malloc(dwBufferLen))
printf("Memory has been allocated for the buffer.\n");
else
HandleError("Out of memory.\n");
do
{
dwCount=fread(pbBuffer,1,dwBlockLen,hSource);
if(ferror(hSource))
HandleError("Error reading plaintext!\n");
if(!CryptEncrypt(hKey,0,feof(hSource),0,pbBuffer,&dwCount,dwBufferLen))
HandleError("Error during CryptEncrypt.\n");
fwrite(pbBuffer,1,dwCount,hDestination); if(ferror(hDestination))
HandleError("Error writing ciphertext.");
}while(!feof(hSource)); if(hSource)
fclose(hSource);
if(hDestination)
fclose(hDestination);
if(pbBuffer)
free(pbBuffer);
if(hKey)
CryptDestroyKey(hKey);
if(hXchgKey)
CryptDestroyKey(hXchgKey);
if(hHash)
CryptDestroyHash(hHash);
if(hCryptProv)
CryptReleaseContext(hCryptProv,0);
return(TRUE);
}BOOL MyDecryptFile(PCHAR szSource,PCHAR szDestination, PCHAR szPassword)
{
FILE *hSource;
FILE *hDestination; HCRYPTPROV hCryptProv;
HCRYPTPROV hKey;
HCRYPTHASH hHash; PBYTE pbKeyBlob=NULL;
DWORD dwKeyBlobLen; PBYTE pbBuffer;
DWORD dwBlockLen;
DWORD dwBufferLen;
DWORD dwCount; BOOL status=FALSE; if(!(hSource=fopen(szSource,"rb")))
HandleError("Error opening ciphertext file!"); if(!(hDestination=fopen(szDestination,"wb")))
HandleError("Error opening plaintext file!"); if(!CryptAcquireContext(&hCryptProv,"黄军",NULL,PROV_RSA_FULL,0))
HandleError("Error during CryptAcquireContext!"); if(!szPassword)
{
fread(&dwKeyBlobLen,sizeof(DWORD),1,hSource);
if(ferror(hSource)||feof(hSource))
HandleError("Error reading file header!"); if(!(pbKeyBlob=(BYTE*)malloc(dwKeyBlobLen)))
HandleError("Memory allocation error."); fread(pbKeyBlob,1,dwKeyBlobLen,hSource);
if(ferror(hSource)||feof(hSource))
HandleError("Error reading file header!\n"); if(!CryptImportKey(hCryptProv,pbKeyBlob,dwKeyBlobLen,0,0,&hKey))
HandleError("Error during CryptImportKey!");
}
else
{
if(!CryptCreateHash(hCryptProv,CALG_MD5,0,0,&hHash))
HandleError("Error during CryptCreateHash!"); if(!CryptHashData(hHash,(BYTE *)szPassword,strlen(szPassword),0))
HandleError("Error during CryptHashData!"); if(!CryptDeriveKey(hCryptProv,ENCRYPT_ALGORITHM,hHash,0,&hKey))
HandleError("Error during CryptDeriveKey!"); CryptDestroyHash(hHash);
hHash=0;
} dwBlockLen=1000 - 1000 % ENCRYPT_BLOCK_SIZE;
dwBufferLen=dwBlockLen; if(!(pbBuffer=(BYTE *)malloc(dwBufferLen)))
HandleError("Out of memory!\n"); do
{
dwCount=fread(pbBuffer,1,dwBlockLen,hSource);
if(ferror(hSource))
HandleError("Error reading ciphertext!");
if(!CryptDecrypt(hKey,0,feof(hSource),0,pbBuffer,&dwCount))
HandleError("Error during CryptDecrypt!"); fwrite(pbBuffer,1,dwCount,hDestination);
if(ferror(hDestination))
HandleError("Error writing plaintext!");
}while(!feof(hSource)); status=TRUE; if(hSource)
fclose(hSource);
if(hDestination)
fclose(hDestination); if(pbKeyBlob)
free(pbKeyBlob);
if(pbBuffer)
free(pbBuffer);
if(hKey)
CryptDestroyKey(hKey);
if(hHash)
CryptDestroyHash(hHash);
if(hCryptProv)
CryptReleaseContext(hCryptProv,0);
return status;
}
--------------------Configuration: Test - Win32 Debug--------------------
Compiling...
main.cpp
c:\documents and settings\administrator\桌面\main\main.cpp(21) : error C2065: 'HCRYPTPROV' : undeclared identifier
c:\documents and settings\administrator\桌面\main\main.cpp(21) : error C2146: syntax error : missing ';' before identifier 'hCryptProv'
c:\documents and settings\administrator\桌面\main\main.cpp(21) : error C2065: 'hCryptProv' : undeclared identifier
c:\documents and settings\administrator\桌面\main\main.cpp(22) : error C2065: 'HCRYPTKEY' : undeclared identifier
c:\documents and settings\administrator\桌面\main\main.cpp(22) : error C2146: syntax error : missing ';' before identifier 'hKey'
c:\documents and settings\administrator\桌面\main\main.cpp(22) : error C2065: 'hKey' : undeclared identifier
c:\documents and settings\administrator\桌面\main\main.cpp(23) : error C2146: syntax error : missing ';' before identifier 'hXchgKey'
c:\documents and settings\administrator\桌面\main\main.cpp(23) : error C2065: 'hXchgKey' : undeclared identifier
c:\documents and settings\administrator\桌面\main\main.cpp(24) : error C2065: 'HCRYPTHASH' : undeclared identifier
c:\documents and settings\administrator\桌面\main\main.cpp(24) : error C2146: syntax error : missing ';' before identifier 'hHash'
c:\documents and settings\administrator\桌面\main\main.cpp(24) : error C2065: 'hHash' : undeclared identifier
c:\documents and settings\administrator\桌面\main\main.cpp(41) : error C2065: 'CryptAcquireContext' : undeclared identifier
c:\documents and settings\administrator\桌面\main\main.cpp(41) : error C2065: 'PROV_RSA_FULL' : undeclared identifier
c:\documents and settings\administrator\桌面\main\main.cpp(48) : error C2065: 'CryptGenKey' : undeclared identifier
c:\documents and settings\administrator\桌面\main\main.cpp(48) : error C2065: 'CALG_3DES' : undeclared identifier
c:\documents and settings\administrator\桌面\main\main.cpp(48) : error C2065: 'CRYPT_EXPORTABLE' : undeclared identifier
c:\documents and settings\administrator\桌面\main\main.cpp(53) : error C2065: 'CryptGetUserKey' : undeclared identifier
c:\documents and settings\administrator\桌面\main\main.cpp(53) : error C2065: 'AT_KEYEXCHANGE' : undeclared identifier
c:\documents and settings\administrator\桌面\main\main.cpp(58) : error C2065: 'CryptExportKey' : undeclared identifier
c:\documents and settings\administrator\桌面\main\main.cpp(58) : error C2065: 'SIMPLEBLOB' : undeclared identifier
c:\documents and settings\administrator\桌面\main\main.cpp(58) : error C2065: 'dwKeyBlobLen' : undeclared identifier
c:\documents and settings\administrator\桌面\main\main.cpp(63) : error C2065: 'pbKeyBlob' : undeclared identifier
c:\documents and settings\administrator\桌面\main\main.cpp(72) : error C2065: 'CryptDestroyKey' : undeclared identifier
c:\documents and settings\administrator\桌面\main\main.cpp(89) : error C2065: 'CryptCreateHash' : undeclared identifier
c:\documents and settings\administrator\桌面\main\main.cpp(89) : error C2065: 'CALG_MD5' : undeclared identifier
c:\documents and settings\administrator\桌面\main\main.cpp(94) : error C2065: 'CryptHashData' : undeclared identifier
c:\documents and settings\administrator\桌面\main\main.cpp(99) : error C2065: 'CryptDeriveKey' : undeclared identifier
c:\documents and settings\administrator\桌面\main\main.cpp(103) : error C2065: 'CryptDestroyHash' : undeclared identifier
c:\documents and settings\administrator\桌面\main\main.cpp(123) : error C2065: 'CryptEncrypt' : undeclared identifier
c:\documents and settings\administrator\桌面\main\main.cpp(144) : error C2065: 'CryptReleaseContext' : undeclared identifier
c:\documents and settings\administrator\桌面\main\main.cpp(153) : error C2146: syntax error : missing ';' before identifier 'hCryptProv'
c:\documents and settings\administrator\桌面\main\main.cpp(154) : error C2146: syntax error : missing ';' before identifier 'hKey'
c:\documents and settings\administrator\桌面\main\main.cpp(155) : error C2146: syntax error : missing ';' before identifier 'hHash'
c:\documents and settings\administrator\桌面\main\main.cpp(189) : error C2065: 'CryptImportKey' : undeclared identifier
c:\documents and settings\administrator\桌面\main\main.cpp(218) : error C2065: 'CryptDecrypt' : undeclared identifier
Error executing cl.exe.Test.exe - 35 error(s), 0 warning(s)