0

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

Aarti Jangid
  • 79
  • 10
  • Please copy paste the stack trace here as well – Alexander Farber Oct 28 '16 at 07:19
  • I think your .p12 file has something wrong, if you use a valid certificate your code works, [here](https://itv.mit-xperts.com/clientssl/issue/dload/index.php?id=1411134668) you can find a valid certificate. – user6904265 Oct 28 '16 at 09:11
  • If you create the .p12 file programmatically, take a look [here](http://stackoverflow.com/questions/5312559/how-do-i-programmatically-create-a-new-keystore) – user6904265 Oct 28 '16 at 09:13

1 Answers1

0

I assume your stack trace is :

Error opening registry key 'C:/Users/Aarti_Jangid/Desktop/p12/certChain.p12'
Error: could not find Java.dll
Error: could not find Java 2 Runtime Environment

If so try this : Java Error opening registry key

Community
  • 1
  • 1
Shree Naath
  • 477
  • 2
  • 5
  • 18
  • 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 – Aarti Jangid Oct 28 '16 at 08:47
  • I am getting null in the private key. – Aarti Jangid Oct 28 '16 at 09:25