从算法 oid 和密钥的 byte[] 值构造 x509 编码的 public 密钥
Construct x509 encoded public key from algorithm oid and byte[] value of key
我有算法 oid 和带 public 键值的字节数组。我需要将其编码为 X.509 标准中定义的 asn1:
SubjectPublicKeyInfo ::= SEQUENCE {
algorithm AlgorithmIdentifier,
subjectPublicKey BIT STRING }
除了手动构建 asn1 结构外,我该怎么做?
比如我有oid 1.2.643.7.1.1.1.1
,
public 点值 7800A33627EF627D19C0A8E1C284067031851A9860A92E0B405B0561643FF1B6056A31FD01AD1D5E74213175D2F3808509C759418FD08554C20C88B109072207
和
我想将其表示为
SEQUENCE {
OBJECT IDENTIFIER '1 2 643 7 1 1 1 1'
BIT STRING, encapsulates {
OCTET STRING
78 00 A3 36 27 EF 62 7D 19 C0 A8 E1 C2 84 06 70
31 85 1A 98 60 A9 2E 0B 40 5B 05 61 64 3F F1 B6
05 6A 31 FD 01 AD 1D 5E 74 21 31 75 D2 F3 80 85
09 C7 59 41 8F D0 85 54 C2 0C 88 B1 09 07 22 07
}
}
更新:感谢威尔克斯的回答!带有 256 位密钥的 GOST 34.10-2012 的最终代码
byte[] publicPoint = Hex.decode("7800A33627EF627D19C0A8E1C284067031851A9860A92E0B405B0561643FF1B6056A31FD01AD1D5E74213175D2F3808509C759418FD08554C20C88B109072207");
GOST3410PublicKeyAlgParameters parameters = new GOST3410PublicKeyAlgParameters(
RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256_paramSetA,
RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256);
SubjectPublicKeyInfo spki = new SubjectPublicKeyInfo(
new AlgorithmIdentifier(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256, parameters),
new DEROctetString(publicPoint));
byte[] encoded = spki.getEncoded();
恕我直言,这完全取决于 OID 标识的内容。我不完全确定以下内容。我不得不手动查找 OID。
byte[] data = Hex.decode("7800A33627EF627D19C0A8E1C284067031851A9860A92E0B405B0561643FF1B6056A31FD01AD1D5E74213175D2F3808509C759418FD08554C20C88B109072207");
SubjectPublicKeyInfo spki = new SubjectPublicKeyInfo(
new AlgorithmIdentifier(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256),
new DEROctetString(data));
ASN1Primitive asn1 = spki.toASN1Primitive();
我尝试使用 org.bouncycastle.jcajce.provider.asymmetric.ecgost12.BCECGOST3410_2012PublicKey#BCECGOST3410_2012PublicKey(org.bouncycastle.asn1.x509.SubjectPublicKeyInfo)
但由于某些我不知道的原因,它不是 public 而只是包私有构造函数。
我有算法 oid 和带 public 键值的字节数组。我需要将其编码为 X.509 标准中定义的 asn1:
SubjectPublicKeyInfo ::= SEQUENCE {
algorithm AlgorithmIdentifier,
subjectPublicKey BIT STRING }
除了手动构建 asn1 结构外,我该怎么做?
比如我有oid 1.2.643.7.1.1.1.1
,
public 点值 7800A33627EF627D19C0A8E1C284067031851A9860A92E0B405B0561643FF1B6056A31FD01AD1D5E74213175D2F3808509C759418FD08554C20C88B109072207
和
我想将其表示为
SEQUENCE {
OBJECT IDENTIFIER '1 2 643 7 1 1 1 1'
BIT STRING, encapsulates {
OCTET STRING
78 00 A3 36 27 EF 62 7D 19 C0 A8 E1 C2 84 06 70
31 85 1A 98 60 A9 2E 0B 40 5B 05 61 64 3F F1 B6
05 6A 31 FD 01 AD 1D 5E 74 21 31 75 D2 F3 80 85
09 C7 59 41 8F D0 85 54 C2 0C 88 B1 09 07 22 07
}
}
更新:感谢威尔克斯的回答!带有 256 位密钥的 GOST 34.10-2012 的最终代码
byte[] publicPoint = Hex.decode("7800A33627EF627D19C0A8E1C284067031851A9860A92E0B405B0561643FF1B6056A31FD01AD1D5E74213175D2F3808509C759418FD08554C20C88B109072207");
GOST3410PublicKeyAlgParameters parameters = new GOST3410PublicKeyAlgParameters(
RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256_paramSetA,
RosstandartObjectIdentifiers.id_tc26_gost_3411_12_256);
SubjectPublicKeyInfo spki = new SubjectPublicKeyInfo(
new AlgorithmIdentifier(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256, parameters),
new DEROctetString(publicPoint));
byte[] encoded = spki.getEncoded();
恕我直言,这完全取决于 OID 标识的内容。我不完全确定以下内容。我不得不手动查找 OID。
byte[] data = Hex.decode("7800A33627EF627D19C0A8E1C284067031851A9860A92E0B405B0561643FF1B6056A31FD01AD1D5E74213175D2F3808509C759418FD08554C20C88B109072207");
SubjectPublicKeyInfo spki = new SubjectPublicKeyInfo(
new AlgorithmIdentifier(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256),
new DEROctetString(data));
ASN1Primitive asn1 = spki.toASN1Primitive();
我尝试使用 org.bouncycastle.jcajce.provider.asymmetric.ecgost12.BCECGOST3410_2012PublicKey#BCECGOST3410_2012PublicKey(org.bouncycastle.asn1.x509.SubjectPublicKeyInfo)
但由于某些我不知道的原因,它不是 public 而只是包私有构造函数。