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.
相关文章