gpt4 book ai didi

java.security.UnrecoverableKeyException : Cannot recover key when executing KeyManagerFactory. init()

转载 作者:太空宇宙 更新时间:2023-11-04 07:10:04 34 4
gpt4 key购买 nike

我有以下代码尝试使用 SHA-256 哈希作为密码来初始化 KeyManagerFactory。

public static KeyManager[] getKeystoreManagers()
throws UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException, NoSuchProviderException {
KeyStore keystore = getKeyStore();

if (keystore == null) {
return null;
}

KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(keystore, getMachinePassword(SHA_256).toCharArray());

return kmf.getKeyManagers();
}

getKeyStore() 返回我的应用程序 keystore 。 getMachinePassword() 使用 SHA-256 返回密码,密码长度为 64 位。
问题是调用 init() 时出现异常:

java.security.UnrecoverableKeyException: Cannot recover key

如果我传递的密码长度较小,例如 50 位数字,则初始化成功。
这里似乎有什么问题?

最佳答案

我已经解决了我的问题。 keystore 是使用带有特定别名的 setEntry 创建的。
因此,在我的转换函数中,我必须使用旧密码获取条目,并使用新密码再次设置具有相同别名的相同条目。现在,使用此更新的 keystore ,KeyManagerFactory.init() 可以成功运行。请参阅下面的代码:

static void convertPasswordAlgorithm(String keystorePath, String fromAlgorithm, String toAlgorithm) throws Exceptionc {
FileInputStream fileInStream = null;
String keystoreFullPath = keystorePath + ISiteConstants.c_FILE_SEPERATOR + KEYSTORE_FILE_NAME;
KeyStore keyStore;

try {
String alias = getCertificateAlias();
keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
fileInStream = new FileInputStream(keystoreFullPath);

// Try to load the keystore with fromAlgorithm password hash.
char[] machineOldAlgPassword = getMachinePassword(fromAlgorithm).toCharArray();
keyStore.load(fileInStream, machineOldAlgPassword);

// Save the entry to update
KeyStore.Entry entry = keyStore.getEntry(alias, new KeyStore.PasswordProtection(machineOldAlgPassword));
HandleFiles.close(fileInStream);

// If succeeded, recalculate password using toAlgorithm hash and save.
String machineNewAlgPassword = getMachinePassword(toAlgorithm);
keyStore.setEntry(alias, entry, new KeyStore.PasswordProtection(machineNewAlgPassword.toCharArray()));

FileOutputStream fileOutputStream = new FileOutputStream(keystoreFullPath);
keyStore.store(fileOutputStream, machineNewAlgPassword.toCharArray());
HandleFiles.close(fileOutputStream);
} finally {
HandleFiles.close(fileInStream);
}
}

关于java.security.UnrecoverableKeyException : Cannot recover key when executing KeyManagerFactory. init(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20787657/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com