为什么pkcs12的keyStore.aliases()为空 [英] Why keyStore.aliases() is empty for pkcs12

查看:1273
本文介绍了为什么pkcs12的keyStore.aliases()为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从.p12文件加载PrivateKey,方法是使用以下代码:

I'm trying to load PrivateKey from .p12 file by using this code:

    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    java.security.KeyStore keyStore = KeyStore.getInstance("PKCS12", "BC");
    keyStore.load(new FileInputStream(new File("my_domain_com.p12")), password);
    keyStore.aliases().hasMoreElements(); //this is false
    java.security.PrivateKey privateKey = (PrivateKey) keyStore.getKey("SomeAlias", password);  

我试图找到没有别名的原因。但我找不到。
空的别名可以是什么原因?
我想要获取私钥,并使用此密钥生成一些文本。还有其他apporach?

I'm trying to find the reason why there is no aliases. But I'm not able to find. What can be reason for the empty alias? I want to get private key and ecrypt some text using this key. Is there other apporach?

我也有.cer文件,但我不知道我应该一起使用。

I also have .cer file but I'm not sure I should use together.

推荐答案

密钥库是否可能没有什么?使用Java keytool 命令进行验证。

Is it possible the keystore has nothing in it at all? Use the Java keytool command to verify.

>keytool -list -v -keystore test.p12 -storetype PKCS12
Enter keystore password:

Keystore type: PKCS12
Keystore provider: SunJSSE

Your keystore contains 1 entry

Alias name: test_alias
Creation date: Sep 3, 2013
Entry type: PrivateKeyEntry
Certificate chain length: 1
Certificate[1]:
...
...

如果密钥库中有条目,你应该看到一个别名。如果密钥库中没有条目,您将看到您的密钥库包含0条目,您将需要将其导入密钥库。

If there are entries in the keystore, you should see an "Alias name" for each one. If there are no entries in the keystore, you will see "Your keystore contains 0 entries", and you will need to import them into the keystore.

另外,加密时你应该加密别人的公共密钥,他们将用他们的私钥进行解密。他们使用您的公钥加密,您的私钥解密。

Also, when encrypting, you should encrypt with someone else's public key, and they will decrypt with their private key. And they encrypt with your public key, which you decrypt with your private key.

这篇关于为什么pkcs12的keyStore.aliases()为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