AsymmetricKeyAlgorithmProvider 클래스

정의

비대칭(공개) 키 알고리즘의 공급자를 나타냅니다. 자세한 내용은 암호화 키를 참조하세요.

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
상속
Object Platform::Object IInspectable AsymmetricKeyAlgorithmProvider
특성

Windows 요구 사항

디바이스 패밀리
Windows 10 (10.0.10240.0에서 도입되었습니다.)
API contract
Windows.Foundation.UniversalApiContract (v1.0에서 도입되었습니다.)

예제

비대칭형 암호화는 대칭형 암호화보다 느리므로 많은 양의 데이터를 직접 암호화하는 데는 거의 사용되지 않습니다. 대신 일반적으로 다음과 같은 방식으로 키를 암호화하는 데 사용됩니다.

  • Alice는 Bob에게 암호화된 메시지만 보내도록 요청합니다.
  • Alice는 프라이빗/공개 키 쌍을 만들어 프라이빗 키 암호는 보관하고 공개 키를 게시합니다.
  • Bob이 Alice에게 보낼 메시지가 있습니다.
  • Bob은 대칭 키를 만듭니다.
  • Bob은 새 대칭 키를 사용하여 Alice에게 보낼 메시지를 암호화합니다.
  • Bob은 Alice의 공개 키를 사용하여 대칭 키를 암호화합니다.
  • Bob은 암호화된 메시지와 암호화된 대칭 키를 Alice에게 보냅니다(봉함).
  • Alice는 프라이빗/공개 쌍에 있는 자신의 프라이빗 키를 사용하여 Bob의 대칭 키 암호를 해독합니다.
  • Alice는 Bob의 대칭 키를 사용하여 메시지의 암호를 해독합니다. 코드에서 해결할 수 있는 이전 프로세스의 측면은 다음 예제에서 보여 줍니다.

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);
        }
    }
}

설명

정적 OpenAlgorithm 메서드를 호출하여 AsymmetricKeyAlgorithmProvider 개체를 만듭니다.

속성

AlgorithmName

열린 비대칭 알고리즘의 이름을 가져옵니다.

메서드

CreateKeyPair(UInt32)

퍼블릭/프라이빗 키 쌍을 만듭니다.

CreateKeyPairWithCurveName(String)

알고리즘 곡선 이름을 사용하여 퍼블릭/프라이빗 키 쌍을 만듭니다.

CreateKeyPairWithCurveParameters(Byte[])

곡선 매개 변수를 사용하여 비대칭 퍼블릭/프라이빗 키 쌍을 만듭니다.

ImportKeyPair(IBuffer)

버퍼에서 퍼블릭/프라이빗 키 쌍을 가져옵니다.

ImportKeyPair(IBuffer, CryptographicPrivateKeyBlobType)

지정된 형식의 버퍼에서 퍼블릭/프라이빗 키 쌍을 가져옵니다.

ImportPublicKey(IBuffer)

퍼블릭 키를 버퍼로 가져옵니다.

ImportPublicKey(IBuffer, CryptographicPublicKeyBlobType)

지정된 형식의 버퍼로 공개 키를 가져옵니다.

OpenAlgorithm(String)

AsymmetricKeyAlgorithmProvider 클래스의 instance 만들고 사용할 지정된 알고리즘을 엽니다.

적용 대상

추가 정보