#include "stdafx.h" #include <Windows.h> #include <WinCrypt.h> #define RSA_FROM_FILE 1 BOOL RSAGenKeys(PVOID &lpKeyPublic, PVOID &lpKeyPrivate, WORD KeyLength) { BOOL IsOk; HCRYPTPROV CryptContext; IsOk = FALSE; lpKeyPublic = NULL; lpKeyPrivate = NULL; if (CryptAcquireContext(&CryptContext, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET) || CryptAcquireContext(&CryptContext, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0)) { HCRYPTKEY KeyContext; if (CryptGenKey(CryptContext, AT_KEYEXCHANGE, MAKELONG(CRYPT_EXPORTABLE, KeyLength), &KeyContext)) { DWORD cbKeyPrivate, cbKeyPublic; if (CryptExportKey(KeyContext, NULL, PUBLICKEYBLOB, 0, NULL, &cbKeyPublic) && CryptExportKey(KeyContext, NULL, PRIVATEKEYBLOB, 0, NULL, &cbKeyPrivate)) { lpKeyPublic = HeapAlloc(GetProcessHeap(), 0, cbKeyPublic); lpKeyPrivate = HeapAlloc(GetProcessHeap(), 0, cbKeyPrivate); if (CryptExportKey(KeyContext, NULL, PUBLICKEYBLOB, 0, PBYTE(lpKeyPublic), &cbKeyPublic) && CryptExportKey(KeyContext, NULL, PRIVATEKEYBLOB, 0, PBYTE(lpKeyPrivate), &cbKeyPrivate)) { IsOk = TRUE; } else { HeapFree(GetProcessHeap(), 0, lpKeyPublic); HeapFree(GetProcessHeap(), 0, lpKeyPrivate); } } CryptDestroyKey(KeyContext); } CryptReleaseContext(CryptContext, 0); } return IsOk; } #define CCRYPT_CONTAINER TEXT("CCryptProvider.ContainerX") int _tmain(int argc, _TCHAR* argv[]) { PVOID lpKeyPublic, lpKeyPrivate; #if RSA_FROM_FILE HCRYPTPROV CryptContext; lpKeyPublic = HeapAlloc(GetProcessHeap(), 0, 0x94); lpKeyPrivate = HeapAlloc(GetProcessHeap(), 0, 0x254); if (FILE *f = fopen("pub.key", "rb")) { fread(lpKeyPublic, 0x94, 1, f); fclose(f); } if (FILE *f = fopen("pri.key", "rb")) { fread(lpKeyPrivate, 0x254, 1, f); fclose(f); } if (CryptAcquireContext(&CryptContext, CCRYPT_CONTAINER, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_NEWKEYSET) || CryptAcquireContext(&CryptContext, CCRYPT_CONTAINER, MS_ENHANCED_PROV, PROV_RSA_FULL, 0)) { HCRYPTKEY KeyPublic; HCRYPTKEY KeyPrivate; if (CryptImportKey(CryptContext, PBYTE(lpKeyPublic), HeapSize(GetProcessHeap(), 0, lpKeyPublic), NULL, 0, &KeyPublic)) { if (CryptImportKey(CryptContext, PBYTE(lpKeyPrivate), HeapSize(GetProcessHeap(), 0, lpKeyPrivate), NULL, 0, &KeyPrivate)) { BYTE buf1[128]; DWORD dwbuf; memset(buf1, 0x90, sizeof(buf1)); dwbuf = 64; if (CryptEncrypt(KeyPublic, NULL, TRUE, 0, buf1, &dwbuf, sizeof(buf1))) { if (CryptDecrypt(KeyPrivate, NULL, TRUE, 0, buf1, &dwbuf)) { _tprintf(TEXT("encryption with RSA public keys.\n")); } } memset(buf1, 0x90, sizeof(buf1)); dwbuf = 64; if (CryptEncrypt(KeyPrivate, NULL, TRUE, 0, buf1, &dwbuf, sizeof(buf1))) { if (CryptDecrypt(KeyPublic, NULL, TRUE, 0, buf1, &dwbuf)) { _tprintf(TEXT("encryption with RSA private keys.\n")); } } CryptDestroyKey(KeyPrivate); } CryptDestroyKey(KeyPublic); } CryptReleaseContext(CryptContext, 0); } HeapFree(GetProcessHeap(), 0, lpKeyPublic); HeapFree(GetProcessHeap(), 0, lpKeyPrivate); #else if (RSAGenKeys(lpKeyPublic, lpKeyPrivate, 1024)) { if (FILE *f = fopen("pub.key", "wb")) { fwrite(lpKeyPublic, HeapSize(GetProcessHeap(), 0, lpKeyPublic), 1, f); fclose(f); } if (FILE *f = fopen("pri.key", "wb")) { fwrite(lpKeyPrivate, HeapSize(GetProcessHeap(), 0, lpKeyPrivate), 1, f); fclose(f); } HeapFree(GetProcessHeap(), 0, lpKeyPublic); HeapFree(GetProcessHeap(), 0, lpKeyPrivate); } #endif _gettchar(); return 0; }