예제 C 프로그램: 세션 키 내보내기
다음 예제에서는 임의 세션 키를 만들고 내보낼 수 있는 키 BLOB만듭니다. 이 예제에서는 CryptGetUserKey, CryptExportKey및 관련 함수를 사용하는 방법을 보여 줍니다.
이 예제에서는 다음 작업 및 CryptoAPI 함수를 보여 줍니다.
- CryptAcquireContext사용하여 CSP 컨텍스트를 가져옵니다.
- CryptGetUserKey사용하여 두 개의 서로 다른 퍼블릭/프라이빗 키 쌍에 대한 액세스 권한을 얻습니다.
- CryptGenKey사용하여 내보낼 수 있는 세션 키를 생성합니다.
- CryptExportKey사용하여 세션 키가 포함된 간단한 키 BLOB 만듭니다.
- CryptDestroyKey사용하여 세션 키 및 두 쌍의 퍼블릭/프라이빗 키에 대한 액세스 권한을 삭제합니다.
- CryptReleaseContext사용하여 CSP 컨텍스트를 해제합니다.
이 예제에서는 MyHandleError함수를 사용합니다. 이 함수의 코드는 샘플에 포함되어 있습니다. 이 함수 및 기타 보조 함수에 대한 코드도 범용 함수 아래에 나열됩니다.
//--------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// This example program creates a session key and a simple key
// BLOB holding that session key. The key BLOB can be written to disk
// and later read from a disk file.
#pragma comment(lib, "crypt32.lib")
#include <stdio.h>
#include <windows.h>
#include <Wincrypt.h>
#define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
void MyHandleError(char *s);
void main(void)
{
//--------------------------------------------------------------------
// Declare and initialize variables.
HCRYPTPROV hProv; // CSP handle
HCRYPTKEY hSignKey; // Signature key pair handle
HCRYPTKEY hXchgKey; // Exchange key pair handle
HCRYPTKEY hKey; // Session key handle
BYTE *pbKeyBlob; // Pointer to a simple key BLOB
DWORD dwBlobLen; // The length of the key BLOB
//--------------------------------------------------------------------
// Acquire a cryptographic provider context handle.
if(CryptAcquireContext(
&hProv,
NULL,
NULL,
PROV_RSA_FULL,
0))
{
printf("The CSP has been acquired. \n");
}
else
{
MyHandleError("Error during CryptAcquireContext.");
}
//--------------------------------------------------------------------
// Get a handle to the signature key.
// The signature key must exist before it can be retrieved. For more
// information, see the CryptGetUserKey documentation.
if(CryptGetUserKey(
hProv,
AT_SIGNATURE,
&hSignKey))
{
printf("The signature key has been acquired. \n");
}
else
{
MyHandleError("Error during CryptGetUserKey for signkey.");
}
//--------------------------------------------------------------------
// Get a handle to the key exchange key.
// The key must exist before it can be retrieved. For more
// information, see the CryptGetUserKey documentation.
if(CryptGetUserKey(
hProv,
AT_KEYEXCHANGE,
&hXchgKey))
{
printf("The key exchange key has been acquired. \n");
}
else
{
printf("Error during CryptGetUserKey exchange key.");
}
// hSignKey may be used to verify a signature. hXchgKey will be used
// to export a session key.
//--------------------------------------------------------------------
// Generate a session key.
if (CryptGenKey(
hProv,
CALG_RC4,
CRYPT_EXPORTABLE,
&hKey))
{
printf("Original session key is created. \n");
}
else
{
MyHandleError("ERROR -- CryptGenKey.");
}
// Determine the size of the key BLOB and allocate memory.
if(CryptExportKey(
hKey,
hXchgKey,
SIMPLEBLOB,
0,
NULL,
&dwBlobLen))
{
printf("Size of the BLOB for the session key determined. \n");
}
else
{
MyHandleError("Error computing BLOB length.");
}
if(pbKeyBlob = (BYTE*)malloc(dwBlobLen))
{
printf("Memory has been allocated for the BLOB. \n");
}
else
{
MyHandleError("Out of memory. \n");
}
//--------------------------------------------------------------------
// Export the key into a simple key BLOB.
if(CryptExportKey(
hKey,
hXchgKey,
SIMPLEBLOB,
0,
pbKeyBlob,
&dwBlobLen))
{
printf("Contents have been written to the BLOB. \n");
}
else
{
MyHandleError("Error during CryptExportKey.");
}
//--------------------------------------------------------------------
// At this point, other processing such as writing the key BLOB to
// a file could be done.
//--------------------------------------------------------------------
// After all processing, clean up.
//--------------------------------------------------------------------
// Free the memory used by the key BLOB.
free(pbKeyBlob);
// Destroy the session key.
if(hKey)
CryptDestroyKey(hKey);
// Destroy the signature key handle.
if(hSignKey)
CryptDestroyKey(hSignKey);
// Destroy the key exchange key handle.
if(hXchgKey)
CryptDestroyKey(hXchgKey);
// Release the provider handle.
if(hProv)
CryptReleaseContext(hProv, 0);
printf("The program ran to completion without error. \n");
}// End of main
//--------------------------------------------------------------------
// This example uses the function MyHandleError, a simple error
// handling function, to print an error message and exit
// the program.
// For most applications, replace this function with one
// that does more extensive error reporting.
void MyHandleError(char *s)
{
printf("An error occurred in running the program.\n");
printf("%s\n",s);
printf("Error number %x\n.",GetLastError());
printf("Program terminating.\n");
exit(1);
}