如何更改 Java Keystore(JKS) 密钥库和别名密码以便它们工作

How to change Java Keystore(JKS) keystore and alias password so that they work

我创建了一个全局 JKS,其中 "changeme" 作为密钥库密码。我使用 Keystore Explorer 创建了 JKS。

使用全局 JKS 背后的想法是,应用程序可以从 S3 中拉下 JKS,然后使用自己的字符串密码重置 JKS。我们做了很多 SpringBoot API,我们使用 JKS 来保护容器中的 Tomcat,这样我们就可以连接 HTTPS。

但这就是我 运行 遇到的问题,当我更改 JKS 密钥库密码时,我开始收到 java.security.UnrecoverableKeyException: Cannot recover key 错误。

在 Keystore Explorer 中,我没有为别名指定密码。当我进入 Keystore Explorer 更改别名密码时,它接受 "changeme" 作为密码。因此,我假设 Keystore Explorer 自动使用 changeme 作为密码,因为我为 JKS 密钥库密码提供了它。

诚然,我不是使用 JKS 和理解安全的复杂性的专家,但这让我感到难过。

我还尝试使用以下命令使用 Keytool 更改密钥库密码:

keytool -storepasswd -keystore myJKS.jks

keytool -keypasswd -alias myalias -keystore myJKS.jks

但是当我尝试更改别名时,我得到:

keytool error: java.io.IOException: Keystore was tampered with, or password was incorrect

我做错了什么?

谢谢

您看到的错误是因为您可能在命令中提供了错误的 keystore-password

基本了解 JKS 的含义。 JKS (Java KeyStore) 基本上是一个保护密钥(对称密钥)、密钥对(非对称密钥)和证书的文件。它保护它们的方式是通过密码,这个密码叫做keystore-password。并且 JKS 文件中的密钥也可以单独保护,这意味着它们可以有自己的密码,称为 key-password.

改变方式keystore-password:

keytool -storepasswd -keystore [KEYSTORE] -storepass [OLD_KEYSTORE_PASSWORD] -new [NEW_KEYSTORE_PASSWORD]

改变方式key-password:

keytool -keypasswd -keystore [KEYSTORE] -storepass [KEYSTORE_PASSWORD] -alias [ALIAS] -keypass [OLD_KEY_PASSWORD] -new [NEW_KEY_PASSWORD]

这些是与保护 spring-boot 应用程序相关的属性。您必须在这些属性中定义 keystore-password 和 key-password。

server.ssl.ciphers= # Supported SSL ciphers.
server.ssl.client-auth= # Client authentication mode.
server.ssl.enabled=true # Whether to enable SSL support.
server.ssl.enabled-protocols= # Enabled SSL protocols.
server.ssl.key-alias= # Alias that identifies the key in the key store.
server.ssl.key-password= # Password used to access the key in the key store.
server.ssl.key-store= # Path to the key store that holds the SSL certificate (typically a jks file).
server.ssl.key-store-password= # Password used to access the key store.
server.ssl.key-store-provider= # Provider for the key store.
server.ssl.key-store-type= # Type of the key store.
server.ssl.protocol=TLS # SSL protocol to use.
server.ssl.trust-store= # Trust store that holds SSL certificates.
server.ssl.trust-store-password= # Password used to access the trust store.
server.ssl.trust-store-provider= # Provider for the trust store.
server.ssl.trust-store-type= # Type of the trust store.

您可以在文档 here 中找到所有 spring-boot 属性。

如果您查看属性,有 server.ssl.key-store-passwordserver.ssl.key-password。您可以要求用户在更改全局 JKS 密码后设置这两个值。