Partager via


AsymmetricKeyAlgorithmProvider Classe

Définition

Représente un fournisseur d’algorithmes de clé asymétrique (publique). Pour plus d’informations, consultez Clés de chiffrement.

public ref class AsymmetricKeyAlgorithmProvider sealed
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
/// [Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
class AsymmetricKeyAlgorithmProvider final
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
[Windows.Foundation.Metadata.Threading(Windows.Foundation.Metadata.ThreadingModel.Both)]
public sealed class AsymmetricKeyAlgorithmProvider
Public NotInheritable Class AsymmetricKeyAlgorithmProvider
Héritage
Object Platform::Object IInspectable AsymmetricKeyAlgorithmProvider
Attributs

Configuration requise pour Windows

Famille d’appareils
Windows 10 (introduit dans 10.0.10240.0)
API contract
Windows.Foundation.UniversalApiContract (introduit dans v1.0)

Exemples

Le chiffrement asymétrique étant beaucoup plus lent que le chiffrement symétrique, il est rarement utilisé pour chiffrer de grandes quantités de données directement. Il est généralement plutôt utilisé selon les manières suivantes pour chiffrer des clés.

  • Alice nécessite que Bob ne lui envoie que des messages chiffrés.
  • Alice crée une paire de clés privées ou publiques, garde sa clé privée secrète et publie sa clé publique.
  • Bob veut envoyer un message à Alice.
  • Bob crée une clé symétrique.
  • Bob utilise une nouvelle clé symétrique pour chiffrer son message pour Alice.
  • Bob utilise la clé publique d’Alice pour chiffrer sa clé symétrique.
  • Bob envoie le message chiffré et la clé symétrique chiffrée à Alice (enveloppée).
  • Alice utilise sa clé privée (à partir de sa paire de clés privées ou publiques) pour déchiffrer la clé symétrique de Bob.
  • Alice utilise la clé symétrique de Bob pour déchiffrer le message. Les aspects du processus précédent qui peuvent être traités dans le code sont illustrés dans l’exemple suivant.

using Windows.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using Windows.Storage.Streams;

namespace SampleAsymmetricKeyAlgorithmProvider
{
    sealed partial class AsymmetricKeyAlgorithmApp : Application
    {
        static IBuffer buffKeyPair;

        public AsymmetricKeyAlgorithmApp()
        {
            // Initialize the application.
            this.InitializeComponent();

            // Create a symmetric session key.
            String strSymmetricAlgName = SymmetricAlgorithmNames.AesCbc;
            UInt32 symmetricKeyLength = 32;
            IBuffer buffSessionKey;
            this.SampleCreateSymmetricSessionKey(
                strSymmetricAlgName, 
                symmetricKeyLength, 
                out buffSessionKey);

            // Create an asymmetric key pair.
            String strAsymmetricAlgName = AsymmetricAlgorithmNames.RsaPkcs1;
            UInt32 asymmetricKeyLength = 512;
            IBuffer buffPublicKey;
            this.SampleCreateAsymmetricKeyPair(
                strAsymmetricAlgName, 
                asymmetricKeyLength, 
                out buffPublicKey);
 
            // Encrypt the symmetric session key by using the asymmetric public key.
            IBuffer buffEncryptedSessionKey;
            this.SampleAsymmetricEncryptSessionKey(
                strAsymmetricAlgName,
                buffSessionKey,
                buffPublicKey,
                out buffEncryptedSessionKey);

            // Decrypt the symmetric session key by using the asymmetric private key
            // that corresponds to the public key used to encrypt the session key.
            this.SampleAsymmetricDecryptSessionKey(
                strAsymmetricAlgName,
                strSymmetricAlgName,
                buffEncryptedSessionKey);
        }

        public void SampleCreateSymmetricSessionKey(
            string strSymmetricAlgName,
            UInt32 keyLength,
            out IBuffer buffSessionKey)
        {
            // Open a symmetric algorithm provider for the specified algorithm.
            SymmetricKeyAlgorithmProvider objAlg = SymmetricKeyAlgorithmProvider.OpenAlgorithm(strSymmetricAlgName);

            // Create a symmetric session key.
            IBuffer keyMaterial = CryptographicBuffer.GenerateRandom(keyLength);
            CryptographicKey sessionKey = objAlg.CreateSymmetricKey(keyMaterial);

            buffSessionKey = keyMaterial;
        }

