本文整理汇总了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);
}
}