gpt4 book ai didi

java - 解码来自 X500Name 的 DER 编码值

转载 作者:行者123 更新时间:2023-12-01 22:06:05 24 4
gpt4 key购买 nike

我有一个从 X500Name 检索的 java.security.util.DerValue

x500Name.findMostSpecificAttribute(SOME_OID)

一些值存储为 DER 编码。 .toString 返回给我一些lile:

[DerValue, tag = 18, length = 12]

如何解码?

此外,无法从中获取 bytes[]。

derValue.getOctetString

返回我

java.io.IOException: DerValue.getOctetString, not an Octet String: 18

编辑:

我的解决方案是:

def decodeDERValue(derValue: DerValue): String = {
val asnInput = new ASN1InputStream(new ByteArrayInputStream(derValue.toByteArray))

val obj = asnInput.readObject()
val derValueSting = obj match {
case p: DERPrintableString => p.getString
case p: DERUTF8String => p.getString
case p: DERIA5String => p.getString
case _ => ASN1Dump.dumpAsString(obj, true)
}
asnInput.close()

derValueSting.trim
}

最佳答案

这是一个 Sun 内部类,名为 sun.security.util.DerValue,而不是 java.security.util.DerValue。 ASN.1 类型由标签中的信息标识。因此,您可以从 DerValue 检索哪种数据取决于标签。更多信息可查询in this book或标准8824-1 and 8825-1 .

但是您不应该依赖 Sun 内部类。如果您可以在 byte[] 中获取底层 DER 编码,则可以使用例如Bouncy CaSTLe 解析 BER/DER。

关于java - 解码来自 X500Name 的 DER 编码值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32757302/

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