PKCS#12:DerInputStream.getLength()异常 [英] PKCS#12 : DerInputStream.getLength() exception
问题描述
我使用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,太大的异常。
I get a DerInputStream.getLength(): lengthTag=127, too big exception.
有什么问题?
推荐答案
google的深度仍然找不到答案。经过几天与一个可怕的质量遗留代码,我发现是什么导致这个错误。
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的下一个字节的位置是end
- 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屋!