gpt4 book ai didi

java - 得到 java.security.InvalidKeyException : IOException: ObjectIdentifier() -- data isn't an object ID (tag = -96)

转载 作者:行者123 更新时间:2023-12-02 00:33:36 24 4
gpt4 key购买 nike

注意: String cert 通过 REST API 作为 HashMap 发送,不确定这里出了什么问题。

HashMap<String, Object> extraParams = //API brings this HashMap here.
String cert = (String) extraParams.get("certificate");
cert = cert.replaceAll("-----BEGIN CERTIFICATE-----", "").
replaceAll("-----END CERTIFICATE-----", "").replaceAll("\r", "").replaceAll("\n", "");
byte[] decodedBytes = Base64.decodeBase64(cert.getBytes("UTF-8"));
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(decodedBytes);
KeyFactory kf = KeyFactory.getInstance("RSA");
PublicKey pk = kf.generatePublic(publicKeySpec);

来自原始服务器的我的证书字符串和我通过 API 收到的证书字符串相同,但仍然收到此错误,不确定为什么?

java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: IOException: ObjectIdentifier() -- data isn't an object ID (tag = -96)

最佳答案

正如我在问题本身中提到的,我的 String Cert 已通过 REST HTTP 传输,我怀疑 UTF-8 编码可能成为一个问题。这就是我所缺少的。下面的代码对我来说很有魅力。部分复制自 https://stackoverflow.com/a/34549537/1665592

String cert = "...";
byte[] encodedCert = cert.getBytes("UTF-8");
byte[] decodedCert = Base64.decodeBase64(encodedCert);
CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
InputStream in = new ByteArrayInputStream(decodedCert);
X509Certificate certificate = (X509Certificate)certFactory.generateCertificate(in);
PublicKey publicKey = ((RSAPublicKey)certificate.getPublicKey());

关于java - 得到 java.security.InvalidKeyException : IOException: ObjectIdentifier() -- data isn't an object ID (tag = -96),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57993807/

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