This is my program. I am getting the error "error opening the key file". Kindly help me where i am going wrong.
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.PrivateKey;
import java.util.Base64;
public class ExtractionClass {
public static void main(String args[]) {
String p12Password = "PassWord";
try{
FileInputStream file = new FileInputStream("C:/Users/Aarti_Jangid/Desktop/p12/certChain.p12");
System.out.println("FILE "+ file);
KeyStore keystore = KeyStore.getInstance("PKCS12", "SunJSSE");
keystore.load(file, p12Password.toCharArray());
String alias = keystore.aliases().nextElement();
PrivateKey key = (PrivateKey)keystore.getKey(alias, p12Password.toCharArray());
System.out.println(Base64.getEncoder().encodeToString(key.getEncoded()));
} catch(KeyStoreException kse) {
kse.printStackTrace();
System.err.println("Error getting the key");
} catch (Exception e) {
System.err.println("Error opening the key file");
e.printStackTrace();
}
}
}
StackTrace:
FILE java.io.FileInputStream@659e0bfd Error opening the key file java.util.NoSuchElementException at java.util.LinkedHashMap$LinkedHashIterator.nextNode(Unknown Source) at java.util.LinkedHashMap$LinkedKeyIterator.next(Unknown Source) at java.util.Collections$3.nextElement(Unknown Source) at ExtractionCode.ExtractionClass.main(ExtractionClass.java:17) Stack Trace is like that. I am giving the correct password and if i remove String alias = keystore.aliases().nextElement(); then it is giving keyStoreException - uninitialized keystore