PKCS#12:DerInputStream.getLength() 异常 [英] PKCS#12 : DerInputStream.getLength() exception

查看:39
本文介绍了PKCS#12:DerInputStream.getLength() 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 keytool 命令生成证书:

I generate a certificate using the keytool command:

keytool -genkeypair -alias myRSAKey -keyalg RSA -keysize 1024 -keystore test.p12 -storepass test -storetype pkcs12

然后,如果我尝试使用 java 安全 API 加载它,在将文件作为 byte[] 获取后:

Then if I try to load it using java security API, after getting the file as a byte[] :

KeyStore ks = KeyStore.getInstance("PKCS12");
try{
   ks.load(new ByteArrayInputStream(data), "test".toCharArray())
} catch (Exception e){
   ...
}

我得到一个 DerInputStream.getLength(): lengthTag=127, too big exception.

I get a DerInputStream.getLength(): lengthTag=127, too big exception.

怎么了?

推荐答案

我遇到了这个问题,我搜索了谷歌的深处,仍然找不到答案.经过几天与质量糟糕的遗留代码作斗争后,我发现了导致此错误的原因.

I had this problem and I've searched the depths of google and still couldn't find the answer. After some days battling with a terrible quality legacy code, I found what was causing this error.

KeyStore.load(InputStream is, String pass);

这个方法接受一个I​​nputStream,如果这个InputStream有任何问题,就会抛出这个异常,我遇到的一些问题:

this method takes an InputStream and if there's any problem with such InputStream, this exception is thrown, some problems that I've encountered:

  • InputStream 指向错误/空白/刚刚创建的文件
  • InputStream 已经打开或其他东西正在持有资源
  • InputStream已经被使用并读取过,所以InputStream的下一个字节的位置就是结束了
  • The InputStream points to the wrong / blank / just created file
  • The InputStream is already open or something else is holding the resource
  • The InputStream was already used and read, thus the position of the next byte of InputStream is it's end

最后一个是我的问题的原因.该代码从证书创建一个 InputStream,并在两个 KeyStore.load() 调用中继续使用它,第一个成功,第二个总是给我这个错误.

The last one was the responsible for my problem. The code was creating an InputStream from a certificate, and proceeding to use it in two KeyStore.load() calls, the first one was successful, the second one always got me this error.

这篇关于PKCS#12:DerInputStream.getLength() 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