从Windows证书存储区将基于ECC的证书导入CngKey [英] Importing ECC-based certificate from the Windows Certificate Store into CngKey

查看:157
本文介绍了从Windows证书存储区将基于ECC的证书导入CngKey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将基于ECC的 X509Certificate2 的公钥/私钥转换为 CngKey 的公钥/私钥与 ECDsaCng ECDiffieHellmanCng 一起使用?

How can I get the public/private keys from an ECC-based X509Certificate2's into CngKey's for use with ECDsaCng and ECDiffieHellmanCng?

I' m当前使用RSA 2048位密钥对来签名/加密内容。为此,我从 X509Store 提取证书,并使用标记为不可导出的私钥安全地存储这些证书。我想将当前实现转换为使用ECDSA和ECDH,以便可以使用较小的密钥大小来实现同等的安全性。

I'm currently using RSA 2048 bit key pairs to sign/encrypt stuff. I'm doing this by pulling the certificates from the X509Store where they are securely stored with private keys marked as non-exportable. I would like to convert the current implementation to use ECDSA and ECDH so that I can use smaller key sizes for equivalent security.

我已经使用openssl成功生成了ECC证书:

I've successfully generated ECC certs using openssl:


  1. openssl ecparam -out private.pem -name prime256v1 -genkey

  2. openssl req -new -key private.pem -x509 -nodes -days 365 -out public.cer

  3. openssl pkcs12 -export -in public.cer -inkey private.pem -out export.pfx

  1. openssl ecparam -out private.pem -name prime256v1 -genkey
  2. openssl req -new -key private.pem -x509 -nodes -days 365 -out public.cer
  3. openssl pkcs12 -export -in public.cer -inkey private.pem -out export.pfx

我已成功将上述生成的证书安装到证书存储中。我可以通过指纹来检索它们,但是私钥和公钥的加密提供程序会引发不支持算法异常。相反,我知道我应该使用 ECDsaCng ECDiffieHellmanCng 进行签名/加密。但是这些交易是用 CngKey 的。

I've successfully installed the above generated certs in to the cert store. I can retrieve them by thumbprint, but the crypto providers for the private and public keys throw "Algorithm not supported" exceptions. Instead, I understand I'm supposed to use ECDsaCng and ECDiffieHellmanCng to sign/encrypt. But these deal in CngKey's.

Bouncy Castle是不可行的,因为它需要私钥才能

Bouncy Castle isn't an option because it requires the private keys to be exportable.

CLR安全性会给我一个 CngKey 通过 GetCngPrivateKey 对,但不能与ECDsa一起使用,因为CLRSecurity返回的密钥是ECDH密钥。此外,CLR安全性还没有提供一种从 X509Certificate2 中获取公钥进行签名验证的方法(在该情况下,我甚至没有或不需要

CLR Security will return me a CngKey pair via GetCngPrivateKey but it cannot be used with ECDsa because the key returned by CLRSecurity is an ECDH key. Furthermore CLR Security doesn't give me a way to get just the public key from an X509Certificate2 for signature verification (where I don't even have or need the private key of the signer).

有什么想法吗?我不知所措...任何帮助将不胜感激。

Any ideas? I'm at my wits end... Any help would be much appreciated.

推荐答案

您需要从证书的公钥创建CngKey:

You need to create the CngKey from the public key of the certificate:


certificate.PublicKey.EncodedKeyValue.RawData

certificate.PublicKey.EncodedKeyValue.RawData

CngKey包含8个附加字节,前4个字节用于所用曲线的名称(ECS1,ECS3或ECS5),后4个是长度的关键包括填充(32、48或66)。

The CngKey contains 8 additional bytes, the first 4 bytes are used for the name of the curve used (ECS1, ECS3 or ECS5), the last 4 are the length of the key incl. padding (32, 48 or 66).

从证书中删除公钥的第一个字节(因为ECDSA公钥始终为0x04)。

The first byte of the public key from the certificate is removed (as it is always 0x04 for ECDSA public key).

例如,对于使用P-256曲线和SHA-256哈希算法的ECDSA,您将获得长度为65个字节的公共密钥。舍弃第一个字节,保留64个字节,然后为前缀添加4个字节的曲线和4个字节的键长度,即(Encoding.ASCII)

So for instance for ECDSA using P-256 curve and SHA-256 hash algorithm, you will get a public key of length 65 bytes. Discard the first byte, leaving 64 bytes, then prefix with 4 bytes for curve and 4 bytes for key length i.e. (Encoding.ASCII):


69(E)

69 (E)

67(C)

83(S)

49(1)

32(密钥长度)

0

0

0

现在您具有公共密钥(72个字节)来从以下位置创建CngKey:

Now you have the public key (72 bytes) to create the CngKey from:


var cngKey = CngKey.Import([byte array],CngKeyBlobFormat.EccPublicBlob);

var cngKey = CngKey.Import([the byte array], CngKeyBlobFormat.EccPublicBlob);

var ecdsaCng = new ECDsaCng(cngKey);

var ecdsaCng = new ECDsaCng(cngKey);

您可以验证签名:


返回ecdsaCng.VerifyData(encodedBytes,签名);

return ecdsaCng.VerifyData(encodedBytes, signature);

这篇关于从Windows证书存储区将基于ECC的证书导入CngKey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