RSAPKCS1KeyExchangeDeformatter.DecryptKeyExchange(Byte[]) 方法

定义

从加密的密钥交换数据中提取机密信息。Extracts secret information from the encrypted key exchange data.

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

参数

rgbIn
Byte[]

其中隐藏了机密信息的密钥交换数据。The key exchange data within which the secret information is hidden.

返回

Byte[]

从密钥交换数据派生的机密信息。The secret information derived from the key exchange data.

例外

缺少密钥。The key is missing.

示例

下面的示例演示如何使用 DecryptKeyExchange 方法从消息发送方重新创建交换密钥。The following example shows how to use the DecryptKeyExchange method to recreate an exchange key from a message sender. 此代码示例是为类提供的更大示例的一部分 RSAPKCS1KeyExchangeDeformatterThis code example is part of a larger example provided for the RSAPKCS1KeyExchangeDeformatter class.

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

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

        // Decrypt the session key
        RSAPKCS1KeyExchangeDeformatter keyDeformatter = new RSAPKCS1KeyExchangeDeformatter(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)

    Dim aes = New AesCryptoServiceProvider()
    Try
        aes.IV = iv

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

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

                Dim message As String = Encoding.UTF8.GetString(plaintext.ToArray())
                Console.WriteLine(message)
            Finally
                cs.Dispose()
            End Try
        Finally
            plaintext.Dispose()
        End Try
    Finally
        aes.Dispose()
    End Try

End Sub

注解

在调用此方法之前,必须指定一个密钥。You must specify a key before calling this method.

适用于