Este artigo foi traduzido por máquina. Para visualizar o arquivo em inglês, marque a caixa de seleção Inglês. Você também pode exibir o texto Em inglês em uma janela pop-up, movendo o ponteiro do mouse sobre o texto.
Tradução
Inglês
Esta documentação foi arquivada e não está sendo atualizada.

Classe RSAOAEPKeyExchangeDeformatter

Descriptografa o preenchimento da criptografia assimétrica ideal (OAEP) chave trocar dados.

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

[ComVisibleAttribute(true)]
public class RSAOAEPKeyExchangeDeformatter : AsymmetricKeyExchangeDeformatter

Troca de chaves permite que um remetente para criar informações secretas, por exemplo, dados aleatórios que podem ser usados sistema autônomo uma chave em um algoritmo de criptografia simétrica e usam criptografia para enviá-lo para o destinatário pretendido.

Use RSAOAEPKeyExchangeFormatter Para criar o chave troca de mensagem com o RSA algoritmo.

Observação de cuidadoCuidado:

É altamente recomendável que você não tenta criar suas próprias chave trocar método a partir da funcionalidade básica fornecida, porque muitos detalhes da operação devem ser executados com cuidado para que o chave troca seja bem-sucedido.

O exemplo de código a seguir demonstra como usar os membros a RSAOAEPKeyExchangeDeformatter classe.

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]
    staticvoid 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 continue.");
        Console.ReadLine();
    }

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

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

        try
        {
            // Construct a formatter with the specified RSA key.
            RSAOAEPKeyExchangeFormatter keyEncryptor =
                new RSAOAEPKeyExchangeFormatter(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 RSAOAEPKeyExchangeDeformatter class to decode the// specified message.private string DecodeMessage(byte[] encodedMessage)
    {
        string decodedMessage = null;

        try
        {
            // Construct a deformatter with the specified RSA key.
            RSAOAEPKeyExchangeDeformatter keyDecryptor =
                new RSAOAEPKeyExchangeDeformatter(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 RSAOAEPKeyExchangeFormatter object with a new RSA key.// Display its properties to the console.privatevoid ConstructFormatter()
    {
        // Construct an empty Optimal Asymmetric Encryption Padding (OAEP)// key exchange.
        RSAOAEPKeyExchangeFormatter rsaFormatter =
            new RSAOAEPKeyExchangeFormatter();

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

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

        // Export InverseQ and set it into the RSAOAEPKeyExchangeFormatter.
        rsaFormatter.Parameter = key.ExportParameters(true).InverseQ;

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

        Console.WriteLine();
        Console.Write("The RSA formatter contains the following InverseQ");
        Console.WriteLine(" parameter:");
        Console.WriteLine(Encoding.ASCII.GetString(rsaFormatter.Parameter));

        Console.WriteLine();
        string xmlParameters = rsaFormatter.Parameters;

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

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

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

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

        string xmlParameters = rsaDeformatter.Parameters;

        Console.WriteLine();
        Console.WriteLine("The RSA deformatter has the following ");
        Console.WriteLine("parameters:" + xmlParameters);
    }
}
//// This sample produces the following output://// Encoding the following message:// A phrase to be encoded.// Resulting message encoded: %?}T:v??xu?eD)YucItjwuALH HB,Uj??2xq?.?s45// ?f?L2?=X?CPzWx???"q5?6&N"AE,Z+T?(]S?_7~,?G{?VV!:S?df?
// Resulting message decoded:// A phrase to be encoded.//// **System.Security.Cryptography.RSAOAEPKeyExchangeFormatter**// The following random number was generated using the class:// System.Security.Cryptography.RNGCryptoServiceProvider//// The RSA formatter contains the following InverseQ parameter:// 3MM??]D#?mBq_;:ws^1?ko??,_ ??A[hyWcP$?`v.>@?^!dU%\?H0N'??Ca?Ns//// The RSA formatter has the following parameters:////// **System.Security.Cryptography.RSAOAEPKeyExchangeDeformatter**//// The RSA deformatter has the following// parameters:// This sample completed successfully,  press enter to continue.
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 continue.");
        Console.ReadLine();
    } //main

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

    // Use the RSAOAEPKeyExchangeDeformatter class to decode the 
    // specified message.
    private ubyte[] EncodeMessage(String message)
    {
        ubyte encodedMessage[] = null;
        try {
            // Construct a formatter with the specified RSA key.
            RSAOAEPKeyExchangeFormatter keyEncryptor 
                = new RSAOAEPKeyExchangeFormatter(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 RSAOAEPKeyExchangeDeformatter class to decode the
    // specified message.
    private String DecodeMessage(ubyte encodedMessage[])
    {
        String decodedMessage = null;
        try {
            // Construct a deformatter with the specified RSA key.
            RSAOAEPKeyExchangeDeformatter keyDecryptor 
                = new RSAOAEPKeyExchangeDeformatter(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 RSAOAEPKeyExchangeFormatter 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.
        RSAOAEPKeyExchangeFormatter rsaFormatter 
            = new RSAOAEPKeyExchangeFormatter();

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

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

        // Export InverseQ and set it into the RSAOAEPKeyExchangeFormatter.
        rsaFormatter.set_Parameter(key.ExportParameters(true).InverseQ);

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

        Console.WriteLine();
        Console.Write("The RSA formatter contains the following InverseQ");
        Console.WriteLine(" parameter:");
        Console.WriteLine(Encoding.get_ASCII().GetString(rsaFormatter.
            get_Parameter()));

        Console.WriteLine();
        String xmlParameters = rsaFormatter.get_Parameters();

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

    // Create an RSAOAEPKeyExchangeDeformatter object with a new RSA key.
    // Display its properties to the console.
    private void ConstructDeformatter()
    {
        // Construct an empty OAEP key exchange.
        RSAOAEPKeyExchangeDeformatter rsaDeformatter 
            = new RSAOAEPKeyExchangeDeformatter();
        // Create an RSAKey and set it into the specified 
        // RSAOAEPKeyExchangeFormatter.
        RSA key = RSA.Create();
        rsaDeformatter.SetKey(key);
        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
//
// This sample produces the following output:
//
// Encoding the following message:
// A phrase to be encoded.
// Resulting message encoded: %?}T:v??xu?eD)YucItjwuALH HB,Uj??2xq?.?s45
// ?f?L2?=X?CPzWx???"q5?6&N"AE,Z+T?(]S?_7~,?G{?VV!:S?df?
// Resulting message decoded:
// A phrase to be encoded.
//
// **System.Security.Cryptography.RSAOAEPKeyExchangeFormatter**
// The following random number was generated using the class:
// System.Security.Cryptography.RNGCryptoServiceProvider
//
// The RSA formatter contains the following InverseQ parameter:
// 3MM??]D#?mBq_;:ws^1?ko??,_ ??A[hyWcP$?`v.>@?^!dU%\?H0N'??Ca?Ns
//
// The RSA formatter has the following parameters:
//
//
// **System.Security.Cryptography.RSAOAEPKeyExchangeDeformatter**
//
// The RSA deformatter has the following
// parameters:
// This sample completed successfully,  press enter to continue.


System.Object
  System.Security.Cryptography.AsymmetricKeyExchangeDeformatter
    System.Security.Cryptography.RSAOAEPKeyExchangeDeformatter

Quaisquer membros static (Shared no Visual Basic) públicos deste tipo são thread-safe. Não há garantia de que qualquer membro de instância seja thread-safe.

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

o.NET Framework e.NET Compact Framework não oferecem suporte a todas as versões de cada plataforma. Para obter uma lista de versões suportadas, consulte Requisitos de sistema do .NET framework.

.NET Framework

Compatível com: 3.5, 3.0, 2.0, 1.1, 1.0
Mostrar: