CryptographicKey
CryptographicKey
CryptographicKey
CryptographicKey
Class
Definition
Represents a symmetric key or an asymmetric key pair.
public : sealed class CryptographicKey : ICryptographicKeypublic sealed class CryptographicKey : ICryptographicKeyPublic NotInheritable Class CryptographicKey Implements ICryptographicKey// You can use this class in JavaScript.
- Attributes
Windows 10 requirements
| Device family |
Windows 10 (introduced v10.0.10240.0)
|
| API contract |
Windows.Foundation.UniversalApiContract (introduced v1)
|
Examples
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;
}
}
}
Remarks
A CryptographicKey object is created when you use methods that create or import keys in the following classes:
- AsymmetricKeyAlgorithmProvider
- KeyDerivationAlgorithmProvider
- MacAlgorithmProvider
- SymmetricKeyAlgorithmProvider
Properties
KeySize KeySize KeySize KeySize
Gets the size, in bits, of the key.
public : unsigned int KeySize { get; }public uint KeySize { get; }Public ReadOnly Property KeySize As uint// You can use this property in JavaScript.
- Value
- unsigned int uint uint uint
Integer that contains the number of bits in the key.
Methods
Export() Export() Export() Export()
Exports the key pair to a buffer.
public : IBuffer Export()public IBuffer Export()Public Function Export() As IBuffer// You can use this method in JavaScript.
Returns
- See Also
Export(CryptographicPrivateKeyBlobType) Export(CryptographicPrivateKeyBlobType) Export(CryptographicPrivateKeyBlobType) Export(CryptographicPrivateKeyBlobType)
Exports the key pair to a buffer given a specified format.
public : IBuffer Export(CryptographicPrivateKeyBlobType BlobType)public IBuffer Export(CryptographicPrivateKeyBlobType BlobType)Public Function Export(BlobType As CryptographicPrivateKeyBlobType) As IBuffer// You can use this method in JavaScript.
Parameters
- BlobType
- CryptographicPrivateKeyBlobType CryptographicPrivateKeyBlobType CryptographicPrivateKeyBlobType CryptographicPrivateKeyBlobType
A CryptographicPrivateKeyBlobType enumeration value that specifies the format of the key in the buffer. The default value is Pkcs8RawPrivateKeyInfo.
Returns
- See Also
ExportPublicKey() ExportPublicKey() ExportPublicKey() ExportPublicKey()
Exports a public key to a buffer.
public : IBuffer ExportPublicKey()public IBuffer ExportPublicKey()Public Function ExportPublicKey() As IBuffer// You can use this method in JavaScript.
Returns
- See Also
ExportPublicKey(CryptographicPublicKeyBlobType) ExportPublicKey(CryptographicPublicKeyBlobType) ExportPublicKey(CryptographicPublicKeyBlobType) ExportPublicKey(CryptographicPublicKeyBlobType)
Exports a public key to a buffer given a specified format.
public : IBuffer ExportPublicKey(CryptographicPublicKeyBlobType BlobType)public IBuffer ExportPublicKey(CryptographicPublicKeyBlobType BlobType)Public Function ExportPublicKey(BlobType As CryptographicPublicKeyBlobType) As IBuffer// You can use this method in JavaScript.
Parameters
- BlobType
- CryptographicPublicKeyBlobType CryptographicPublicKeyBlobType CryptographicPublicKeyBlobType CryptographicPublicKeyBlobType
A CryptographicPublicKeyBlobType enumeration value that specifies the format of the key in the buffer. The default value is X509SubjectPublicKeyInfo.
Returns
- See Also