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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
#include <iostream>
#include <fstream>
#include <windows.h>
#include <Wincrypt.h>
using namespace std;
int main()
{
srand( time ( NULL ) - 3 );
HCRYPTPROV hCryptProv = NULL;
BYTE pbData[16];
LPCSTR UserName = "MyKeyContainer";
cout << "lpcstr = " << UserName << endl;
cout << hCryptProv << endl << endl;
for (int i = 0; i < 17; ++i)
{
cout << pbData[i] << " ";
}cout << endl << endl << endl;
if( CryptAcquireContext( &hCryptProv, // handle to the CSP
UserName, // container name
NULL, // use the default provider
PROV_RSA_FULL, // provider type
0)) // flag values
{
printf("A cryptographic context with the %s key container \n", UserName);
printf("has been acquired.\n\n");
}
else
{
//-------------------------------------------------------------------
// An error occurred in acquiring the context. This could mean
// that the key container requested does not exist. In this case,
// the function can be called again to attempt to create a new key
// container. Error codes are defined in Winerror.h.
if (GetLastError() == NTE_BAD_KEYSET)
{
if(CryptAcquireContext(
&hCryptProv,
UserName,
NULL,
PROV_RSA_FULL,
CRYPT_NEWKEYSET))
{
printf("A new key container has been created.\n");
}
else
{
printf("Could not create a new key container.\n");
exit(1);
}
}
else
{
printf("A cryptographic service handle could not be "
"acquired.\n");
exit(1);
}
} // End of else.
//-------------------------------------------------------------------
// A cryptographic context and a key container are available. Perform
// any functions that require a cryptographic provider handle.
//-------------------------------------------------------------------
// When the handle is no longer needed, it must be released.
/*if (CryptReleaseContext(hCryptProv,0))
{
printf("The handle has been released.\n");
}
else
{
printf("The handle could not be released.\n");
}*/
cout << "username = " << UserName << " " << endl;
cout << "pbdata is now " << pbData << " " << endl;
if (CryptGenRandom ( hCryptProv, 8, pbData ) )
{
printf("Random sequence generated. \n");
}
else
{
printf("Error during CryptGenRandom.\n");
cout << "now " << hCryptProv << " " << endl;
exit(1);
}
cout << "pbdata is now " << pbData << " " << endl;
cout << "pbdata is now " << pbData << " " << endl;
cout << "hCryptProv is " << hCryptProv << " " << endl;
HCRYPTKEY hKey;
ALG_ID ENCRYPT_ALGORITHM = PROV_RSA_FULL;
DWORD KEYLENGTH = 3;
if(CryptGenKey(
hCryptProv,
ENCRYPT_ALGORITHM,
KEYLENGTH | CRYPT_EXPORTABLE,
&hKey))
{
printf("A session key has been created.\n");
}
else
{
printf("Error during CryptGenKey.\n");
exit(1);
}
//-------------------------------------------------------------------
// The key created can be exported into a key BLOB that can be
// written to a file.
// ...
// When you have finished using the key, free the resource.
cout << "hKey = " << hKey << " " << endl << endl << endl;
if (!CryptDestroyKey(hKey))
{
printf("Error during CryptDestroyKey.\n");
exit(1);
}
cout << "hkey = " << hKey << " " << endl << endl;
return 0;
}
|