RSAPKCS1KeyExchangeFormatter.CreateKeyExchange 方法

定义

创建加密密钥交换数据。

重载

CreateKeyExchange(Byte[])

从指定的输入数据创建加密的密钥交换数据。

CreateKeyExchange(Byte[], Type)

从指定的输入数据创建加密的密钥交换数据。

CreateKeyExchange(Byte[])

Source:
RSAPKCS1KeyExchangeFormatter.cs
Source:
RSAPKCS1KeyExchangeFormatter.cs
Source:
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)

Source:
RSAPKCS1KeyExchangeFormatter.cs
Source:
RSAPKCS1KeyExchangeFormatter.cs
Source:
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

注解

此数据只能由私钥的持有者解释,该私钥对应于用于加密数据的公钥。 这有助于确保只有预期的收件人可以访问机密信息。

另请参阅

适用于