ECDiffieHellmanCng 類別

定義

提供次世代密碼學(CNG)實現的橢圓曲線 Diffie-Hellman(ECDH)演算法。 此類別用於執行密碼學操作。

public ref class ECDiffieHellmanCng sealed : System::Security::Cryptography::ECDiffieHellman
public sealed class ECDiffieHellmanCng : System.Security.Cryptography.ECDiffieHellman
type ECDiffieHellmanCng = class
    inherit ECDiffieHellman
Public NotInheritable Class ECDiffieHellmanCng
Inherits ECDiffieHellman
繼承
繼承

範例

以下範例展示了如何利用該 ECDiffieHellmanCng 類別建立金鑰交換,以及如何利用該金鑰加密可透過公共通道傳送並由接收方解密的訊息。

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

class Alice
{
    public static byte[] alicePublicKey;

    public static void Main(string[] args)
    {
        using (ECDiffieHellmanCng alice = new ECDiffieHellmanCng())
        {

            alice.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
            alice.HashAlgorithm = CngAlgorithm.Sha256;
            alicePublicKey = alice.PublicKey.ToByteArray();
            Bob bob = new Bob();
            CngKey bobKey = CngKey.Import(bob.bobPublicKey, CngKeyBlobFormat.EccPublicBlob);
            byte[] aliceKey = alice.DeriveKeyMaterial(bobKey);
            byte[] encryptedMessage = null;
            byte[] iv = null;
            Send(aliceKey, "Secret message", out encryptedMessage, out iv);
            bob.Receive(encryptedMessage, iv);
        }
    }

    private static void Send(byte[] key, string secretMessage, out byte[] encryptedMessage, out byte[] iv)
    {
        using (Aes aes = new AesCryptoServiceProvider())
        {
            aes.Key = key;
            iv = aes.IV;

            // Encrypt the message
            using (MemoryStream ciphertext = new MemoryStream())
            using (CryptoStream cs = new CryptoStream(ciphertext, aes.CreateEncryptor(), CryptoStreamMode.Write))
            {
                byte[] plaintextMessage = Encoding.UTF8.GetBytes(secretMessage);
                cs.Write(plaintextMessage, 0, plaintextMessage.Length);
                cs.Close();
                encryptedMessage = ciphertext.ToArray();
            }
        }
    }
}
public class Bob
{
    public byte[] bobPublicKey;
    private byte[] bobKey;
    public Bob()
    {
        using (ECDiffieHellmanCng bob = new ECDiffieHellmanCng())
        {

            bob.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
            bob.HashAlgorithm = CngAlgorithm.Sha256;
            bobPublicKey = bob.PublicKey.ToByteArray();
            bobKey = bob.DeriveKeyMaterial(CngKey.Import(Alice.alicePublicKey, CngKeyBlobFormat.EccPublicBlob));
        }
    }

    public void Receive(byte[] encryptedMessage, byte[] iv)
    {

        using (Aes aes = new AesCryptoServiceProvider())
        {
            aes.Key = bobKey;
            aes.IV = iv;
            // Decrypt the message
            using (MemoryStream plaintext = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(plaintext, aes.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(encryptedMessage, 0, encryptedMessage.Length);
                    cs.Close();
                    string message = Encoding.UTF8.GetString(plaintext.ToArray());
                    Console.WriteLine(message);
                }
            }
        }
    }
}
Imports System.IO
Imports System.Security.Cryptography
Imports System.Text




