java.io.IOException: toDerInputStream 拒绝标签类型 77

问题描述

我正在尝试使用 Java 从 weblogic 服务器读取 pfx 证书,但出现 java.io.IOException:toDerInputStream 拒绝标签类型 77

解决方法

看起来您可能正在尝试使用 .cer 证书,而它应该是 .pfx 或其他不同于 .cer 的格式。 检查您使用的密钥库或您的证书格式。

String strFile = "certificate.cer"; // << ((( should be .pfx )))
File file = new File(strFile);

byte[] certificateBytes = FileUtils.readFileToByteArray(file);
String password = "MyPassword123";


KeyStore keyStore = KeyStore.getInstance("PKCS12"); // << KeyStore used for .pfx

try (ByteArrayInputStream bs = new ByteArrayInputStream(certificateBytes)) {
    keyStore.load(bs,password.toCharArray());
} catch (NoSuchAlgorithmException | CertificateException e) {
  e.printStackTrace();
} 
,

这是给我的。
当我以字符串形式下载为 base64 时。
我的代码需要它在 byte[],
我的代码告诉我“java.io.IOException:toDerInputStream 拒绝标签类型 77”。
然后我在 powershell 中执行此操作,它有效。

$fileContentBytes = get-content "the file you read from web"
$a= [System.Convert]::FromBase64String($fileContentBytes)
[IO.File]::WriteAllBytes(‘xxx.pfx’,$a)