1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
| BOOL GenerateKey(PBYTE* ppPublicKey, PDWORD pdwPublicKeyLength, PBYTE* ppPrivateKey, PDWORD pdwPrivateKeyLength) { HCRYPTPROV hCryptProv = NULL; HCRYPTKEY hCryptKey = NULL; DWORD dwPublicKeyLength = 0, dwPrivateKeyLength = 0; PBYTE pPublicKey = NULL, pPrivateKey = NULL; BOOL bRet = FALSE; do { bRet = ::CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0); if (FALSE == bRet) break; bRet = ::CryptGenKey(hCryptProv, AT_KEYEXCHANGE, CRYPT_EXPORTABLE, &hCryptKey); if (FALSE == bRet) break; bRet = ::CryptExportKey(hCryptKey, NULL, PUBLICKEYBLOB, 0, NULL, &dwPublicKeyLength); if (FALSE == bRet) break; pPublicKey = new BYTE[dwPublicKeyLength]; ::RtlZeroMemory(pPublicKey, NULL, PUBLICKEYBLOB, 0, pPublicKey, &dwPublicKeyLength); bRet = ::CryptExportKey(hCryptKey, NULL, PUBLICKEYBLOB, 0, pPublicKey, &dwPublicKeyLength); if (FALSE == bRet) break; bRet = ::CryptExportKey(hCryptKey, NULL, PRIVATEKEYBLOB, 0, NULL, &dwPrivateKeyLength); if (FALSE == bRet) break; pPrivateKey = new BYTE[dwPrivateKeyLength]; ::RtlZeroMemory(pPrivateKey, NULL, PRIVATEKEYBLOB, 0, pPrivateKey, &dwPrivateKeyLength); bRet = ::CryptExportKey(hCryptKey, NULL, PRIVATEKEYBLOB, 0, pPrivateKey, &dwPrivateKeyLength); if (FALSE == bRet) break; bRet = TRUE; *ppPublicKey = pPublicKey; *pdwPublicKeyLength = dwPublicKeyLength; *ppPrivateKey = pPrivateKey; *pdwPrivateKeyLength = dwPrivateKeyLength; } while (FALSE); if (hCryptKey != NULL) { ::CryptDestroyKey(hCryptKey); hCryptKey = NULL; }; if (hCryptProv != NULL) { ::CryptReleaseContext(hCryptProv, 0); hCryptProv = NULL; }; if (pPublicKey != NULL) { delete[]pPublicKey; pPublicKey = NULL; }; if (pPrivateKey != NULL) { delete[]pPrivateKey; pPrivateKey = NULL; }; return bRet; }; BOOL RsaEncrypt(PBYTE pPublicKey, DWORD dwPublicKeyLength, PBYTE pData, DWORD& dwDataLength, DWORD dwBufferLength) { HCRYPTPROV hCryptProv = NULL; HCRYPTKEY hCryptKey = NULL; BOOL bRet = FALSE; do { bRet = ::CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0); if (FALSE == bRet) break; bRet = ::CryptImportKey(hCryptProv, pPublicKey, dwPublicKeyLength, NULL, 0, &hCryptKey); if (FALSE == bRet) break; bRet = ::CryptEncrypt(hCryptKey, NULL, TRUE, 0, pData, &dwDataLength, dwBufferLength); if (FALSE == bRet) break; } while (FALSE); if (hCryptProv != NULL) { ::CryptReleaseContext(hCryptProv, 0); hCryptProv = NULL; }; if (hCryptKey != NULL) { ::CryptDestroyKey(hCryptKey); hCryptKey = NULL; }; return bRet; }; BOOL RsaDecrypt(PBYTE pPrivateKey, DWORD dwPrivateKeyLength, PBYTE pData, DWORD& dwDataLength, DWORD dwBufferLength) { HCRYPTPROV hCryptProv = NULL; HCRYPTKEY hCryptKey = NULL; BOOL bRet = FALSE; do { bRet = ::CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0); if (FALSE == bRet) break; bRet = ::CryptImportKey(hCryptProv, pPrivateKey, dwPrivateKeyLength, NULL, 0, &hCryptKey); if (FALSE == bRet) break; bRet = ::CryptEncrypt(hCryptKey, NULL, TRUE, 0, pData, &dwDataLength, dwBufferLength); if (FALSE == bRet) break; } while (FALSE); if (hCryptProv != NULL) { ::CryptReleaseContext(hCryptProv, 0); hCryptProv = NULL; }; if (hCryptKey != NULL) { ::CryptDestroyKey(hCryptKey); hCryptKey = NULL; }; return bRet; };
|