A tarefa é esta: Exibe a lista de provedores de criptografia instalados na tela. Escreva um teste da velocidade de criptografia do provedor de criptografia da Microsoft subjacente.
Acontece que sempre ele apresenta certos erros que não consigo entender o porque!
#include <iostream>
#include <ctime>
#include <tchar.h>
#include <stdio.h>
#include <windows.h>
#include <wincrypt.h>
#include <combaseapi.h>
#pragma comment(lib, "advapi32.lib")
using namespace std;
#define KEYLENGTH 0x00800000
#define ENCRYPT_ALGORITHM CALG_RC4
#define ENCRYPT_BLOCK_SIZE 8
HANDLE hSourceFile = INVALID_HANDLE_VALUE; // data handle
HANDLE hKeyFile = INVALID_HANDLE_VALUE; // key handle
HANDLE hDestinationFile = INVALID_HANDLE_VALUE; // write handle
HCRYPTPROV hCryptProv = NULL; // cryptoprovide descriptor
HCRYPTKEY hKey = NULL; // key descriptor
HCRYPTKEY hXchgKey = NULL;
PBYTE pbBuffer = NULL; // file buffer
DWORD dwBlockLen;
DWORD dwBufferLen;
DWORD dwCount;
PBYTE pbKeyBlob = NULL;
DWORD dwKeyBlobLen;
clock_t t; // timer
int main() {
DWORD size;
DWORD type;
DWORD index=0;
char* name;
cout << "1. List cryptoproviders: " << endl;
while( CryptEnumProviders(index, 0, 0, &type, 0, &size) )
{
name = new char[size];
CryptEnumProviders(index, 0, 0, &type, name, &size);
printf(" %d %d %s'n", index + 1, type, name);
index++;
delete[] name;
}
cout << endl << endl << "2. Speed encription test: " << endl;
// file opening
hSourceFile = CreateFile("input.txt", FILE_READ_DATA, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hSourceFile){
printf("ERROR opening input file 'n");
system("Pause");
return 0;
}
hDestinationFile = CreateFile("output.txt", FILE_WRITE_DATA, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hDestinationFile) {
printf("Erorr open output file 'n");
system("Pause");
return 0;
}
hKeyFile = CreateFile( "key.txt" , FILE_WRITE_DATA, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hKeyFile) {
printf("Erorr open key file 'n 'n");
system("Pause");
return 0;
}
//dwBufferLen = GetFileSize(hSourceFile, NULL);
//pbBuffer = (BYTE *)malloc(dwBufferLen);
//ReadFile(hSourceFile, pbBuffer, dwBufferLen, &dwCount, NULL);
t = clock();
//---------------------------------------------------------------------------------- //
// Initialization of cryptoprovider
cout << " Create provider" << endl;
if (!CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0)) {
printf(" Provider is not initialized............ERROR!'n");
cout <<" " << GetLastError() << endl;
system("Pause");
return 0;
} else {
printf(" Provider is initialized...................Ok'n'n");
}
//---------------------------------------------------------------------------------- //
printf(" Key BLOB has been written to the file.....Ok 'n");
}
bool fEOF = FALSE;
do {
// Read up to dwBlockLen bytes from the source file.
if (!ReadFile(hSourceFile, pbBuffer, dwBlockLen, &dwCount, NULL)) {
printf("Error reading plaintext!'n");
break;
}
if (dwCount < dwBlockLen) { fEOF = TRUE; }
// Encrypt data.
if (!CryptEncrypt(hKey, NULL, fEOF, 0, pbBuffer, &dwCount, dwBufferLen)) {
printf("Error during CryptEncrypt. 'n");
break;
}
// Write the encrypted data to the destination file.
if (!WriteFile(hDestinationFile, pbBuffer, dwCount, &dwCount, NULL)) {
printf("Error writing ciphertext.'n");
break;
}
}
while (!fEOF);
float sizeFile = GetFileSize(hSourceFile, NULL);
cout << " Test speed:" << endl;
cout << " File 'input.txt'" << endl;
cout << " Size File " << sizeFile / 1024.0 / 1024.0 << " MBytes" << endl << endl;
t = clock() - t;
cout << " Encryption time " << ((float)t) / CLOCKS_PER_SEC << " seconds" << endl;
cout << " Encryption speed " << sizeFile / ((float)t / CLOCKS_PER_SEC) / 1024.0 / 1024.0 << " MByte/s" << endl << endl;
//printf("It took me %d clicks (%f seconds).'n", t, ((float)t) / CLOCKS_PER_SEC);
//printf("Speed: %f Mb/s 'n", sizeFile / ((float)t / CLOCKS_PER_SEC) / 1024.0 / 1024.0);
if (pbKeyBlob) { free(pbKeyBlob); }
if (pbBuffer) { free(pbBuffer); }
if (hKeyFile) { CloseHandle(hKeyFile); }
if (hSourceFile) { CloseHandle(hSourceFile); }
if (hDestinationFile) { CloseHandle(hDestinationFile); }
// Release the session key.
if (hKey) {
if (!(CryptDestroyKey(hKey))) {
printf("Error during CryptDestroyKey!'n");
cout << " " << GetLastError() << endl;
system("Pause");
return 0;
}
}
// Release the provider handle.
if (hCryptProv) {
if (!(CryptReleaseContext(hCryptProv, 0))) {
printf("Error during CryptReleaseContext!'n");
cout << " " << GetLastError() << endl;
system("Pause");
return 0;
}
}
system("Pause");
return 0;
}