Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

RSAPKCS1KeyExchangeDeformatter Class

Decrypts the PKCS #1 key exchange data.

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

[ComVisibleAttribute(true)] 
public class RSAPKCS1KeyExchangeDeformatter : AsymmetricKeyExchangeDeformatter
/** @attribute ComVisibleAttribute(true) */ 
public class RSAPKCS1KeyExchangeDeformatter extends AsymmetricKeyExchangeDeformatter
ComVisibleAttribute(true) 
public class RSAPKCS1KeyExchangeDeformatter extends AsymmetricKeyExchangeDeformatter
Not applicable.

Key exchange allows a sender to create secret information (such as random data that can be used as a key in a symmetric encryption algorithm) and use encryption to send it to the intended recipient.

Use RSAPKCS1KeyExchangeFormatter to create the key exchange message using the RSA algorithm.

Caution noteCaution:

It is highly recommended that you not attempt to create your own key exchange method from the basic functionality provided, because many details of the operation must be performed carefully in order for the key exchange to be successful.

The following code example demonstrates how to use members of the RSAPKCS1KeyExchangeDeformatter class.

using System;
using System.Security.Cryptography;
using System.Text;

class RSAEncoder
{
    // Use a member variable to hold the RSA key for encoding and decoding.
    private RSA rsaKey;

    [STAThread]
    static void Main(string[] args)
    {
        string message = "A phrase to be encoded.";

        RSAEncoder rsaEncoder = new RSAEncoder();
        rsaEncoder.InitializeKey(RSA.Create());

        Console.WriteLine("Encoding the following message:");
        Console.WriteLine(message);
        byte[] encodedMessage = rsaEncoder.EncodeMessage(message);
        Console.WriteLine("Resulting message encoded:");
        Console.WriteLine(Encoding.ASCII.GetString(encodedMessage));

        string decodedMessage = rsaEncoder.DecodeMessage(encodedMessage);
        Console.WriteLine("Resulting message decoded:");
        Console.WriteLine(decodedMessage);

        // Construct a formatter to demonstrate how to set each property.
        rsaEncoder.ConstructFormatter();

        // Construct a deformatter to demonstrate how to set each property.
        rsaEncoder.ConstructDeformatter();

        Console.WriteLine("This sample completed successfully; " + 
            "press Enter to exit.");
        Console.ReadLine();
    }

    // Initialize an rsaKey member variable with the specified RSA key.
    private void InitializeKey(RSA key)
    {
        rsaKey = key;
    }

    // Use the RSAPKCS1KeyExchangeDeformatter class to decode the 
    // specified message.
    private byte[] EncodeMessage(string message)
    {
        byte[] encodedMessage = null;

        try
        {
            // Construct a formatter with the specified RSA key.
            RSAPKCS1KeyExchangeFormatter keyEncryptor =
                new RSAPKCS1KeyExchangeFormatter(rsaKey);

            // Convert the message to bytes to create the encrypted data.
            byte[] byteMessage = Encoding.ASCII.GetBytes(message);
            encodedMessage = keyEncryptor.CreateKeyExchange(byteMessage);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Unexpected exception caught:" + ex.ToString());
        }

        return encodedMessage;
    }

    // Use the RSAPKCS1KeyExchangeDeformatter class to decode the
    // specified message.
    private string DecodeMessage(byte[] encodedMessage)
    {
        string decodedMessage = null;

        try
        {
            // Construct a deformatter with the specified RSA key.
            RSAPKCS1KeyExchangeDeformatter keyDecryptor =
                new RSAPKCS1KeyExchangeDeformatter(rsaKey);

            // Decrypt the encoded message.
            byte[] decodedBytes =
                keyDecryptor.DecryptKeyExchange(encodedMessage);

            // Retrieve a string representation of the decoded message.
            decodedMessage = Encoding.ASCII.GetString(decodedBytes);
        }
        catch (Exception ex)
        {
            Console.WriteLine("Unexpected exception caught:" + ex.ToString());
        }

        return decodedMessage;
    }

