niting
niting

Reputation: 55

Read Secretkey from JCEKS keystore | Java

I am trying to read secret key (passphrase) from JCEKS keystore using Java code , code is working for JKS keystore but not for JCEKS keystore. i am using ibm_sdk71 as runtime (due to project requirements).

public static String getPasswordFromKeystore(String entry, String keystoreLocation, String keyStorePassword,String keyPP1) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException, InvalidKeySpecException, java.security.UnrecoverableEntryException{
    char[] password = "new".toCharArray() ;  
    FileInputStream fIn = new FileInputStream(keystoreLocation);
    KeyStore.PasswordProtection keyStorePP = new KeyStore.PasswordProtection(keyStorePassword.toCharArray());
    KeyStore.PasswordProtection keyPP = new KeyStore.PasswordProtection(keyPP1.toCharArray());
    KeyStore ks= KeyStore.getInstance("JCEKS");
    ks.load(fIn, keyStorePassword.toCharArray());
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBE");
    KeyStore.SecretKeyEntry ske;
    ske = (KeyStore.SecretKeyEntry)ks.getEntry(entry, keyPP);
    PBEKeySpec keySpec;
    keySpec = (PBEKeySpec)factory.getKeySpec(ske.getSecretKey(),PBEKeySpec.class);
    password = keySpec.getPassword();
  return new String(password);
}

Appreciate any guidance. Regards

Upvotes: 1

Views: 3900

Answers (0)

Related Questions