Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why keyStore.aliases() is empty for pkcs12

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);  

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?

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

like image 996
user2662294 Avatar asked Sep 03 '13 19:09

user2662294


1 Answers

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]:
...
...

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.

like image 170
gtrig Avatar answered Oct 15 '22 13:10

gtrig