共用方式為


Diffie-Hellman 金鑰

產生 Diffie-Hellman 金鑰

若要產生 Diffie-Hellman 金鑰,請執行下列步驟:

  1. 呼叫 cryptAcquireContext函式,以取得Microsoft Diffie-Hellman 密碼編譯提供者的句柄。

  2. 產生新的金鑰。 有兩種方式可以達成此目的:讓 CryptoAPI 產生 G、P 和 X 的所有新值,或使用 G 和 P 的現有值,並為 X 產生新的值。

    產生所有新值來產生索引鍵

    1. 呼叫 CryptGenKey 函式,在 Algid 參數中傳遞 CALG_DH_SF(儲存和轉寄)或 CALG_DH_EPHEM(暫時)。 索引鍵會使用 G 和 P 的新隨機值、X 的新計算值,並在 phKey 參數中傳回其句柄。
    2. 新的金鑰現在已可供使用。 執行 金鑰交換時,G 和 P 的值必須連同金鑰一起傳送給收件者(或由其他方法傳送)。

    使用 G 和 P 預先定義的值來產生索引鍵

    1. 呼叫 CryptGenKeyAlgid 參數中傳遞 CALG_DH_SF(儲存和轉寄)或 CALG_DH_EPHEMCALG_DH_EPHEMCRYPT_PREGENdwFlags 參數。 密鑰句柄將會在 phKey 參數中產生並傳回。
    2. 使用設定為 P 值的 pbData 成員,初始化 CRYPT_DATA_BLOB 結構。 BLOB 不包含標頭資訊,且 pbData 成員的格式
    3. P 的值是藉由呼叫 CryptSetKeyParam 函式來設定,傳遞 hKey 參數中步驟 a 中擷取的密鑰句柄、dwParam 參數中的 KP_P 旗標,以及包含 pbData 參數中 P 值的指標。
    4. 使用設定為 G 值的 pbData 成員,初始化 CRYPT_DATA_BLOB 結構。 BLOB 不包含標頭資訊,而且 pbData 成員的格式為小數。
    5. G 的值是藉由呼叫 CryptSetKeyParam 函式來設定,傳遞 hKey 參數中步驟 a 中擷取的密鑰句柄、dwParam 參數中的 KP_G 旗標,以及包含 pbData 參數中 G 值的結構指標。
    6. X 的值是藉由呼叫 CryptSetKeyParam 函式來產生,傳遞 hKey 參數中步驟 a 中擷取的密鑰句柄、dwParam 參數中的 KP_X 旗標,以及在 pbData 參數中傳遞 NULL
    7. 如果所有函式呼叫都成功,則 Diffie-Hellman 公鑰已可供使用。
  3. 不再需要密鑰時,請將密鑰句柄傳遞至 CryptDestroyKey 函式來終結它。

如果在先前程式中指定 CALG_DH_SF,索引鍵值會儲存至記憶體,每次呼叫 cryptSetKeyParam 。 接著可以使用 cryptGetKeyParam函式來擷取 G 和 P 值。 某些 CSP 可能有硬式編碼的 G 和 P 值。 在此情況下,如果使用 dwParam 參數中指定的 KP_GKP_P 呼叫 CryptSetKeyParam,則會傳回 NTE_FIXEDPARAMETER 錯誤。 如果呼叫 CryptDestroyKey,則會終結密鑰的句柄,但密鑰值會保留在 CSP 中。 不過,如果指定了 CALG_DH_EPHEM,則會終結密鑰的句柄,而且會從 CSP 清除所有值。

交換 Diffie-Hellman 金鑰

Diffie-Hellman 演算法的目的是要透過不安全的網路共享資訊,讓兩個以上的合作對象能夠建立和共用相同的秘密會話密鑰。 透過網路共用的資訊形式為數個常數值和 Diffie-Hellman 公鑰。 兩個金鑰交換者所使用的程式如下所示:

  • 雙方都同意 Diffie-Hellman 參數,這些參數是質數(P)和產生器編號(G)。
  • 第一方將其 Diffie-Hellman 公鑰傳送給第二方。
  • 合作物件 2 會使用其私鑰和合作物件 1 公鑰中包含的資訊來計算秘密會話金鑰。
  • 第 2 方將其 Diffie-Hellman 公鑰傳送給第 1 方。
  • 合作物件 1 會使用其私鑰和第 2 方公鑰中包含的資訊來計算秘密會話金鑰。
  • 雙方現在都有相同的會話密鑰,可用於加密和解密數據。 下列程序顯示此作業所需的步驟。