Class Alice
    Public Shared alicePublicKey() As Byte


    Public Shared Sub Main(ByVal args() As String)
        Using alice As New ECDiffieHellmanCng()
            alice.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash
            alice.HashAlgorithm = CngAlgorithm.Sha256
            alicePublicKey = alice.PublicKey.ToByteArray()
            Dim bob As New Bob()
            Dim k As CngKey = CngKey.Import(bob.bobPublicKey, CngKeyBlobFormat.EccPublicBlob)
            Dim aliceKey As Byte() = alice.DeriveKeyMaterial(CngKey.Import(bob.bobPublicKey, CngKeyBlobFormat.EccPublicBlob))
            Dim encryptedMessage As Byte() = Nothing
            Dim iv As Byte() = Nothing
            Send(aliceKey, "Secret message", encryptedMessage, iv)
            bob.Receive(encryptedMessage, iv)
        End Using
    End Sub


    Private Shared Sub Send(ByVal key() As Byte, ByVal secretMessage As String, ByRef encryptedMessage() As Byte, ByRef iv() As Byte)
        Using aes As New AesCryptoServiceProvider()
            aes.Key = key
            iv = aes.IV

            ' Encrypt the message
            Using ciphertext As New MemoryStream()
                Using cs As New CryptoStream(ciphertext, aes.CreateEncryptor(), CryptoStreamMode.Write)
                    Dim plaintextMessage As Byte() = Encoding.UTF8.GetBytes(secretMessage)
                    cs.Write(plaintextMessage, 0, plaintextMessage.Length)
                    cs.Close()
                    encryptedMessage = ciphertext.ToArray()
                End Using
            End Using
        End Using

    End Sub
End Class

Public Class Bob
    Public bobPublicKey() As Byte
    Private bobKey() As Byte

    Public Sub New()
        Using bob As New ECDiffieHellmanCng()

            bob.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash
            bob.HashAlgorithm = CngAlgorithm.Sha256
            bobPublicKey = bob.PublicKey.ToByteArray()
            bobKey = bob.DeriveKeyMaterial(CngKey.Import(Alice.alicePublicKey, CngKeyBlobFormat.EccPublicBlob))
        End Using

    End Sub


    Public Sub Receive(ByVal encryptedMessage() As Byte, ByVal iv() As Byte)

        Using aes As New AesCryptoServiceProvider()
                aes.Key = bobKey
                aes.IV = iv
                ' Decrypt the message
            Using plaintext As New MemoryStream()
                Using cs As New CryptoStream(plaintext, aes.CreateDecryptor(), CryptoStreamMode.Write)
                    cs.Write(encryptedMessage, 0, encryptedMessage.Length)
                    cs.Close()
                    Dim message As String = Encoding.UTF8.GetString(plaintext.ToArray())
                    Console.WriteLine(message)
                End Using
            End Using
        End Using
    End Sub
End Class

備註

ECDiffieHellmanCng 類別允許雙方即使透過公開通道通訊,也能交換私鑰資料。 雙方可以計算相同的秘密值,這在管理 Diffie-Hellman 類別中稱為 秘密協議 。 秘密協議可用於多種用途,包括作為對稱密鑰。 然而,該 ECDiffieHellmanCng 類別並未直接揭露秘密協議,而是在提供價值前對協議進行後續處理。 此後處理稱為 金鑰推導函數(KDF);你可以選擇想使用的 KDF,並透過 Diffie-Hellman 物件實例上的一組屬性設定參數。

密鑰推導函數 屬性
Hash HashAlgorithm - 用於處理秘密協議的雜湊演算法。

SecretPrepend - 一個可選的位元組陣列,用於在雜湊前先加於秘密協議。

SecretAppend - 一個可選的位元組陣列,可在雜湊前附加於秘密協議。
Hmac HashAlgorithm - 用於處理秘密協議的雜湊演算法。

SecretPrepend- 一個可選的位元組陣列,用於在雜湊前先加於秘密協議。

SecretAppend - 一個可選的位元組陣列,可在雜湊前附加於秘密協議。
Tls Label - 用於金鑰衍生的標籤。

Seed - 密鑰推導的種子。

