首页 > 解决方案 > CngAlgorithm.ECDiffieHellmanP521/P256/P384生成的EccPrivateBlob的CngKeyBlobFormat是什么?

问题描述

CngKey key = CngKey.Create(CngAlgorithm.ECDiffieHellmanP521, null,
   new CngKeyCreationParameters { ExportPolicy = CngExportPolicies.AllowPlaintextExport });    
byte[] keyBlob= key.Export(CngKeyBlobFormat.EccPrivateBlob);

keyBlob 的长度是 206。它的格式是什么?哪些字节是 32 字节私钥和 64 字节公钥?

将公钥从其他地方导入到 CngKey?,长度为4+4+64+32=104。

标签: c#cngecdh

解决方案


对于所有三个曲线,blob 的二进制结构都是相同的:

<magic number, 4 bytes><modulus length in bytes, 4 bytes><x-value of public key><y-value of public key><private key>

详细适用:

  • secp256r1 / NIST P-256

    Private: 45434B32 20000000 <x-value of public key, 32 bytes><y-value of public key, 32 bytes><private key, 32 bytes>   total length: 104 bytes
    Public:  45434B31 20000000 <x-value of public key, 32 bytes><y-value of public key, 32 bytes>                          total length:  72 bytes
    
  • secp384r1 / NIST P-384

    Private: 45434B34 30000000 <x-value of public key, 48 bytes><y-value of public key, 48 bytes><private key, 48 bytes>   total length: 152 bytes
    Public:  45434B33 30000000 <x-value of public key, 48 bytes><y-value of public key, 48 bytes>                          total length: 104 bytes
    
  • secp521r1 / NIST P-521

    Private: 45434B36 42000000 <x-value of public key, 66 bytes><y-value of public key, 66 bytes><private key, 66 bytes>   total length: 206 bytes
    Public:  45434B35 42000000 <x-value of public key, 66 bytes><y-value of public key, 66 bytes>                          total length: 140 bytes
    

私钥和公钥的x- 和 -y组件以大端格式存储。所有三个分量都具有模数的长度。因此,斑点的不同长度是由曲线的不同模量引起的。

另请参阅:SECG、SEC2密钥 blob 格式幻数ECCPublicBlob 和 ECCPrivateBlob 的格式


推荐阅读