AsymmetricKeyExchangeDeformatter Class

Definition

Represents the base class from which all asymmetric key exchange deformatters derive.

C#
public abstract class AsymmetricKeyExchangeDeformatter
C#
[System.Runtime.Versioning.UnsupportedOSPlatform("browser")]
public abstract class AsymmetricKeyExchangeDeformatter
C#
[System.Runtime.InteropServices.ComVisible(true)]
public abstract class AsymmetricKeyExchangeDeformatter
Inheritance
AsymmetricKeyExchangeDeformatter
Derived
Attributes

Examples

The following example demonstrates how to extend the AsymmetricKeyExchangeDeformatter class.

C#
using System;
using System.Security.Cryptography;

namespace Contoso
{
    public class ContosoDeformatter : AsymmetricKeyExchangeDeformatter
    {
        private RSA _rsaKey;

        // Default constructor.
        public ContosoDeformatter() { }

        // Constructor with the public key to use for encryption.
        public ContosoDeformatter(AsymmetricAlgorithm key)
        {
            SetKey(key);
        }

        // Set the public key for encyption operations.
        public override void SetKey(AsymmetricAlgorithm key)
        {
            if (key != null)
            {
                _rsaKey = (RSA)key;
            }
            else
            {
                throw new ArgumentNullException(nameof(key));
            }
        }

        // Disallow access to the parameters of the formatter.
        public override string Parameters
        {
            get { return null; }
            set {; }
        }

        // Create the encrypted key exchange data from the specified input
        // data. This method uses the RSA class only. To
        // support additional providers or provide custom decryption logic,
        // add logic to this member.
        public override byte[] DecryptKeyExchange(byte[] rgbData)
        {
            byte[] decryptedBytes = null;

            if (_rsaKey != null)
            {
                if (_rsaKey is RSA rsa)
                {
                    decryptedBytes = rsa.Decrypt(rgbData, RSAEncryptionPadding.OaepSHA1);
                }
                // Add custom decryption logic here.
            }
            else
            {
                throw new CryptographicUnexpectedOperationException(
                    "Cryptography_MissingKey");
            }

            return decryptedBytes;
        }
    }
}
//
// This code example produces the following output:
//
// Data to encrypt : Sample Contoso encryption application.
// Encrypted data: Khasdf-3248&$%23
// Data decrypted : Sample Contoso encryption application.
// 
// This sample completed successfully; press Enter to exit.

Remarks

Asymmetric key exchange deformatters decrypt key exchange data.

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.

Caution

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.

Constructors

Properties

Parameters

When overridden in a derived class, gets or sets the parameters for the asymmetric key exchange.

Methods

DecryptKeyExchange(Byte[])

When overridden in a derived class, extracts secret information from the encrypted key exchange data.

Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
SetKey(AsymmetricAlgorithm)

When overridden in a derived class, sets the private key to use for decrypting the secret information.

ToString()

Returns a string that represents the current object.

(Inherited from Object)

Applies to

Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1