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() 异常
基础教程推荐
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- 如何对 Java Hashmap 中的值求和 2022-01-01
