Sun Java KeyManagerFactory 和空密码

发布于 2024-08-13 01:01:06 字数 575 浏览 6 评论 0原文

Sun JRE 1.6 中的 KeyManagerFactory 存在问题。我们使用类似于以下的代码来上传和使用 p12 格式的证书:

KeyStore keyStore = KeyStore.getInstance(PKCS12);
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(SUN_X509);

InputStream certificateFile = getSSLCertificate();
String certificatePassword = getSSLCertificatePassword();
keyStore.load(certificateFile, certificatePassword);
keyManagerFactory.init(keyStore, certificatePassword);

当证书密码存在时,此代码可以正常工作。但是,当证书密码为空(因此证书不受密码保护)时,我们会从 keyManagerFactory.init 行收到除零错误。

有谁知道为什么会发生这种情况?没有密码就不能使用证书吗? 谢谢

We are having a problem with the KeyManagerFactory in the Sun JRE 1.6. We are using code similar to the following to upload and use a certificate in p12 format:

KeyStore keyStore = KeyStore.getInstance(PKCS12);
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(SUN_X509);

InputStream certificateFile = getSSLCertificate();
String certificatePassword = getSSLCertificatePassword();
keyStore.load(certificateFile, certificatePassword);
keyManagerFactory.init(keyStore, certificatePassword);

This code works correctly when the certificate password exists. But when the certificate password is null (so the certificate is not protected by a password) we get a divide by zero error from the keyManagerFactory.init line.

Does anyone know why this is happening? Is it not possible to use a certificate without a password?
Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

蓝梦月影 2024-08-20 01:01:06

这是一个错误:

https://bugs.java.com/bugdatabase/view_bug?bug_id =6415637

解决方法是设置密码。

This is a bug:

https://bugs.java.com/bugdatabase/view_bug?bug_id=6415637

Workaround is to set a password.

巷子口的你 2024-08-20 01:01:06

由于 PKCS12 包含私钥,因此您应该始终拥有密码。我认为 Sun 无意中强制执行了这一点:)

对于所有 Keystore API,存储和私钥都需要密码。如果您真的不想处理配置或用户交互,只需在任何地方使用默认密码“changeit”即可。

Because PKCS12 contains private key, you should always have a password. I think Sun accidentally enforces this :)

For all Keystore API, password is required for the store and private keys. If you don't really want deal with the configuration or user-interaction, just use the default password "changeit" everywhere.

狼亦尘 2024-08-20 01:01:06

看来使用空字符数组将配置KeyManagerFactory以允许在没有密码的情况下访问密钥。

有各种各样的原因需要使用没有密码的KeyStore(仅内存中的KeyStore是一种可能性)。

String keystorePassword = ...;
KeyStore keys = ...;

char[] kpwd;
if(null != keystorePassword && 0 != keystorePassword.length())
    kpwd = keystorePassword.toCharArray();
else
    kpwd = new char[0];

KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());

kmf.init(keys, kpwd);

KeyManagers managers = kmf.getKeyManagers();

// Now, use "managers" for great things

It appears that using an empty character array will configure the KeyManagerFactory to allow access to the keys without a password.

There are all kinds of reasons to have a KeyStore without a password (in-memory-only KeyStores being one possibility).

String keystorePassword = ...;
KeyStore keys = ...;

char[] kpwd;
if(null != keystorePassword && 0 != keystorePassword.length())
    kpwd = keystorePassword.toCharArray();
else
    kpwd = new char[0];

KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());

kmf.init(keys, kpwd);

KeyManagers managers = kmf.getKeyManagers();

// Now, use "managers" for great things
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文