將秘密協議傳遞給金鑰導出函式後,會產生一個位元組陣列,可用於您的應用程式作為金鑰材料。 產生的金鑰資料位元組數取決於金鑰推導函數;例如,SHA-256 會產生 256 位元的金鑰材料,而 SHA-512 則會產生 512 位元的金鑰材料。 ECDH 金鑰交換的基本流程如下:

  1. Alice 和 Bob 建立一對金鑰對,用於 Diffie-Hellman 金鑰交換操作

  2. Alice 和 Bob 用他們同意的參數配置 KDF。

  3. 愛麗絲把她的公鑰寄給鮑勃。

  4. Bob 把他的公開金鑰傳給 Alice。

  5. Alice 和 Bob 使用彼此的公鑰來產生秘密協議,並將 KDF 應用於秘密協議以產生密鑰材料。

建構函式

名稱 Description
ECDiffieHellmanCng()

初始化一個隨機金鑰對的新類別實例 ECDiffieHellmanCng

ECDiffieHellmanCng(CngKey)

使用指定的 ECDiffieHellmanCng 物件,初始化 CngKey 類別的新實例。

ECDiffieHellmanCng(ECCurve)

建立一個新的類別實例 ECDiffieHellmanCng ,其公私鑰對是在指定的曲線上產生的。

ECDiffieHellmanCng(Int32)

使用指定的金鑰大小,初始化一個隨機金鑰對的新類別實例 ECDiffieHellmanCng

欄位

名稱 Description
KeySizeValue

代表非對稱演算法所使用的金鑰模數的大小(以位元為單位)。

(繼承來源 AsymmetricAlgorithm)
LegalKeySizesValue

指定非對稱演算法所支援的金鑰大小。

(繼承來源 AsymmetricAlgorithm)

屬性

名稱 Description
HashAlgorithm

取得或設定雜湊演算法,用於產生關鍵資料。

HmacKey

取得或設定基於雜湊的訊息認證碼(HMAC)金鑰,用於導出金鑰資料。

Key

指定 CngKey 當前物件用於密碼學運算的 that。

KeyDerivationFunction

取得或設定類別的 ECDiffieHellmanCng 金鑰推導函數。

KeyExchangeAlgorithm

取得金鑰交換演算法的名稱。

(繼承來源 ECDiffieHellman)
KeySize

取得或設定非對稱演算法所使用的金鑰模數的大小(以位元為單位)。

KeySize

取得或設定非對稱演算法所使用的金鑰模數的大小(以位元為單位)。

(繼承來源 AsymmetricAlgorithm)
Label

取得或設定用於金鑰推導的標籤值。

LegalKeySizes

取得非對稱演算法所支援的金鑰大小。

LegalKeySizes

取得非對稱演算法所支援的金鑰大小。

(繼承來源 AsymmetricAlgorithm)
PublicKey

取得公鑰,其他物件可用 ECDiffieHellmanCng 來產生共享秘密協議。

SecretAppend

取得或設定一個值,並在產生關鍵資料時附加於秘密協議後。

SecretPrepend

取得或設定一個值,並在推導關鍵資料時加到秘密協議的開頭。

Seed

取得或設定用於推導關鍵材料的種子值。

SignatureAlgorithm

取得簽名演算法的名稱。

(繼承來源 ECDiffieHellman)
UseSecretAgreementAsHmacKey

會獲得一個值,指示秘密協議是否作為基於雜湊的訊息認證碼(HMAC)金鑰來推導金鑰資料。

方法

名稱 Description
Clear()

釋放 AsymmetricAlgorithm 類別所使用的所有資源。

(繼承來源 AsymmetricAlgorithm)
DeriveKeyFromHash(ECDiffieHellmanPublicKey, HashAlgorithmName, Byte[], Byte[])

使用指定的雜湊演算法執行金鑰推導,並可選擇前置或附加資料。

DeriveKeyFromHash(ECDiffieHellmanPublicKey, HashAlgorithmName)

使用指定的雜湊演算法進行金鑰推導。

(繼承來源 ECDiffieHellman)
DeriveKeyFromHmac(ECDiffieHellmanPublicKey, HashAlgorithmName, Byte[], Byte[], Byte[])

使用指定的 HMAC(基於雜湊的訊息認證碼)演算法執行金鑰導出,並可選擇前置或附加資料。

