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.
Is it possible the keystore has nothing in it at all? Use the Java
keytool
command to verify.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.