CryptographicKey 类

定义

表示对称密钥或非对称密钥对。

public ref class CryptographicKey sealed
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Foundation.UniversalApiContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
class CryptographicKey final
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
public sealed class CryptographicKey
Public NotInheritable Class CryptographicKey
继承
Object Platform::Object IInspectable CryptographicKey
属性

Windows 要求

设备系列
Windows 10 (在 10.0.10240.0 中引入)
API contract
Windows.Foundation.UniversalApiContract (在 v1.0 中引入)

示例

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

namespace SampleCryptographicKey
{
    sealed partial class CryptographicKeyApp : Application
    {
        static IBuffer buffKeyPair;

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

            // Create an asymmetric key pair.
            String strAsymmetricAlgName = AsymmetricAlgorithmNames.RsaPkcs1;
            UInt32 asymmetricKeyLength = 512;
            IBuffer buffPublicKey = this.SampleCreateAsymmetricKeyPair(
                strAsymmetricAlgName,
                asymmetricKeyLength);
        }

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

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

            // Export the public key to a buffer for use by others.
            IBuffer 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.
            CryptographicKeyApp.buffKeyPair = keyPair.Export();

            // Retrieve the size of the key pair.
            UInt32 lengthKeyPair = keyPair.KeySize;

            // Return the public key.
            return buffPublicKey;
        }
    }
}

注解

使用在以下类中创建或导入密钥的方法时,会创建 CryptographicKey 对象:

属性

KeySize

获取密钥的大小(以位为单位)。

方法

Export()

将密钥对导出到缓冲区。

Export(CryptographicPrivateKeyBlobType)

在给定指定格式的情况下,将密钥对导出到缓冲区。

ExportPublicKey()

将公钥导出到缓冲区。

ExportPublicKey(CryptographicPublicKeyBlobType)

在给定指定格式的情况下,将公钥导出到缓冲区。

适用于

另请参阅