How to Export Private Key For ECDiffieHellmanCng

Kevin Junghans

I am trying to export the keys from a new instance of a ECDiffieHellmanCng object so I can create an instance of it later with the same keys. But I am getting an error when trying to export it.

//Create new ECDiffieHellmanCng which automatically creates new keys
var ecdh = new ECDiffieHellmanCng();
//Export the keys
var privateKey = ecdh.Key.Export(CngKeyBlobFormat.EccPrivateBlob);

I am getting a CryptographicException when I call the Export method with the message "The requested operation is not supported." After putting some breakpoints in the code it looks like it is throwing the exception before even executing the method. Looking at the definition of the Export method it is adorned with a SecuritySafeCriticalAttribute so I am suspicious that this attribute is actually throwing the exception. What is causing this exception? How can I save the keys so I can create an instance of the same ECDiffieHellmanCng object at a later time?

vcsjones

By default, keys aren't exportable - they are securely stored in the KSP. When creating the key, it needs to be marked allowed for export. Example:

var ecdh = new ECDiffieHellmanCng(CngKey.Create(CngAlgorithm.ECDiffieHellmanP256, null, new CngKeyCreationParameters {ExportPolicy = CngExportPolicies.AllowPlaintextExport}));
//Export the keys
var privateKey = ecdh.Key.Export(CngKeyBlobFormat.EccPrivateBlob);

To make this simpler, we can just export it from the CngKey directly and not use the algorithm if all you want to do is create a new key and export the private key.

var cngKey = CngKey.Create(CngAlgorithm.ECDiffieHellmanP256, null, new CngKeyCreationParameters {ExportPolicy = CngExportPolicies.AllowPlaintextExport});
var privateKey = cngKey.Export(CngKeyBlobFormat.EccPrivateBlob);

You can re-create the CngKey from the exported blob by using CngKey.Import(yourBlob, CngKeyBlobFormat.EccPrivateBlob) and passing that to the constructor of ECDiffieHellmanCng.


SecuritySafeCriticalAttribute is part of the .NET Security Transparency model. It is not the source of your errors.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to export a GPG private key and public key to a file

From Dev

How to export Certificate and private key as a single file using java code?

From Java

How to convert a private key to an RSA private key?

From Dev

How do I export a single private key from the command line in OSX?

From Dev

How can I export a (PKCS#8?) private key in Putty or Puttygen readable format?

From Dev

How to rsync with a private key?

From Dev

Export Token Signing certificate private key from ADFS

From Dev

How to retrieve passphrase for private key?

From Dev

How to find Private Key Location

From Dev

How to retrieve passphrase for private key?

From Dev

How to encrypt a file with private key

From Dev

How to install ssh private key?

From Dev

How to convert String to Private key?

From Dev

ECC key pair - how to print private key?

From Dev

How to store private key in Key Container?

From Dev

How to export symmetric encryption key?

From Dev

How to export symmetric encryption key?

From Dev

Use PHP to generate a public/private key pair and export public key as a .der encoded string

From Dev

How to obtain private RSA key(private) as byte array in pgp file?

From Dev

How to obtain private RSA key(private) as byte array in pgp file?

From Dev

How to encrypt data with RSA private key in python?

From Dev

How to store ECDSA private key in Go

From Dev

How to calculate the coefficient of a rsa private key?

From Dev

php openssl: how to match the private key with the certificate

From Dev

How to enter private key password with ansible

From Dev

How to generate certificate if private key is in HSM?

From Dev

JSch how to use with PuTTY private key

From Dev

How to use encrypted RSA private key with PyCrypto?

From Dev

How to load a private key from a JWK into openSSL?

Related Related

  1. 1

    How to export a GPG private key and public key to a file

  2. 2

    How to export Certificate and private key as a single file using java code?

  3. 3

    How to convert a private key to an RSA private key?

  4. 4

    How do I export a single private key from the command line in OSX?

  5. 5

    How can I export a (PKCS#8?) private key in Putty or Puttygen readable format?

  6. 6

    How to rsync with a private key?

  7. 7

    Export Token Signing certificate private key from ADFS

  8. 8

    How to retrieve passphrase for private key?

  9. 9

    How to find Private Key Location

  10. 10

    How to retrieve passphrase for private key?

  11. 11

    How to encrypt a file with private key

  12. 12

    How to install ssh private key?

  13. 13

    How to convert String to Private key?

  14. 14

    ECC key pair - how to print private key?

  15. 15

    How to store private key in Key Container?

  16. 16

    How to export symmetric encryption key?

  17. 17

    How to export symmetric encryption key?

  18. 18

    Use PHP to generate a public/private key pair and export public key as a .der encoded string

  19. 19

    How to obtain private RSA key(private) as byte array in pgp file?

  20. 20

    How to obtain private RSA key(private) as byte array in pgp file?

  21. 21

    How to encrypt data with RSA private key in python?

  22. 22

    How to store ECDSA private key in Go

  23. 23

    How to calculate the coefficient of a rsa private key?

  24. 24

    php openssl: how to match the private key with the certificate

  25. 25

    How to enter private key password with ansible

  26. 26

    How to generate certificate if private key is in HSM?

  27. 27

    JSch how to use with PuTTY private key

  28. 28

    How to use encrypted RSA private key with PyCrypto?

  29. 29

    How to load a private key from a JWK into openSSL?

HotTag

Archive