RSAOAEPKeyExchangeDeformatter.DecryptKeyExchange(Byte[]) 方法

定义

从加密的密钥交换数据中提取机密信息。

public:
 override cli::array <System::Byte> ^ DecryptKeyExchange(cli::array <System::Byte> ^ rgbData);
public override byte[] DecryptKeyExchange (byte[] rgbData);
override this.DecryptKeyExchange : byte[] -> byte[]
Public Overrides Function DecryptKeyExchange (rgbData As Byte()) As Byte()

参数

rgbData
Byte[]

其中隐藏了机密信息的密钥交换数据。

返回

Byte[]

从密钥交换数据派生的机密信息。

例外

密钥交换数据验证已失败。

示例

以下示例演示如何使用 DecryptKeyExchange 方法重新创建来自邮件发件人的交换密钥。 此代码示例是为 RSAPKCS1KeyExchangeDeformatter 类提供的一个更大示例的一部分。

public void Receive(byte[] iv, byte[] encryptedSessionKey, byte[] encryptedMessage)
{

    using (Aes aes = new AesCryptoServiceProvider())
    {
        aes.IV = iv;

        // Decrypt the session key
        RSAOAEPKeyExchangeDeformatter keyDeformatter = new RSAOAEPKeyExchangeDeformatter(rsaKey);
        aes.Key = keyDeformatter.DecryptKeyExchange(encryptedSessionKey);

        // Decrypt the message
        using (MemoryStream plaintext = new MemoryStream())
        using (CryptoStream cs = new CryptoStream(plaintext, aes.CreateDecryptor(), CryptoStreamMode.Write))
        {
            cs.Write(encryptedMessage, 0, encryptedMessage.Length);
            cs.Close();

            string message = Encoding.UTF8.GetString(plaintext.ToArray());
            Console.WriteLine(message);
        }
    }
}
Public Sub Receive(ByVal iv() As Byte, ByVal encryptedSessionKey() As Byte, ByVal encryptedMessage() As Byte)

    Using aes = New AesCryptoServiceProvider()

        aes.IV = iv

        ' Decrypt the session key
        Dim keyDeformatter As New RSAOAEPKeyExchangeDeformatter(rsaKey)
        aes.Key = keyDeformatter.DecryptKeyExchange(encryptedSessionKey)

        ' Decrypt the message
        Using plaintext As New MemoryStream()
            Using cs As New CryptoStream(plaintext, aes.CreateDecryptor(), CryptoStreamMode.Write)
                    cs.Write(encryptedMessage, 0, encryptedMessage.Length)
                    cs.Close()

                    Dim message As String = Encoding.UTF8.GetString(plaintext.ToArray())
                    Console.WriteLine(message)
            End Using
        End Using
    End Using

End Sub

注解

在调用此方法之前,必须指定一个键。

适用于

另请参阅