Search code examples
javakeystore

Validate KeyStore password in Java


I'm allowing the user to specify its own KeyStore in my application. I'd like to validate that KeyStore password, given by the user, is valid.
Is there a way doing that?


Solution

  • try loading it

    FileInputStream is = new FileInputStream(file);
    KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
    /* getting the key*/
    keystore.load(is, password.toCharArray());
    

    Throws: IOException - if there is an I/O or format problem with the keystore data, if a password is required but not given, or if the given password was incorrect. If the error is due to a wrong password, the cause of the IOException should be an UnrecoverableKeyException