    // Create an RSAPKCS1KeyExchangeFormatter object with a new RSA key.
    // Display its properties to the console.
    private void ConstructFormatter()
    {
        // Construct an empty Optimal Asymmetric Encryption Padding (OAEP)
        // key exchange.
        RSAPKCS1KeyExchangeFormatter rsaFormatter =
            new RSAPKCS1KeyExchangeFormatter();

        // Create an RSAKey and set it into the specified 
        // RSAPKCS1KeyExchangeFormatter.
        RSA key = RSA.Create();
        rsaFormatter.SetKey(key);

        // Create a random number using RNGCryptoServiceProvider.
        RNGCryptoServiceProvider ring = new RNGCryptoServiceProvider(); 
        rsaFormatter.Rng = ring;

        Console.WriteLine();
        Console.WriteLine("**" + rsaFormatter.ToString() + "**");
        Console.Write("The following random number was generated using the ");
        Console.WriteLine("class:");
        Console.WriteLine(rsaFormatter.Rng);

        string xmlParameters = rsaFormatter.Parameters;

        Console.WriteLine();
        Console.WriteLine("The RSA formatter has the following parameters:");
        Console.WriteLine(rsaFormatter.Parameters);
    }

    // Create an RSAPKCS1KeyExchangeDeformatter object with a new RSA key.
    // Display its properties to the console.
    private void ConstructDeformatter()
    {
        // Construct an empty OAEP key exchange.
        RSAPKCS1KeyExchangeDeformatter rsaDeformatter = 
            new RSAPKCS1KeyExchangeDeformatter();

        // Create an RSAKey and set it into the specified 
        // RSAPKCS1KeyExchangeFormatter.
        RSA key = RSA.Create();
        rsaDeformatter.SetKey(key);

        RNGCryptoServiceProvider ring = new RNGCryptoServiceProvider(); 
        rsaDeformatter.RNG = ring;

        Console.WriteLine();
        Console.WriteLine("**" + rsaDeformatter.ToString() + "**");

        string xmlParameters = rsaDeformatter.Parameters;

        Console.WriteLine();
        Console.WriteLine("The RSA deformatter has the following ");
        Console.WriteLine("parameters:" + xmlParameters);
    }
}

import System.*;
import System.Security.Cryptography.*;
import System.Text.*;

class RSAEncoder
{
    // Use a member variable to hold the RSA key for encoding and decoding.
    private RSA rsaKey;

    /** @attribute STAThread()
     */
    public static void main(String[] args)
    {
        String message = "A phrase to be encoded.";

        RSAEncoder rsaEncoder = new RSAEncoder();
        rsaEncoder.InitializeKey(RSA.Create());

        Console.WriteLine("Encoding the following message:");
        Console.WriteLine(message);
        ubyte encodedMessage[] = rsaEncoder.EncodeMessage(message);
        Console.WriteLine("Resulting message encoded:");
        Console.WriteLine(Encoding.get_ASCII().GetString(encodedMessage));

        String decodedMessage = rsaEncoder.DecodeMessage(encodedMessage);
        Console.WriteLine("Resulting message decoded:");
        Console.WriteLine(decodedMessage);
        // Construct a formatter to demonstrate how to set each property.
        rsaEncoder.ConstructFormatter();
        // Construct a deformatter to demonstrate how to set each property.
        rsaEncoder.ConstructDeformatter();

        Console.WriteLine("This sample completed successfully; " 
            + "press Enter to exit.");
        Console.ReadLine();
    } //main

    // Initialize an rsaKey member variable with the specified RSA key.
    private void InitializeKey(RSA key)
    {
        rsaKey = key;
    } //InitializeKey

