English 中文(简体)
我如何在窗户网上使用ECC X509证书的公用钥匙加密数据?
原标题:How can I encrypt data using a public key from ECC X509 certificate in .net framework on windows?

我正在使用:

  • Windows 10 (Version 1709, OS Build 17025.1000)
  • .net framework 4.7
  • VS 2017 (version: 15.3.5)

我在这里做的是:

  1. Got a self signed ECC certificate using OpenSSL and steps outlined in the script at https://gist.github.com/sidshetye/4759690 with modifications:

    a) 利用NIST/P-256在256个轨道主要领域进行治疗

    b) Used SHA-256

  2. 将证书从档案中(原步骤中生成)移至X509Certificate2物体

  3. 向窗口信托库进口Peno文档(供测试)。 这是成功的。

  4. Inspection of the imported certificate shows Public Key field as ECC (256 Bits) and Public key parameters as ECDSA_P256 .
  5. Next tried to figure out how to encrypt with this certificate.

我站在最后一步,因为使用X509Certificate2的所有例子主要只使用RSA,我使用ECC证书。 关于RSA证书,在X509Certificate2 和RSA类别中,有GetRSAPublicKey的电离层方法。 然而,欧共体证书没有这种方法。

其次,我 st倒了这个职位()。 • 颁发证书,使用X509Certificate2和ECC公共钥匙,并在以下几处进行审判(尽管如此似乎令人怀疑ECC为何胁迫公用钥匙进入RSA类型):

RSACryptoServiceProvider csp = (RSACryptoServiceProvider)cert.PublicKey.Key

我有以下例外:证书关键算法没有得到支持。

接下来,我 st倒了这个职位()。 从Windows证书仓库向CngKey进口基于ECC的证书,该证书基本试图制造CNGKey型和瞬时的ECDsaCng。 然而,即使我能够与埃德韦里曼卡纳克打交道,也没有任何加密方法。

因此,我不清楚我如何进一步利用ECC X509证书作为加密数据的公共钥匙。

最佳回答

Background

不对称算法有三个不同的目的(我知道)

  1. Encryption
  • RSA is the only "standard" algorithm that can do this directly.
  1. Signature
  • RSA
  • DSA
  • ECDSA
  • ElGamal Signature
  1. Key Agreement
  • Diffie-Hellman (DH)
  • ECDH
  • ElGamal encryption (the asymmetric startup phase)
  • MQV
  • ECMQV

由于RSA加密空间有限,90年代的计算机很难使用,因此RSA加密的主要用途是“Key Transfer”,也就是说,“加密电”只是DES/3DES(AES尚未发明)的不对称加密钥匙——

Key agreement (or transfer) schemes always have to be combined with a protocol/scheme to result in an encryption operation. Such schemes include

  • TLS (nee SSL)
  • CMS or S/MIME encrypted-data
  • IES (Integrated Encryption Scheme)
  • ECIES (Elliptic Curve Integrated Encryption Scheme)
  • ElGamal encryption (holistically)
  • PGP encryption

因此,你可能想要的是ESCIES。

ECIES.Net

Currently (.NET Framework 4.7.1, .NET Core 2.0) there s no support to get an ECDiffieHellman object from a certificate in .NET.

Game? 很可能不是。 除非附有欧盟人权局钥匙的证书明确使用背后的人权算法识别符号(即标准比较高的立方位),否则即可作为欧洲空间法中心开放。 然后,你可以强迫该物体成为欧洲人权法院:

using (ECDsa ecdsa = cert.GetECDsaPublicKey())
{
    return ECDiffieHellman.Create(ecdsa.ExportParameters(false));
}

让我们走下去,把受援的公众目标 off开:

ECDiffieHellmanPublicKey recipientPublic = GetECDHFromCertificate(cert).PublicKey;
ECCurve curve = recipientPublic.ExportParameters().Curve;

因此,我们现在来看。 http://www.secg.org/sec1-v2.pdf。 第5.1节(Elliptic Curve 综合加密计划)

Setup

  1. Choose ANSI-X9.63-KDF with SHA-2-256 as the hash function.
  2. Choose HMAC–SHA-256–256.
  3. Choose AES–256 in CBC mode.
  4. Choose Elliptic Curve Diffie-Hellman Primitive.
  5. You already chose secp256r1.
  6. Hard-coded. Done.
  7. Point compression s annoying, choose not to use it.
  8. I m omitting SharedInfo. That probably makes me a bad person.
  9. Not using XOR, N/A.

Encrypt

  1. Make an ephemeral key on the right curve.

     ECDiffieHellman ephem = ECDiffieHellman.Create(curve);
    
  2. 我们决定不这样做。

     ECParameters ephemPublicParams = ephem.ExportParameters(false);
     int pointLen = ephemPublicParams.Q.X.Length;
     byte[] rBar = new byte[pointLen * 2 + 1];
     rBar[0] = 0x04;
     Buffer.BlockCopy(ephemPublicParams.Q.X, 0, rBar, 1, pointLen);
     Buffer.BlockCopy(ephemPublicParams.Q.Y, 0, rBar, 1 + pointLen, pointLen);
    
  3. 这样做是直接的。

  4. 这样做是直接的。

  5. 自从我们在此重新控制以来,我们仅举一 thing三、4、5和6。

  6. KDF时间。

     // This is why we picked AES 256, HMAC-SHA-2-256(-256) and SHA-2-256,
     // the KDF is dead simple.
     byte[] ek = ephem.DeriveKeyFromHash(
         recipientPublic,
         HashAlgorithmName.SHA256,
         null,
         new byte[] { 0, 0, 0, 1 });
    
     byte[] mk = ephem.DeriveKeyFromHash(
         recipientPublic,
         HashAlgorithmName.SHA256,
         null,
         new byte[] { 0, 0, 0, 2 });
    
  7. 加密。

     byte[] em;
    
     // ECIES uses AES with the all zero IV. Since the key is never reused,
     // there s not risk in that.
     using (Aes aes = Aes.Create())
     using (ICryptoTransform encryptor = aes.CreateEncryptor(ek, new byte[16]))
     {
         if (!encryptor.CanTransformMultipleBlocks)
         {
             throw new InvalidOperationException();
         }
    
         em = encryptor.TransformFinalBlock(message, 0, message.Length);
     }
    
  8. MAC

     byte[] d;
    
     using (HMAC hmac = new HMACSHA256(mk))
     {
         d = hmac.ComputeHash(em);
     }
    
  9. Finish

     // Either
     return Tuple.Create(rBar, em, d);
     // Or
     return rBar.Concat(em).Concat(d).ToArray();
    

Decrypt

Left as an exercise to the reader.

问题回答

取得证书的私人钥匙使用以下方法:

  • Install NuGet package Security.Cryptography (CLR Security). (The package is under MIT license.)
  • Use the following extension method to get the CngKey instance: CngKey cngKey = certificate.GetCngPrivateKey(); (Note: The extension method certificate.GetECDsaPrivateKey(), natively supported in .NET, returns an ECDsaCng instance; there is no extension method to return ECDiffieHellmanCng.)
  • The cngKey instance can be used to create either an ECDsaCng or an ECDiffieHellmanCng instance: var sa = new ECDsaCng(cngKey); var sa = new ECDiffieHellmanCng(cngKey);




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...