Share via


RSAPKCS1KeyExchangeFormatter.CreateKeyExchange 方法

定義

建立加密的金鑰交換資料。

多載

CreateKeyExchange(Byte[])

從指定的輸入資料建立加密的金鑰交換資料。

CreateKeyExchange(Byte[], Type)

從指定的輸入資料建立加密的金鑰交換資料。

CreateKeyExchange(Byte[])

來源:
RSAPKCS1KeyExchangeFormatter.cs
來源:
RSAPKCS1KeyExchangeFormatter.cs
來源:
RSAPKCS1KeyExchangeFormatter.cs

從指定的輸入資料建立加密的金鑰交換資料。

public:
 override cli::array <System::Byte> ^ CreateKeyExchange(cli::array <System::Byte> ^ rgbData);
public override byte[] CreateKeyExchange (byte[] rgbData);
override this.CreateKeyExchange : byte[] -> byte[]
Public Overrides Function CreateKeyExchange (rgbData As Byte()) As Byte()

參數

rgbData
Byte[]

要以金鑰交換傳遞的機密資訊。

傳回

Byte[]

要傳送給預定收件者的加密金鑰交換資料。

例外狀況

rgbData 太大。

備註

此資料只能由對應至用來加密資料的公開金鑰之私密金鑰的持有者解譯。 這有助於確保只有預期的收件者可以存取秘密資訊。

另請參閱

適用於

CreateKeyExchange(Byte[], Type)

來源:
RSAPKCS1KeyExchangeFormatter.cs
來源:
RSAPKCS1KeyExchangeFormatter.cs
來源:
RSAPKCS1KeyExchangeFormatter.cs

從指定的輸入資料建立加密的金鑰交換資料。

public:
 override cli::array <System::Byte> ^ CreateKeyExchange(cli::array <System::Byte> ^ rgbData, Type ^ symAlgType);
public override byte[] CreateKeyExchange (byte[] rgbData, Type? symAlgType);
public override byte[] CreateKeyExchange (byte[] rgbData, Type symAlgType);
override this.CreateKeyExchange : byte[] * Type -> byte[]
Public Overrides Function CreateKeyExchange (rgbData As Byte(), symAlgType As Type) As Byte()

參數

rgbData
Byte[]

要以金鑰交換傳遞的機密資訊。

symAlgType
Type

目前版本中未使用這個參數。

傳回

Byte[]

要傳送給預定收件者的加密金鑰交換資料。

範例

下列範例示範如何使用 RSAPKCS1KeyExchangeFormatter.CreateKeyExchange 方法來建立郵件收件者的交換金鑰。 此程式碼範例是針對 類別提供的較大範例的 RSAPKCS1KeyExchangeFormatter 一部分。

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

        // Encrypt the session key
        RSAPKCS1KeyExchangeFormatter keyFormatter = new RSAPKCS1KeyExchangeFormatter(key);
        encryptedSessionKey = keyFormatter.CreateKeyExchange(aes.Key, typeof(Aes));

        // 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();
        }
    }
}
Private Shared Sub Send(ByVal key As RSA, ByVal secretMessage As String, ByRef iv() As Byte, ByRef encryptedSessionKey() As Byte, ByRef encryptedMessage() As Byte)
    Dim aes = New AesCryptoServiceProvider()
    Try
        iv = aes.IV

        ' Encrypt the session key
        Dim keyFormatter As New RSAPKCS1KeyExchangeFormatter(key)
        encryptedSessionKey = keyFormatter.CreateKeyExchange(aes.Key, GetType(Aes))

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

                encryptedMessage = ciphertext.ToArray()
            Finally
                cs.Dispose()
            End Try
        Finally
            ciphertext.Dispose()
        End Try
    Finally
        aes.Dispose()
    End Try

End Sub

備註

此資料只能由對應至用來加密資料的公開金鑰之私密金鑰的持有者解譯。 這有助於確保只有預期的收件者可以存取秘密資訊。

另請參閱

適用於