DeriveKeyFromHmac(ECDiffieHellmanPublicKey, HashAlgorithmName, Byte[])

使用指定的 HMAC(基於雜湊的訊息認證碼)演算法執行金鑰導出。

(繼承來源 ECDiffieHellman)
DeriveKeyMaterial(CngKey)

根據包含第二方公開金鑰的物件,從兩方 CngKey 秘密協議中產生的金鑰材料。

DeriveKeyMaterial(ECDiffieHellmanPublicKey)

從雙方秘密協議中產生的金鑰材料,給定 ECDiffieHellmanPublicKey 包含第二方公開金鑰的物件。

DeriveKeyTls(ECDiffieHellmanPublicKey, Byte[], Byte[])

使用 TLS(傳輸層安全)1.1 PRF(Pseudo-Random 函數)執行金鑰推導。

DeriveRawSecretAgreement(ECDiffieHellmanPublicKey)

推導原始關鍵材料。

(繼承來源 ECDiffieHellman)
DeriveSecretAgreementHandle(CngKey)

取得雙方產生的秘密協議的句柄,該 CngKey 物件包含第二方的公開金鑰。

DeriveSecretAgreementHandle(ECDiffieHellmanPublicKey)

取得兩個當事人之間產生的秘密協議的帳柄,給定 ECDiffieHellmanPublicKey 一個包含第二方公開金鑰的物件。

Dispose()

釋放目前類別實例 AsymmetricAlgorithm 所使用的所有資源。

(繼承來源 AsymmetricAlgorithm)
Dispose(Boolean)

釋放類別使用的 AsymmetricAlgorithm 非管理資源,並可選擇性地釋放受管理資源。

(繼承來源 AsymmetricAlgorithm)
Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
ExportECPrivateKey()

匯出目前的金鑰,格式為 ECPrivateKey。

(繼承來源 ECDiffieHellman)
ExportECPrivateKeyPem()

匯出目前的金鑰,格式為 ECPrivateKey,並以 PEM 編碼。

(繼承來源 ECAlgorithm)
ExportEncryptedPkcs8PrivateKey(ReadOnlySpan<Byte>, PbeParameters)

以 PKCS#8 EncryptedPrivateKeyInfo 格式匯出目前金鑰,並以位元組為基礎的密碼。

ExportEncryptedPkcs8PrivateKey(ReadOnlySpan<Byte>, PbeParameters)

以 PKCS#8 EncryptedPrivateKeyInfo 格式匯出目前金鑰,並以位元組為基礎的密碼。

(繼承來源 AsymmetricAlgorithm)
ExportEncryptedPkcs8PrivateKey(ReadOnlySpan<Char>, PbeParameters)

以 PKCS#8 EncryptedPrivateKeyInfo 格式匯出目前金鑰,並以字元為基礎的密碼。

ExportEncryptedPkcs8PrivateKey(ReadOnlySpan<Char>, PbeParameters)

以 PKCS#8 EncryptedPrivateKeyInfo 格式匯出目前金鑰,並以字元為基礎的密碼。

(繼承來源 AsymmetricAlgorithm)
ExportEncryptedPkcs8PrivateKeyPem(ReadOnlySpan<Byte>, PbeParameters)

匯出目前的金鑰,格式為 PKCS#8 EncryptedPrivateKeyInfo,並以位元組密碼(PEM 編碼)匯出。

(繼承來源 AsymmetricAlgorithm)
ExportEncryptedPkcs8PrivateKeyPem(ReadOnlySpan<Char>, PbeParameters)

以 PKCS#8 EncryptedPrivateKeyInfo 格式匯出目前金鑰,並以字元密碼(PEM encoded)匯出。

(繼承來源 AsymmetricAlgorithm)
ExportExplicitParameters(Boolean)

將物件使用的 ECCurve 鍵和明確的曲線參數匯出到物件中 ECParameters

ExportParameters(Boolean)

將物件使用的ECCurve金鑰匯出成物件。ECParameters

ExportPkcs8PrivateKey()

