سؤال

My app needs only to read private keys(with associated public cert), no writing to KeyStore, no password changing, no changes at all - just reading. Does anybody know for sure that for reading I can use this code:

///doing some actions
KeyStore store = KeyStore.getInstance("foo", "bar");
store.load(iaminputstream, iampwd); // I'M JUST LOADING, I'M NOT GONNA STORE IT!
PrivateKey pk = (PrivateKey) store.getKey(iamalias, iamkeypass);
Certificate cert = store.getCertificate(iamalias);
///contnuing some actions

instead of this:

///doing some actions
KeyStore store = KeyStore.getInstance("foo", "bar");

try{
    store.load(iaminputstream, iampwd); //I'VE LOADED

    PrivateKey pk = (PrivateKey) store.getKey(iamalias, iamkeypass);
    Certificate cert = store.getCertificate(iamalias);

} finally {
    store.store(iamoutputstream, iampassword); //AND I'VE SAVED!
}
///contnuing some actions

Prove link is highly appreciated!

هل كانت مفيدة؟

المحلول

You don't need to save it of course. Just don't forget to handle exceptions. Take a look at javadoc - there is nothing about required saving after load() call.

نصائح أخرى

If you are not storing anything, you don't have to call store.store()

But do remember to close the inputstream and handle exceptions.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top