    // Use the RSAPKCS1KeyExchangeDeformatter class to decode the 
    // specified message.
    private ubyte[] EncodeMessage(String message)
    {
        ubyte encodedMessage[] = null;
        try {
            // Construct a formatter with the specified RSA key.
            RSAPKCS1KeyExchangeFormatter keyEncryptor 
                = new RSAPKCS1KeyExchangeFormatter(rsaKey);

            // Convert the message to bytes to create the encrypted data.
            ubyte byteMessage[] = Encoding.get_ASCII().GetBytes(message);
            encodedMessage = keyEncryptor.CreateKeyExchange(byteMessage);
        } 
        catch (System.Exception ex) {
            Console.WriteLine("Unexpected exception caught:" + ex.ToString());
        }
        return encodedMessage;
    } //EncodeMessage

    // Use the RSAPKCS1KeyExchangeDeformatter class to decode the
    // specified message.
    private String DecodeMessage(ubyte encodedMessage[])
    {
        String decodedMessage = null;
        try {
            // Construct a deformatter with the specified RSA key.
            RSAPKCS1KeyExchangeDeformatter keyDecryptor 
                = new RSAPKCS1KeyExchangeDeformatter(rsaKey);

            // Decrypt the encoded message.
            ubyte decodedBytes[] = keyDecryptor.
                DecryptKeyExchange(encodedMessage);

            // Retrieve a string representation of the decoded message.
            decodedMessage = Encoding.get_ASCII().GetString(decodedBytes);
        }
        catch (System.Exception ex) {
            Console.WriteLine("Unexpected exception caught:" 
                + ex.ToString());
        }
        return decodedMessage;
    } //DecodeMessage

    // Create an RSAPKCS1KeyExchangeFormatter object with a new RSA key.
    // Display its properties to the console.
    private void ConstructFormatter()
    {
        // Construct an empty Optimal Asymmetric Encryption Padding (OAEP)
        // key exchange.
        RSAPKCS1KeyExchangeFormatter rsaFormatter 
            = new RSAPKCS1KeyExchangeFormatter();

        // Create an RSAKey and set it into the specified 
        // RSAPKCS1KeyExchangeFormatter.
        RSA key = RSA.Create();
        rsaFormatter.SetKey(key);

        // Create a random number using RNGCryptoServiceProvider.
        RNGCryptoServiceProvider ring = new RNGCryptoServiceProvider();
        rsaFormatter.set_Rng(ring);

        Console.WriteLine();
        Console.WriteLine("**" + rsaFormatter.ToString() + "**");
        Console.Write("The following random number was generated using the ");
        Console.WriteLine("class:");
        Console.WriteLine(rsaFormatter.get_Rng());
        String xmlParameters = rsaFormatter.get_Parameters();

        Console.WriteLine();
        Console.WriteLine("The RSA formatter has the following parameters:");
        Console.WriteLine(rsaFormatter.get_Parameters());
    } //ConstructFormatter

    // Create an RSAPKCS1KeyExchangeDeformatter object with a new RSA key.
    // Display its properties to the console.
    private void ConstructDeformatter()
    {
        // Construct an empty OAEP key exchange.
        RSAPKCS1KeyExchangeDeformatter rsaDeformatter 
            = new RSAPKCS1KeyExchangeDeformatter();

        // Create an RSAKey and set it into the specified 
        // RSAPKCS1KeyExchangeFormatter.
        RSA key = RSA.Create();
        rsaDeformatter.SetKey(key);

        RNGCryptoServiceProvider ring = new RNGCryptoServiceProvider();
        rsaDeformatter.set_RNG(ring);

        Console.WriteLine();
        Console.WriteLine("**" + rsaDeformatter.ToString() + "**");
        String xmlParameters = rsaDeformatter.get_Parameters();

        Console.WriteLine();
        Console.WriteLine("The RSA deformatter has the following ");
        Console.WriteLine("parameters:" + xmlParameters);
    } //ConstructDeformatter
} //RSAEncoder

System.Object
   System.Security.Cryptography.AsymmetricKeyExchangeDeformatter
    System.Security.Cryptography.RSAPKCS1KeyExchangeDeformatter
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.