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

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

public override byte[] CreateKeyExchange (byte[] rgbData);

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

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

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

CreateKeyExchange(Byte[], Type)

Source:
RSAOAEPKeyExchangeFormatter.cs
Source:
RSAOAEPKeyExchangeFormatter.cs
Source:
RSAOAEPKeyExchangeFormatter.cs

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

public override byte[] CreateKeyExchange (byte[] rgbData, Type? symAlgType);
public override byte[] CreateKeyExchange (byte[] rgbData, Type symAlgType);

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 RSAOAEPKeyExchangeFormatter.CreateKeyExchange method to create an exchange key for a message recipient. This code example is part of a larger example provided for the RSAOAEPKeyExchangeFormatter 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();
        }
    }
}

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

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1