21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Once a provider context has been successfully acquired, you need a key. The API<br />

provides three ways to obtain a key object, which is stored by CryptoAPI as an<br />

opaque object to which you’ll have only a handle:<br />

CryptGenKey( )<br />

Generates a random key.<br />

CryptDeriveKey( )<br />

Derives a key from a password or passphrase.<br />

CryptImportKey( )<br />

Creates a key object from key data in a buffer.<br />

All three functions return a new key object that keeps the key data hidden and has<br />

associated with it a symmetric encryption algorithm and a set of flags that control the<br />

behavior of the key. The key data can be obtained from the key object using<br />

CryptExportKey( ) if the key object allows it. The CryptExportKey( ) and<br />

CryptImportKey( ) functions provide the means for exchanging keys.<br />

The CryptExportKey( ) function will only allow you to export a symmetric<br />

encryption key encrypted with another key. For maximum<br />

portability across all versions of Windows, a public key should be<br />

used. However, Windows 2000 introduced the ability to encrypt the<br />

symmetric encryption key with another symmetric encryption key.<br />

Similarly, CryptImportKey( ) can only import symmetric encryption<br />

keys that are encrypted.<br />

If you need the raw key data, you must first export the key in<br />

encrypted form, then decrypt from it (see Recipe 5.27). While this may<br />

seem like a lot of extra work, the reason is that CryptoAPI was<br />

designed with the goal of making it very difficult (if not impossible) to<br />

unintentionally disclose sensitive information.<br />

Generating a new key with CryptGenKey( ) that can be exported is very simple, as<br />

illustrated in the following code. If you don’t want the new key to be exportable,<br />

simply remove the CRYPT_EXPORTABLE flag.<br />

HCRYPTKEY SpcGetRandomKey(HCRYPTPROV hProvider, ALG_ID Algid, DWORD dwSize) {<br />

DWORD dwFlags;<br />

HCRYPTKEY hKey;<br />

dwFlags = ((dwSize

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!