匯出目前的金鑰,格式為 PKCS#8 PrivateKeyInfo。

(繼承來源 AsymmetricAlgorithm)
ExportPkcs8PrivateKeyPem()

匯出目前的金鑰,格式為 PKCS#8 PrivateKeyInfo,並以 PEM 編碼。

(繼承來源 AsymmetricAlgorithm)
ExportSubjectPublicKeyInfo()

匯出目前金鑰中公開金鑰部分,格式為 X.509 SubjectPublicKeyInfo。

(繼承來源 AsymmetricAlgorithm)
ExportSubjectPublicKeyInfoPem()

匯出目前金鑰中公開金鑰部分,格式為 X.509 SubjectPublicKeyInfo,PEM 編碼。

(繼承來源 AsymmetricAlgorithm)
FromXmlString(String, ECKeyXmlFormat)
已淘汰.

透過使用指定的格式,將 XML 字串中的金鑰資訊反序列化。

FromXmlString(String)

未實作此方法。

FromXmlString(String)

這種方法涵蓋了所有情況。

(繼承來源 ECDiffieHellman)
GenerateKey(ECCurve)

為指定曲線產生新的短暫公私鑰對。

GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type

(繼承來源 Object)
ImportECPrivateKey(ReadOnlySpan<Byte>, Int32)

從 ECPrivateKey 結構匯入公私鑰對,替換此物件的金鑰。

(繼承來源 ECDiffieHellman)
ImportEncryptedPkcs8PrivateKey(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Int32)

在以位元組密碼解密後,從 PKCS#8 EncryptedPrivateKeyInfo 結構匯入公私密金鑰對,替換該物件的金鑰。

ImportEncryptedPkcs8PrivateKey(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Int32)

在以位元組密碼解密後,從 PKCS#8 EncryptedPrivateKeyInfo 結構匯入公私密金鑰對,替換該物件的金鑰。

(繼承來源 ECDiffieHellman)
ImportEncryptedPkcs8PrivateKey(ReadOnlySpan<Char>, ReadOnlySpan<Byte>, Int32)

在以字元密碼解密後,從 PKCS#8 EncryptedPrivateKeyInfo 結構匯入公私鑰對,替換該物件的金鑰。

ImportEncryptedPkcs8PrivateKey(ReadOnlySpan<Char>, ReadOnlySpan<Byte>, Int32)

在以字元密碼解密後,從 PKCS#8 EncryptedPrivateKeyInfo 結構匯入公私鑰對,替換該物件的金鑰。

(繼承來源 ECDiffieHellman)
ImportFromEncryptedPem(ReadOnlySpan<Char>, ReadOnlySpan<Byte>)

匯入加密的 RFC 7468 PEM 編碼私鑰,取代此物件的金鑰。

(繼承來源 ECDiffieHellman)
ImportFromEncryptedPem(ReadOnlySpan<Char>, ReadOnlySpan<Char>)

匯入加密的 RFC 7468 PEM 編碼私鑰,取代此物件的金鑰。

(繼承來源 ECDiffieHellman)
ImportFromPem(ReadOnlySpan<Char>)

匯入一個 RFC 7468 PEM 編碼的金鑰,替換此物件的金鑰。

(繼承來源 ECDiffieHellman)
ImportParameters(ECParameters)

將物件指定的參數 ECCurve 作為鍵值匯入目前實例。

ImportPkcs8PrivateKey(ReadOnlySpan<Byte>, Int32)

解密後,從 PKCS#8 PrivateKeyInfo 結構匯入公私鑰對,替換此物件的金鑰。

ImportPkcs8PrivateKey(ReadOnlySpan<Byte>, Int32)

解密後,從 PKCS#8 PrivateKeyInfo 結構匯入公私鑰對,替換此物件的金鑰。

(繼承來源 ECDiffieHellman)
ImportSubjectPublicKeyInfo(ReadOnlySpan<Byte>, Int32)

解密後,從 X.509 SubjectPublicKeyInfo 結構匯入公鑰,替換該物件的金鑰。

