Update/Delete 证书链:无法将密钥分配给给定的别名

Update/Delete Certificate chain: Cannot assign the key to the given alias

我想用新证书更新我以前的自签名证书,例如:chains 从 TEST CA 返回(签名的 CSR)。我的默认密钥库是:Windows-MY

现在我想用 CA 返回的证书替换那个自签名证书。注意:我的私钥存储在我的商店中。

Key privKey = keyStore.getKey(commonName, keyPass);
System.out.println("invalid private key :" + (privKey == null));

// keyStore.deleteEntry(commonName);
// keyStore.load(null, keyPass);

if (isPrivateKeyAvailable) {
    System.out.println("name:" + commonName + " is updatded");
    keyStore.setKeyEntry(commonName, privKey, keyPass, chains);
} else {
    System.out.println("name:" + commonName + " does not exist");
}

但是我收到了这些错误:

java.lang.UnsupportedOperationException: Cannot assign the key to the given alias. at sun.security.mscapi.KeyStore.engineSetKeyEntry(KeyStore.java:415) at sun.security.mscapi.KeyStore$MY.engineSetKeyEntry(KeyStore.java:55) at java.security.KeyStore.setKeyEntry(Unknown Source) at keygenerator.KeyInstaller.installCertificateInWindowsStore(KeyInstaller.java:284) at keygenerator.KeyInstaller.doJob(KeyInstaller.java:167) at keygenerator.KeyGeneration.installCertificate(KeyGeneration.java:171) at keygenerator.KeyGeneration.main(KeyGeneration.java:68)

Windows 密钥库(从 Java 命名为 Windows-MY)不可直接写入。您需要将CA返回的私钥和证书链打包成PKCS#12文件(.p12),使用Windows导入工具导入。