Export (0) Print
Expand All

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

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft