本文整理匯總了Java中javax.crypto.SecretKeyFactory.getKeySpec方法的典型用法代碼示例。如果您正苦於以下問題:Java SecretKeyFactory.getKeySpec方法的具體用法?Java SecretKeyFactory.getKeySpec怎麽用?Java SecretKeyFactory.getKeySpec使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.crypto.SecretKeyFactory
的用法示例。
在下文中一共展示了SecretKeyFactory.getKeySpec方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getPassword
import javax.crypto.SecretKeyFactory; //導入方法依賴的package包/類
/**
* Gets the secret password stored in keystore under given alias.
* @param alias
* @param entryPassword entry password to access the secret password stored in keystore
* @return the secret password or null if secret password does not exists in keystore
* @throws KeyStoreProviderException
*/
public String getPassword(String alias, String entryPassword) throws KeyStoreProviderException {
try {
LOG.info(String.format("Getting password with alias %s from keystore ...", alias));
SecretKeyFactory factory = SecretKeyFactory.getInstance(SECRET_KEY_PASSWORD_ALGORITHM);
Optional<KeyStore.SecretKeyEntry> ske = Optional.fromNullable((KeyStore.SecretKeyEntry) this.keystore.getEntry(alias, new KeyStore.PasswordProtection(entryPassword.toCharArray())));
if(!ske.isPresent()) {
return null;
}
PBEKeySpec keySpec = (PBEKeySpec)factory.getKeySpec(ske.get().getSecretKey(),PBEKeySpec.class);
char[] password = keySpec.getPassword();
if(ArrayUtils.isEmpty(password)) {
throw new KeyStoreProviderException("Recovered password is blank.");
}
return new String(password);
} catch (NoSuchAlgorithmException nsae) {
throw new KeyStoreProviderException("Algorithm used to create PBE secret cannot be found.", nsae);
} catch (UnrecoverableEntryException uee) {
throw new KeyStoreProviderException("Invalid entry password to recover secret.", uee);
} catch (KeyStoreException kse) {
throw new KeyStoreProviderException("Failed to get PBE secret to keystore.", kse);
} catch (InvalidKeySpecException ikse) {
throw new KeyStoreProviderException("Failed to get key spec from PBE secret.", ikse);
} catch (Exception e) {
throw new KeyStoreProviderException("Failed to get PBE secret.", e);
}
}