PKCS#12 : DerInputStream.getLength() exception(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);
这个方法接受一个InputStream,如果这个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() 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PKCS#12:DerInputStream.getLength() 异常
基础教程推荐
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01