PKCS#12:DerInputStream.getLength() 异常
我使用 keytool 命令生成证书:
keytool -genkeypair -alias myRSAKey -keyalg RSA -keysize 1024 -keystore test.p12 -storepass test -storetype pkcs12
然后,如果我尝试使用 java security API 加载它,在将文件获取为 byte[] 后:
KeyStore ks = KeyStore.getInstance("PKCS12");
try{
ks.load(new ByteArrayInputStream(data), "test".toCharArray())
} catch (Exception e){
...
}
我得到 DerInputStream.getLength(): lengthTag=127,太大的异常。
怎么了?
I generate a certificate using the keytool command:
keytool -genkeypair -alias myRSAKey -keyalg RSA -keysize 1024 -keystore test.p12 -storepass test -storetype pkcs12
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){
...
}
I get a DerInputStream.getLength(): lengthTag=127, too big exception.
What is wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
我遇到了这个问题,我已经搜索了谷歌的深处,但仍然找不到答案。经过几天与质量糟糕的遗留代码的斗争后,我找到了导致此错误的原因。
这个方法接受一个InputStream,如果这样的InputStream有任何问题,就会抛出这个异常,我遇到过一些问题:
最后一个是造成我的问题的原因。该代码从证书创建一个 InputStream,并继续在两个 KeyStore.load() 调用中使用它,第一个成功,第二个总是给我带来这个错误。
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.
this method takes an InputStream and if there's any problem with such InputStream, this exception is thrown, some problems that I've encountered:
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.
对于其他有类似问题的人:
对我来说,解决方案是删除参数:
-storetype pkcs12
因为标准类型是 jks
For others with a similar problem:
For me solution was to remove the param:
-storetype pkcs12
since the standard type is jks
您创建的证书可能在末尾有一个额外的字符,该字符被误解为另一个证书。
在末尾使用一个或多个空行。
参考:Java证书解析
Probably the certificate you create has an extra character at the end which is misinterpreted to be another certificate.
Use one or more blank lines at the end.
Refer: Java Certificate Parsing
我的问题(
lengthTag=109,太大
)是 .p12 文件实际上是 JKS 格式,而不是 PKCS # 12 格式。有人重命名了文件扩展名。通过以正确的 PKCS 格式重新生成解决了该问题。要检查安全文件的格式,可以使用 KeyStore Explorer 打开该文件。左下栏显示实际格式。
My issue (
lengthTag=109, too big
) was the .p12 file actually is JKS format and not PKCS # 12 format. Someone renamed the file extension. By regenerating in proper PKCS format resolved the issue.To check the format of a security file, may use KeyStore Explorer to open the file. The left bottom bar shows the actual format.
在代码中指定证书类型
例如:
Specify the type of certificate in the code
for eg:
在 AndroidX 迁移并使用新的测试框架后,我在 Android Studio 中发生了这种情况。即使删除现有的
~/.android/debug.keystore
对我来说也失败了解决方案是手动重新生成它(接受所有问题为空并在最后一个问题上说“是”)
并复制它
This happened to me in Android Studio after AndroidX migration and using the new testing framework. Even deleting the existing
~/.android/debug.keystore
was failing for meThe solution was regenerate it manually (accept all questions as empty and say yes at the last one)
And copy it
这发生在我身上,因为我已在 Windows 10 计算机上本地复制并粘贴了
.p12
文件。不知道如何/为什么这是一个问题,但是当我克隆一个具有.p12
文件的项目并将我的代码指向它们时,这些文件可以工作。但是,将 Windows 文件资源管理器中的文件复制并粘贴到硬盘驱动器上的其他位置会导致此错误!!!!This happened to me because I had copy and pasted the
.p12
file locally on my windows 10 machine. No clue how/why this is a problem, but when I clone a project that has.p12
files and point my code to them, the files work. However, copy and pasting the files in windows file explorer to somewhere else on the harddrive causes this error!!!!我有同样的问题。
我的解决方案是在下面的行中将 PKCS12 替换为 jceks,因为我显然使用了错误的类型。
I had the same issue.
My solution is to replace PKCS12 with jceks in the line below because I was apparently using the wrong type.
你做错了什么。
我尝试了你的命令,然后加载了 p12 就好了。
以下代码有效:
我想知道如果您按原样输入命令,您会从 keytool 收到错误消息,密码必须至少为 6 个字符。
您没有收到该错误吗?您使用什么版本的java?
注意:如果您需要创建证书,也可以查看此工具。
http://sourceforge.net/projects/certhelper/
You are doing something wrong.
I tried your command and then loaded the p12 just fine.
The following code works:
I was wondering if you put the command as is you get an error from keytool that the password must be at least 6 characters.
You did not get that error? What version of java are you using?
Note:if you need to create certificates you can also look into this tool.
http://sourceforge.net/projects/certhelper/
确保 inputstream 变量的范围仅限于声明它的方法,而不是静态/类变量。这样可以避免此异常。
原因:当输入流被声明为类变量时,在第一次加载证书或数据后,输入流不会关闭。因此,使其仅可用于方法。
Make sure the scope of the inputstream variable is only to the method where you’re declaring it but not as static/class variable.This way this exception can be avoided.
Reason : Inputstream is not getting closed after the first time of loading certificate or data in it while it is declared as class variable.so make it available only to method.
这发生在我身上,因为以下命令:
openssl pkcs12 -export -in import.pem -inkey myhost.key.pem -name shared > server.p12 (来自 https://docs.oracle.com/en/database/other-databases/nosql-database/12.2.4.5/security/import-key-pair-java-keystore.html)
生成了错误的格式化的 pkcs12 文件。使用以下命令纠正了问题:
openssl pkcs12 -export -in import.pem -inkey myhost.key.pem -name shared -out server.p12
This happened to me because the following command:
openssl pkcs12 -export -in import.pem -inkey myhost.key.pem -name shared > server.p12
(from https://docs.oracle.com/en/database/other-databases/nosql-database/12.2.4.5/security/import-key-pair-java-keystore.html)generated a wrongly formatted pkcs12 file. Using the following corrected the problem:
openssl pkcs12 -export -in import.pem -inkey myhost.key.pem -name shared -out server.p12
这个错误有多种原因......日志可能真的很混乱。
主要原因之一可能是 Maven 过滤。
根据 maven 官方文档
警告:不要过滤包含图像等二进制内容的文件!这很可能会导致输出损坏。
我们的 .jks 在打包阶段被 Maven 损坏了。
这个帖子帮助我弄清楚了。
我们可以直接在相关的 pom.xml 中从过滤中排除一些目录或文件扩展名:
This error has multpile causes... The log can be realy confusing.
One main cause can be maven filtering.
According to maven official documentation
Warning: Do not filter files with binary content like images! This will most likely result in corrupt output.
Our .jks was corrupted by maven during packaging stage.
This thread helped me to figure it out.
We can exclude some directories or file extensions from filtering directly in concerned pom.xml :