gpt4 book ai didi

elliptic-curve - 通过具有 ec 公钥坐标构建 PEM 文件

转载 作者:行者123 更新时间:2023-12-02 03:13:50 24 4
gpt4 key购买 nike

我尝试通过给定数字(我的私钥)计算曲线上的点来创建椭圆公钥,因此我得到了椭圆曲线点的坐标(x,y)

我得到坐标

myPublicKeyCoordinates = myPrivateKeyValue * GPointOnCurve

如何为我的公钥构建 PEM(或 DER)文件?

我不关心语言(java、python、javascript...)
因为我想知道如何构建文件(即使我写了每个字节......)

最佳答案

假设您已经了解 ITU-T X.680-201508 (ASN.1 语言)和 ITU-T X.690-201508 (ASN.1 数据的 BER(和 CER)和 DER 编码),椭圆曲线 key 的主要定义文档及其表示形式是 https://www.secg.org/sec1-v2.pdf来自高效密码学标准组织(而非美国证券交易委员会)。

C.3 节(椭圆曲线公钥语法)指出 EC 公钥的通用传输容器是 X.509 SubjectPublicKeyInfo 结构:

SubjectPublicKeyInfo ::= SEQUENCE {
algorithm AlgorithmIdentifier {{ECPKAlgorithms}} (WITH COMPONENTS
{algorithm, parameters}) ,
subjectPublicKey BIT STRING
}

可能的“算法”(实际上意味着关键编码类型)是开放式集合

ECPKAlgorithms ALGORITHM ::= {
ecPublicKeyType |
ecPublicKeyTypeRestricted |
ecPublicKeyTypeSupplemented |
{OID ecdh PARMS ECDomainParameters {{SECGCurveNames}}} |
{OID ecmqv PARMS ECDomainParameters {{SECGCurveNames}}},
...
}

ecPublicKeyType ALGORITHM ::= {
OID id-ecPublicKey PARMS ECDomainParameters {{SECGCurveNames}}
}

...

ECDomainParameters 来自 C.2:

ECDomainParameters{ECDOMAIN:IOSet} ::= CHOICE {
specified SpecifiedECDomain,
named ECDOMAIN.&id({IOSet}),
implicitCA NULL
}

C.3 提到了大约一半

The elliptic curve public key (a value of type ECPoint that is an OCTET STRING) is mapped to a subjectPublicKey (a value encoded as type BIT STRING) as follows: The most significant bit of the value of the OCTET STRING becomes the most significant bit of the value of the BIT STRING and so on with consecutive bits until the least significant bit of the OCTET STRING becomes the least significant bit of the BIT STRING.

所以我们向后寻找并找到

An elliptic curve point itself is represented by the following type

ECPoint ::= OCTET STRING

whose value is the octet string obtained from the conversion routines given in Section 2.3.3.

2.3.3(椭圆曲线点到八位字节字符串转换)有很多单词,但最好支持的格式不是使用点压缩(并且 P != 无穷大点)

  1. If P = (xP , yP ) != O and point compression is not being used, proceed as follows:

3.1. Convert the field element xP to an octet string X of length (log2 q)/8 octets using the conversion routine specified in Section 2.3.5.

3.2. Convert the field element yP to an octet string Y of length (log2 q)/8 octets using the conversion routine specified in Section 2.3.5.

3.3. Output M = 0416 || X || Y .

2.3.5 是一大堆单词,表示“长度足以容纳字段中所有值的大端字节顺序”(又名“保留前导零”)。

现在我们聚会吧。

鉴于 secp256r1 上的 FIPS 186-3 引用 key (d=70A12C2DB16845ED56FF68CFC21A472B3F04D7D6851BF6349F2D7D5B3452B38A),

Q 是(8101ECE47464A6EAD70CF69A6E2BD3D88691A3262D22CBA4F7635EAFF26680A8D8A12BA61D599235F67D9CB4D58F1783D3CA43E78F0A5ABAA624079936C 0C3A9)

公钥 DER 看起来像

// SubjectPublicKeyInfo
30 XA
// AlgorithmIdentifier
30 XB
// AlgorithmIdentifier.id (id-ecPublicKey (1.2.840.10045.2.1))
06 07 2A 86 48 CE 3D 02 01
// AlgorithmIdentifier.parameters, using the named curve id (1.2.840.10045.3.1.7)
06 08 2A 86 48 CE 3D 03 01 07
// SubjectPublicKeyInfo.subjectPublicKey
03 XC 00
// Uncompressed public key
04
// Q.X
81 01 EC E4 74 64 A6 EA D7 0C F6 9A 6E 2B D3 D8
86 91 A3 26 2D 22 CB A4 F7 63 5E AF F2 66 80 A8
// Q.Y
D8 A1 2B A6 1D 59 92 35 F6 7D 9C B4 D5 8F 17 83
D3 CA 43 E7 8F 0A 5A BA A6 24 07 99 36 C0 C3 A9

计算 XA、XB 和 XC 的所有字节:

XC = 32 (Q.X) + 32 (Q.Y) + 1 (0x04) + 1(0x00 表示未使用的位)= 66 = 0x42

XB = 19 = 0x13

XA 则为 66 + 19 + 2(标记字节)+ 2(长度字节)= 89 = 0x59

(当然,如果我们的任何长度值超过 0x7F,我们就必须正确编码它们)

现在我们只剩下

30 59 30 13 06 07 2A 86 48 CE 3D 02 01 06 08 2A
86 48 CE 3D 03 01 07 03 42 00 04 81 01 EC E4 74
64 A6 EA D7 0C F6 9A 6E 2B D3 D8 86 91 A3 26 2D
22 CB A4 F7 63 5E AF F2 66 80 A8 D8 A1 2B A6 1D
59 92 35 F6 7D 9C B4 D5 8F 17 83 D3 CA 43 E7 8F
0A 5A BA A6 24 07 99 36 C0 C3 A9

并且,我们验证:

$ xxd -r -p | openssl ec -text -noout -inform der -pubin
read EC key
<paste, then hit CTRL+D>
30 59 30 13 06 07 2A 86 48 CE 3D 02 01 06 08 2A
86 48 CE 3D 03 01 07 03 42 00 04 81 01 EC E4 74
64 A6 EA D7 0C F6 9A 6E 2B D3 D8 86 91 A3 26 2D
22 CB A4 F7 63 5E AF F2 66 80 A8 D8 A1 2B A6 1D
59 92 35 F6 7D 9C B4 D5 8F 17 83 D3 CA 43 E7 8F
0A 5A BA A6 24 07 99 36 C0 C3 A9
Private-Key: (256 bit)
pub:
04:81:01:ec:e4:74:64:a6:ea:d7:0c:f6:9a:6e:2b:
d3:d8:86:91:a3:26:2d:22:cb:a4:f7:63:5e:af:f2:
66:80:a8:d8:a1:2b:a6:1d:59:92:35:f6:7d:9c:b4:
d5:8f:17:83:d3:ca:43:e7:8f:0a:5a:ba:a6:24:07:
99:36:c0:c3:a9
ASN1 OID: prime256v1
NIST CURVE: P-256

将其打印为“私钥:(256 位)”只是该工具的一个错误/怪癖,那里没有私钥。

对于指定的参数曲线来说事情更困难,但它们不能很好地互操作(https://www.rfc-editor.org/rfc/rfc5480#section-2.1.1表示符合要求的CA不得使用指定的参数形式或隐式形式,但必须使用命名形式)。

关于elliptic-curve - 通过具有 ec 公钥坐标构建 PEM 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56772982/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com