準備傳輸 Diffie-Hellman 公鑰

  1. 呼叫 cryptAcquireContext函式,以取得Microsoft Diffie-Hellman 密碼編譯提供者的句柄。
  2. 呼叫 CryptGenKey 函式來建立新的金鑰,或呼叫 CryptGetUserKey 函式來擷取現有的金鑰,以建立 Diffie-Hellman 密鑰。
  3. 藉由呼叫 CryptExportKey來取得保存 Diffie-Hellman 密鑰 BLOB 所需的大小,並傳遞 pbData 參數的 NULL。 必要的大小會在 pdwDataLen中傳回。
  4. 配置金鑰 BLOB 的記憶體。
  5. 藉由呼叫 CryptExportKey 函式,將 dwBlobType 參數中的 PUBLICKEYBLOB,以及 hKey 參數中的 Diffie-Hellman 密鑰句柄,建立 Diffie-Hellman 公鑰 BLOB。 此函式呼叫會導致公鑰值 (G^X) mod P 的計算。
  6. 如果上述所有函式呼叫都成功,則 Diffie-Hellman 公鑰 BLOB 現在已準備好進行編碼和傳輸。

匯入 Diffie-Hellman 公鑰並計算秘密會話金鑰

  1. 呼叫 cryptAcquireContext函式,以取得Microsoft Diffie-Hellman 密碼編譯提供者的句柄。
  2. 呼叫 CryptGenKey 函式來建立新的金鑰,或呼叫 CryptGetUserKey 函式來擷取現有的金鑰,以建立 Diffie-Hellman 密鑰。
  3. 若要將 Diffie-Hellman 公鑰匯入 CSP,請呼叫 CryptImportKey 函式,將指標傳遞至 pbData 參數中的公鑰 BLOB、dwDataLen 參數中的 BLOB 長度,以及 hPubKey 參數中 Diffie-Hellman 密鑰的句柄。 這會導致執行計算 (Y^X) mod P,從而建立共用的秘密密鑰,並完成 金鑰交換。 此函式呼叫會將句柄傳回 hKey 參數中的新秘密會話密鑰。
  4. 此時,匯入 Diffie-Hellman 的類型為 CALG_AGREEDKEY_ANY。 使用金鑰之前,它必須轉換成會話密鑰類型。 藉由呼叫 CryptSetKeyParam 函式,將 dwParam 設為 KP_ALGID,並將 pbData 設為代表會話索引鍵之 ALG_ID 值的指標,例如 CALG_RC4。 在 cryptEncryptCryptDecrypt 函式中使用共用密鑰之前,必須先轉換金鑰。 轉換金鑰類型之前,對這些函式的其中一個呼叫將會失敗。
  5. 秘密會話金鑰現在已準備好用於加密或解密。
  6. 不再需要密鑰時,呼叫 CryptDestroyKey 函式來終結密鑰句柄。

匯出 Diffie-Hellman 私鑰

若要匯出 Diffie-Hellman 私鑰,請執行下列步驟:

  1. 呼叫 cryptAcquireContext函式,以取得Microsoft Diffie-Hellman 密碼編譯提供者的句柄。
  2. 呼叫 CryptGenKey 函式來建立新的金鑰,或呼叫 CryptGetUserKey 函式來擷取現有的金鑰,以建立 Diffie-Hellman 密鑰。
  3. 藉由呼叫 CryptExportKey 函式,在 dwBlobType 參數中傳遞 PRIVATEKEYBLOB,並將句柄傳遞至 hKey 參數中的 Diffie-Hellman 密鑰,以建立 Diffie-Hellman 私鑰 BLOB
  4. 不再需要密鑰句柄時,請呼叫 CryptDestroyKey 函式來終結密鑰句柄。

範例程序代碼

