RSAOAEPKeyExchangeFormatter.CreateKeyExchange Method (Byte[], Type)

 

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

Namespace:   System.Security.Cryptography
Assembly:  mscorlib (in mscorlib.dll)

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

Parameters

rgbData
Type: System.Byte[]

The secret information to be passed in the key exchange.

symAlgType
Type: System.Type

This parameter is not used in the current version.

Return Value

Type: System.Byte[]

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

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.

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

.NET Framework
Available since 1.1
Windows Phone Silverlight
Available since 7.1
Return to top
Show: