Редагувати

Поділитися через


RSAPKCS1KeyExchangeFormatter.CreateKeyExchange Method

Definition

Creates the encrypted key exchange data.

Overloads

CreateKeyExchange(Byte[])

Creates the encrypted key exchange data from the specified input data.

CreateKeyExchange(Byte[], Type)

Creates the encrypted key exchange data from the specified input data.

CreateKeyExchange(Byte[])

Source:
RSAPKCS1KeyExchangeFormatter.cs
Source:
RSAPKCS1KeyExchangeFormatter.cs
Source:
RSAPKCS1KeyExchangeFormatter.cs

Creates the encrypted key exchange data from the specified input data.

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()

Parameters

rgbData
Byte[]

The secret information to be passed in the key exchange.

Returns

Byte[]

The encrypted key exchange data to be sent to the intended recipient.

Exceptions

rgbData is too big.

Remarks

This data can be interpreted only by the holder of the private key corresponding to the public key used to encrypt the data. This helps to ensure that only the intended recipient can access the secret information.

See also

Applies to

CreateKeyExchange(Byte[], Type)

Source:
RSAPKCS1KeyExchangeFormatter.cs
Source:
RSAPKCS1KeyExchangeFormatter.cs
Source:
RSAPKCS1KeyExchangeFormatter.cs

Creates the encrypted key exchange data from the specified input data.

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()

Parameters

rgbData
Byte[]

The secret information to be passed in the key exchange.

symAlgType
Type

This parameter is not used in the current version.

Returns

Byte[]

The encrypted key exchange data to be sent to the intended recipient.

Examples

The following example shows how to use the RSAPKCS1KeyExchangeFormatter.CreateKeyExchange method to create an exchange key for a message recipient. This code example is part of a larger example provided for the RSAPKCS1KeyExchangeFormatter class.

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

Remarks

This data can be interpreted only by the holder of the private key corresponding to the public key used to encrypt the data. This helps to ensure that only the intended recipient can access the secret information.

See also

Applies to