下列範例示範如何建立、匯出、匯入及使用 Diffie-Hellman 密鑰來執行金鑰交換。

#include <tchar.h>
#include <windows.h>
#include <wincrypt.h>
#pragma comment(lib, "crypt32.lib")

// The key size, in bits.
#define DHKEYSIZE 512

// Prime in little-endian format.
static const BYTE g_rgbPrime[] = 
{
    0x91, 0x02, 0xc8, 0x31, 0xee, 0x36, 0x07, 0xec, 
    0xc2, 0x24, 0x37, 0xf8, 0xfb, 0x3d, 0x69, 0x49, 
    0xac, 0x7a, 0xab, 0x32, 0xac, 0xad, 0xe9, 0xc2, 
    0xaf, 0x0e, 0x21, 0xb7, 0xc5, 0x2f, 0x76, 0xd0, 
    0xe5, 0x82, 0x78, 0x0d, 0x4f, 0x32, 0xb8, 0xcb,
    0xf7, 0x0c, 0x8d, 0xfb, 0x3a, 0xd8, 0xc0, 0xea, 
    0xcb, 0x69, 0x68, 0xb0, 0x9b, 0x75, 0x25, 0x3d,
    0xaa, 0x76, 0x22, 0x49, 0x94, 0xa4, 0xf2, 0x8d 
};

// Generator in little-endian format.
static BYTE g_rgbGenerator[] = 
{
    0x02, 0x88, 0xd7, 0xe6, 0x53, 0xaf, 0x72, 0xc5,
    0x8c, 0x08, 0x4b, 0x46, 0x6f, 0x9f, 0x2e, 0xc4,
    0x9c, 0x5c, 0x92, 0x21, 0x95, 0xb7, 0xe5, 0x58, 
    0xbf, 0xba, 0x24, 0xfa, 0xe5, 0x9d, 0xcb, 0x71, 
    0x2e, 0x2c, 0xce, 0x99, 0xf3, 0x10, 0xff, 0x3b,
    0xcb, 0xef, 0x6c, 0x95, 0x22, 0x55, 0x9d, 0x29,
    0x00, 0xb5, 0x4c, 0x5b, 0xa5, 0x63, 0x31, 0x41,
    0x13, 0x0a, 0xea, 0x39, 0x78, 0x02, 0x6d, 0x62
};

BYTE g_rgbData[] = {0x01, 0x02, 0x03, 0x04,    0x05, 0x06, 0x07, 0x08};

