#include<iostream> #include <Windows.h>
using namespace std;
BOOL GenerateKey(BYTE **ppPublicKey, DWORD *pdwPublicKeyLength, BYTE **ppPrivateKey, DWORD *pdwPrivateKeyLength) { BOOL bRet = TRUE; HCRYPTPROV hCryptProv = NULL; HCRYPTKEY hCryptKey = NULL; BYTE *pPublicKey = NULL; DWORD dwPublicKeyLength = 0; BYTE *pPrivateKey = NULL; DWORD dwPrivateKeyLength = 0;
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, 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, dwPrivateKeyLength); bRet = CryptExportKey(hCryptKey, NULL, PRIVATEKEYBLOB, 0, pPrivateKey, &dwPrivateKeyLength); if (FALSE == bRet) break;
*ppPublicKey = pPublicKey; *pdwPublicKeyLength = dwPublicKeyLength; *ppPrivateKey = pPrivateKey; *pdwPrivateKeyLength = dwPrivateKeyLength;
} while (FALSE);
if (hCryptKey) CryptDestroyKey(hCryptKey); if (hCryptProv) CryptReleaseContext(hCryptProv, 0); return bRet; }
BOOL RsaEncrypt(BYTE *pPublicKey, DWORD dwPublicKeyLength, BYTE *pData, DWORD &dwDataLength, DWORD dwBufferLength) { BOOL bRet = TRUE; HCRYPTPROV hCryptProv = NULL; HCRYPTKEY hCryptKey = NULL;
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 (hCryptKey) CryptDestroyKey(hCryptKey); if (hCryptProv) CryptReleaseContext(hCryptProv, 0); return bRet; }
BOOL RsaDecrypt(BYTE *pPrivateKey, DWORD dwProvateKeyLength, BYTE *pData, DWORD &dwDataLength) { BOOL bRet = TRUE; HCRYPTPROV hCryptProv = NULL; HCRYPTKEY hCryptKey = NULL;
do { bRet = CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0); if (FALSE == bRet) break;
bRet = CryptImportKey(hCryptProv, pPrivateKey, dwProvateKeyLength, NULL, 0, &hCryptKey); if (FALSE == bRet) break;
bRet = CryptDecrypt(hCryptKey, NULL, TRUE, 0, pData, &dwDataLength); if (FALSE == bRet) break; } while (FALSE);
if (hCryptKey) CryptDestroyKey(hCryptKey); if (hCryptProv) CryptReleaseContext(hCryptProv, 0); return bRet; }
int main(int argc, char * argv[]) { BYTE *pPublicKey = NULL; DWORD dwPublicKeyLength = 0; BYTE *pPrivateKey = NULL; DWORD dwPrivateKeyLength = 0; BYTE *pData = NULL; DWORD dwDataLength = 0; DWORD dwBufferLength = 4096;
pData = new BYTE[dwBufferLength];
RtlZeroMemory(pData, dwBufferLength); lstrcpy((char *)pData, "hello lyshark"); dwDataLength = 1 + lstrlen((char *)pData);
printf("加密前原始数据: "); for (int i = 0; i < dwDataLength; i++) printf("%x", pData[i]); printf("\n\n");
GenerateKey(&pPublicKey, &dwPublicKeyLength, &pPrivateKey, &dwPrivateKeyLength); printf("公钥: "); for (int i = 0; i < dwPublicKeyLength; i++) printf("%.2x", pPublicKey[i]); printf("\n\n");
printf("私钥: "); for (int i = 0; i < dwPrivateKeyLength; i++) printf("%.2x", pPrivateKey[i]); printf("\n\n");
RsaEncrypt(pPublicKey, dwPublicKeyLength, pData, dwDataLength, dwBufferLength); printf("公钥加密: "); for (int i = 0; i < dwDataLength; i++) printf("%x", pData[i]); printf("\n\n");
RsaDecrypt(pPrivateKey, dwPrivateKeyLength, pData, dwDataLength); printf("私钥解密: "); for (int i = 0; i < dwDataLength; i++) printf("%x", pData[i]); printf("\n\n");
delete[]pData; delete[]pPrivateKey; delete[]pPublicKey;
system("pause"); return 0; }
|