(繼承來源 ECDiffieHellman)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)
ToXmlString(Boolean)

未實作此方法。

ToXmlString(Boolean)

這種方法涵蓋了所有情況。

(繼承來源 ECDiffieHellman)
ToXmlString(ECKeyXmlFormat)
已淘汰.

透過指定格式將金鑰資訊序列化為 XML 字串。

TryExportECPrivateKey(Span<Byte>, Int32)

嘗試將格式中的 ECPrivateKey 當前金鑰匯出到提供的緩衝區。

(繼承來源 ECDiffieHellman)
TryExportECPrivateKeyPem(Span<Char>, Int32)

嘗試將目前的 PEM 編碼 ECPrivateKey 格式匯出至提供的緩衝區。

(繼承來源 ECAlgorithm)
TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan<Byte>, PbeParameters, Span<Byte>, Int32)

嘗試以 PKCS#8 EncryptedPrivateKeyInfo 格式將目前金鑰匯出到提供的緩衝區,使用位元組密碼。

TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan<Byte>, PbeParameters, Span<Byte>, Int32)

嘗試以 PKCS#8 EncryptedPrivateKeyInfo 格式將目前金鑰匯出到提供的緩衝區,使用位元組密碼。

(繼承來源 ECDiffieHellman)
TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan<Char>, PbeParameters, Span<Byte>, Int32)

嘗試將目前的 PKCS#8 EncryptedPrivateKeyInfo 格式金鑰匯出到提供的緩衝區,使用字元密碼。

TryExportEncryptedPkcs8PrivateKey(ReadOnlySpan<Char>, PbeParameters, Span<Byte>, Int32)

嘗試將目前的 PKCS#8 EncryptedPrivateKeyInfo 格式金鑰匯出到提供的緩衝區,使用字元密碼。

(繼承來源 ECDiffieHellman)
TryExportEncryptedPkcs8PrivateKeyPem(ReadOnlySpan<Byte>, PbeParameters, Span<Char>, Int32)

嘗試以 PKCS#8 EncryptedPrivateKeyInfo 格式匯出目前金鑰,並以位元組密碼(PEM 編碼)匯出。

(繼承來源 AsymmetricAlgorithm)
TryExportEncryptedPkcs8PrivateKeyPem(ReadOnlySpan<Char>, PbeParameters, Span<Char>, Int32)

以 PKCS#8 EncryptedPrivateKeyInfo 格式匯出目前金鑰,並以字元密碼(PEM encoded)匯出。

(繼承來源 AsymmetricAlgorithm)
TryExportPkcs8PrivateKey(Span<Byte>, Int32)

嘗試將目前的 PKCS#8 PrivateKeyInfo 格式匯出至提供的緩衝區。

TryExportPkcs8PrivateKey(Span<Byte>, Int32)

嘗試將目前的 PKCS#8 PrivateKeyInfo 格式匯出至提供的緩衝區。

(繼承來源 ECDiffieHellman)
TryExportPkcs8PrivateKeyPem(Span<Char>, Int32)

嘗試將目前以 PEM 編碼的 PKCS#8 PrivateKeyInfo 格式匯出至提供的緩衝區。

(繼承來源 AsymmetricAlgorithm)
TryExportSubjectPublicKeyInfo(Span<Byte>, Int32)

嘗試將目前的 X.509 SubjectPublicKeyInfo 格式匯出至提供的緩衝區。

(繼承來源 ECDiffieHellman)
TryExportSubjectPublicKeyInfoPem(Span<Char>, Int32)

嘗試將目前的 PEM 編碼 X.509 SubjectPublicKeyInfo 格式匯出至提供的緩衝區。

(繼承來源 AsymmetricAlgorithm)

明確介面實作

名稱 Description
IDisposable.Dispose()

此 API 支援此產品基礎結構,但無法直接用於程式碼之中。

關於此成員的描述,請參見 Dispose()

(繼承來源 AsymmetricAlgorithm)

適用於