int _tmain(int argc, _TCHAR* argv[])
{
    UNREFERENCED_PARAMETER(argc);
    UNREFERENCED_PARAMETER(argv);
    
    BOOL fReturn;
    HCRYPTPROV hProvParty1 = NULL; 
    HCRYPTPROV hProvParty2 = NULL; 
    DATA_BLOB P;
    DATA_BLOB G;
    HCRYPTKEY hPrivateKey1 = NULL;
    HCRYPTKEY hPrivateKey2 = NULL;
    PBYTE pbKeyBlob1 = NULL;
    PBYTE pbKeyBlob2 = NULL;
    HCRYPTKEY hSessionKey1 = NULL;
    HCRYPTKEY hSessionKey2 = NULL;
    PBYTE pbData = NULL;

    /************************
    Construct data BLOBs for the prime and generator. The P and G 
    values, represented by the g_rgbPrime and g_rgbGenerator arrays 
    respectively, are shared values that have been agreed to by both 
    parties.
    ************************/
    P.cbData = DHKEYSIZE/8;
    P.pbData = (BYTE*)(g_rgbPrime);

    G.cbData = DHKEYSIZE/8;
    G.pbData = (BYTE*)(g_rgbGenerator);

    /************************
    Create the private Diffie-Hellman key for party 1. 
    ************************/
    // Acquire a provider handle for party 1.
    fReturn = CryptAcquireContext(
        &hProvParty1, 
        NULL,
        MS_ENH_DSS_DH_PROV,
        PROV_DSS_DH, 
        CRYPT_VERIFYCONTEXT);
    if(!fReturn)
    {
        goto ErrorExit;
    }

    // Create an ephemeral private key for party 1.
    fReturn = CryptGenKey(
        hProvParty1, 
        CALG_DH_EPHEM, 
        DHKEYSIZE << 16 | CRYPT_EXPORTABLE | CRYPT_PREGEN,
        &hPrivateKey1);
    if(!fReturn)
    {
        goto ErrorExit;
    }

    // Set the prime for party 1's private key.
    fReturn = CryptSetKeyParam(
        hPrivateKey1,
        KP_P,
        (PBYTE)&P,
        0);
    if(!fReturn)
    {
        goto ErrorExit;
    }

    // Set the generator for party 1's private key.
    fReturn = CryptSetKeyParam(
        hPrivateKey1,
        KP_G,
        (PBYTE)&G,
        0);
    if(!fReturn)
    {
        goto ErrorExit;
    }

    // Generate the secret values for party 1's private key.
    fReturn = CryptSetKeyParam(
        hPrivateKey1,
        KP_X,
        NULL,
        0);
    if(!fReturn)
    {
        goto ErrorExit;
    }

    /************************
    Create the private Diffie-Hellman key for party 2. 
    ************************/
    // Acquire a provider handle for party 2.
    fReturn = CryptAcquireContext(
        &hProvParty2, 
        NULL,
        MS_ENH_DSS_DH_PROV,
        PROV_DSS_DH, 
        CRYPT_VERIFYCONTEXT);
    if(!fReturn)
    {
        goto ErrorExit;
    }

    // Create an ephemeral private key for party 2.
    fReturn = CryptGenKey(
        hProvParty2, 
        CALG_DH_EPHEM, 
        DHKEYSIZE << 16 | CRYPT_EXPORTABLE | CRYPT_PREGEN,
        &hPrivateKey2);
    if(!fReturn)
    {
        goto ErrorExit;
    }

    // Set the prime for party 2's private key.
    fReturn = CryptSetKeyParam(
        hPrivateKey2,
        KP_P,
        (PBYTE)&P,
        0);
    if(!fReturn)
    {
        goto ErrorExit;
    }

    // Set the generator for party 2's private key.
    fReturn = CryptSetKeyParam(
        hPrivateKey2,
        KP_G,
        (PBYTE)&G,
        0);
    if(!fReturn)
    {
        goto ErrorExit;
    }

    // Generate the secret values for party 2's private key.
    fReturn = CryptSetKeyParam(
        hPrivateKey2,
        KP_X,
        NULL,
        0);
    if(!fReturn)
    {
        goto ErrorExit;
    }

    /************************
    Export Party 1's public key.
    ************************/
    // Public key value, (G^X) mod P is calculated.
    DWORD dwDataLen1;

    // Get the size for the key BLOB.
    fReturn = CryptExportKey(
        hPrivateKey1,
        NULL,
        PUBLICKEYBLOB,
        0,
        NULL,
        &dwDataLen1);
    if(!fReturn)
    {
        goto ErrorExit;
    }

    // Allocate the memory for the key BLOB.
    if(!(pbKeyBlob1 = (PBYTE)malloc(dwDataLen1)))
    { 
        goto ErrorExit;
    }

    // Get the key BLOB.
    fReturn = CryptExportKey(
        hPrivateKey1,
        0,
        PUBLICKEYBLOB,
        0,
        pbKeyBlob1,
        &dwDataLen1);
    if(!fReturn)
    {
        goto ErrorExit;
    }

    /************************
    Export Party 2's public key.
    ************************/
    // Public key value, (G^X) mod P is calculated.
    DWORD dwDataLen2;

    // Get the size for the key BLOB.
    fReturn = CryptExportKey(
        hPrivateKey2,
        NULL,
        PUBLICKEYBLOB,
        0,
        NULL,
        &dwDataLen2);
    if(!fReturn)
    {
        goto ErrorExit;
    }

    // Allocate the memory for the key BLOB.
    if(!(pbKeyBlob2 = (PBYTE)malloc(dwDataLen2)))
    { 
        goto ErrorExit;
    }

    // Get the key BLOB.
    fReturn = CryptExportKey(
        hPrivateKey2,
        0,
        PUBLICKEYBLOB,
        0,
        pbKeyBlob2,
        &dwDataLen2);
    if(!fReturn)
    {
        goto ErrorExit;
    }

    /************************
    Party 1 imports party 2's public key.
    The imported key will contain the new shared secret 
    key (Y^X) mod P. 
    ************************/
    fReturn = CryptImportKey(
        hProvParty1,
        pbKeyBlob2,
        dwDataLen2,
        hPrivateKey1,
        0,
        &hSessionKey2);
    if(!fReturn)
    {
        goto ErrorExit;
    }
    
    /************************
    Party 2 imports party 1's public key.
    The imported key will contain the new shared secret 
    key (Y^X) mod P. 
    ************************/
    fReturn = CryptImportKey(
        hProvParty2,
        pbKeyBlob1,
        dwDataLen1,
        hPrivateKey2,
        0,
        &hSessionKey1);
    if(!fReturn)
    {
        goto ErrorExit;
    }

    /************************
    Convert the agreed keys to symmetric keys. They are currently of 
    the form CALG_AGREEDKEY_ANY. Convert them to CALG_RC4.
    ************************/
    ALG_ID Algid = CALG_RC4;

    // Enable the party 1 public session key for use by setting the 
    // ALGID.
    fReturn = CryptSetKeyParam(
        hSessionKey1,
        KP_ALGID,
        (PBYTE)&Algid,
        0);
    if(!fReturn)
    {
        goto ErrorExit;
    }

    // Enable the party 2 public session key for use by setting the 
    // ALGID.
    fReturn = CryptSetKeyParam(
        hSessionKey2,
        KP_ALGID,
        (PBYTE)&Algid,
        0);
    if(!fReturn)
    {
        goto ErrorExit;
    }

    /************************
    Encrypt some data with party 1's session key. 
    ************************/
    // Get the size.
    DWORD dwLength = sizeof(g_rgbData);
    fReturn = CryptEncrypt(
        hSessionKey1, 
        0, 
        TRUE,
        0, 
        NULL, 
        &dwLength,
        sizeof(g_rgbData));
    if(!fReturn)
    {
        goto ErrorExit;
    }

    // Allocate a buffer to hold the encrypted data.
    pbData = (PBYTE)malloc(dwLength);
    if(!pbData)
    {
        goto ErrorExit;
    }

    // Copy the unencrypted data to the buffer. The data will be 
    // encrypted in place.
    memcpy(pbData, g_rgbData, sizeof(g_rgbData)); 

    // Encrypt the data.
    dwLength = sizeof(g_rgbData);
    fReturn = CryptEncrypt(
        hSessionKey1, 
        0, 
        TRUE,
        0, 
        pbData, 
        &dwLength,
        sizeof(g_rgbData));
    if(!fReturn)
    {
        goto ErrorExit;
    }
  
    /************************
    Decrypt the data with party 2's session key. 
    ************************/
    dwLength = sizeof(g_rgbData);
    fReturn = CryptDecrypt(
        hSessionKey2,
        0,
        TRUE,
        0,
        pbData,
        &dwLength);
    if(!fReturn)
    {
        goto ErrorExit;
    }


ErrorExit:
    if(pbData)
    {
        free(pbData);
        pbData = NULL;
    }

    if(hSessionKey2)
    {
        CryptDestroyKey(hSessionKey2);
        hSessionKey2 = NULL;
    }

    if(hSessionKey1)
    {
        CryptDestroyKey(hSessionKey1);
        hSessionKey1 = NULL;
    }

    if(pbKeyBlob2)
    {
        free(pbKeyBlob2);
        pbKeyBlob2 = NULL;
    }

    if(pbKeyBlob1)
    {
        free(pbKeyBlob1);
        pbKeyBlob1 = NULL;
    }

    if(hPrivateKey2)
    {
        CryptDestroyKey(hPrivateKey2);
        hPrivateKey2 = NULL;
    }

    if(hPrivateKey1)
    {
        CryptDestroyKey(hPrivateKey1);
        hPrivateKey1 = NULL;
    }

    if(hProvParty2)
    {
        CryptReleaseContext(hProvParty2, 0);
        hProvParty2 = NULL;
    }

    if(hProvParty1)
    {
        CryptReleaseContext(hProvParty1, 0);
        hProvParty1 = NULL;
    }

    return 0;
}