如何用java读取密码加密密钥?
我的私钥以 PKCS8 DER 格式存储在文件中,并受密码保护。最简单的阅读方法是什么?
这是我用来加载未加密密钥的代码:
InputStream in = new FileInputStream(privateKeyFilename);
byte[] privateKeydata = new byte[in.available()];
in.read(privateKeydata);
in.close();
KeyFactory privateKeyFactory = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(privateKeydata);
PrivateKey privateKey = privateKeyFactory.generatePrivate(encodedKeySpec);
对于具有相同规格的未加密密钥,它工作得很好。顺便说一句,我正在使用 BouncyCastle。
我可以使用以下 openssl 命令查看此私钥
openssl pkcs8 -in ./privatekey.key -inform DER -passin pass:thisismypass
请帮忙!!!
我在自己对此主题的回答中发布了一些解决方案。但我没有回答问题,以防有人可以帮助使其在没有额外库(只需 BouncyCastle)的情况下工作。
I have private key stored in file in PKCS8 DER format and protected by password. What is the easiest way to read it?
Here is the code I use to load unencrypted one:
InputStream in = new FileInputStream(privateKeyFilename);
byte[] privateKeydata = new byte[in.available()];
in.read(privateKeydata);
in.close();
KeyFactory privateKeyFactory = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(privateKeydata);
PrivateKey privateKey = privateKeyFactory.generatePrivate(encodedKeySpec);
It works fine for unencrypted keys with the same specification. By the way, I am using BouncyCastle.
I can view this private key using following openssl command
openssl pkcs8 -in ./privatekey.key -inform DER -passin pass:thisismypass
Please, Help!!!
I,ve posted some solutions in my own answer to this topic. But I keep question unanswered in case anybody can help with making it work without extra library, just BouncyCastle.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了解决方案!也许它不是那么优雅,但是......
在这里我将发布两个解决方案:
第一个:
我找到了一种解决方案 这里,但它抛出异常。解决方案:
我的例外:
第二:
按照此http://juliusdavies.ca/commons-ssl/pkcs8.html 您可以阅读第二个可行的解决方案
I found the solution! Maybe its not so elegant, but...
Here I will post two solutions:
First:
I found a kind of solution here, but it throws exception. Solution:
And my exception:
Second:
And following this http://juliusdavies.ca/commons-ssl/pkcs8.html you can read about the second, working solution
这是我的代码,它可以工作:)
This is my code and it work's :)