        public void SampleCreateAsymmetricKeyPair(
            String strAsymmetricAlgName,
            UInt32 keyLength,
            out IBuffer buffPublicKey)
        {
            // Open the algorithm provider for the specified asymmetric algorithm.
            AsymmetricKeyAlgorithmProvider objAlgProv = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(strAsymmetricAlgName);

            // Demonstrate use of the AlgorithmName property (not necessary to create a key pair).
            String strAlgName = objAlgProv.AlgorithmName;

            // Create an asymmetric key pair.
            CryptographicKey keyPair = objAlgProv.CreateKeyPair(keyLength);

            // Export the public key to a buffer for use by others.
            buffPublicKey = keyPair.ExportPublicKey();

            // You should keep your private key (embedded in the key pair) secure. For  
            // the purposes of this example, however, we're just copying it into a
            // static class variable for later use during decryption.
            AsymmetricKeyAlgorithmApp.buffKeyPair = keyPair.Export();
        }
 
        public void SampleAsymmetricEncryptSessionKey(
            String strAsymmetricAlgName,
            IBuffer buffSessionKeyToEncrypt,
            IBuffer buffPublicKey,
            out IBuffer buffEncryptedSessionKey)
        {
            // Open the algorithm provider for the specified asymmetric algorithm.
            AsymmetricKeyAlgorithmProvider objAlgProv = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(strAsymmetricAlgName);

            // Import the public key from a buffer.
            CryptographicKey publicKey = objAlgProv.ImportPublicKey(buffPublicKey);

            // Encrypt the session key by using the public key.
            buffEncryptedSessionKey = CryptographicEngine.Encrypt(publicKey, buffSessionKeyToEncrypt, null);
        }

        public void SampleAsymmetricDecryptSessionKey(
            String strAsymmetricAlgName,
            String strSymmetricAlgName,
            IBuffer buffEncryptedSessionKey)
        {
            // Open the algorithm provider for the specified asymmetric algorithm.
            AsymmetricKeyAlgorithmProvider objAsymmAlgProv = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(strAsymmetricAlgName);

            // Import the public key from a buffer. You should keep your private key
            // secure. For the purposes of this example, however, the private key is
            // just stored in a static class variable.
            CryptographicKey keyPair = objAsymmAlgProv.ImportKeyPair(AsymmetricKeyAlgorithmApp.buffKeyPair);

            // Use the private key embedded in the key pair to decrypt the session key.
            IBuffer buffDecryptedSessionKey = CryptographicEngine.Decrypt(keyPair, buffEncryptedSessionKey, null);

            // Convert the decrypted session key into a CryptographicKey object that
            // can be used to decrypt the message that it previously encrypted (not shown).
            SymmetricKeyAlgorithmProvider objSymmAlgProv = SymmetricKeyAlgorithmProvider.OpenAlgorithm(strSymmetricAlgName);
            CryptographicKey sessionKey = objSymmAlgProv.CreateSymmetricKey(buffDecryptedSessionKey);
        }
    }
}

Remarques

Vous créez un objet AsymmetricKeyAlgorithmProvider en appelant la méthode OpenAlgorithm statique.

Propriétés

AlgorithmName

Obtient le nom de l’algorithme asymétrique ouvert.

Méthodes

CreateKeyPair(UInt32)

Crée une paire de clés publique/privée.

CreateKeyPairWithCurveName(String)

Crée une paire de clés publique/privée à l’aide d’un nom de courbe algorithmique.

CreateKeyPairWithCurveParameters(Byte[])

Crée une paire de clés publique/privée asymétrique à l’aide de paramètres de courbe.

ImportKeyPair(IBuffer)

Importe une paire de clés publique/privée à partir d’une mémoire tampon.

ImportKeyPair(IBuffer, CryptographicPrivateKeyBlobType)

Importe une paire de clés publique/privée à partir d’une mémoire tampon au format spécifié.

ImportPublicKey(IBuffer)

Importe une clé publique dans une mémoire tampon.

ImportPublicKey(IBuffer, CryptographicPublicKeyBlobType)

Importe une clé publique dans une mémoire tampon pour un format spécifié.

OpenAlgorithm(String)

Crée un instance de la classe AsymmetricKeyAlgorithmProvider et ouvre l’algorithme spécifié à utiliser.

S’applique à

Voir aussi