为什么即使我们提供空密码 JKS 加载也能正常工作?

why does a JKS load work even when we supply null password?

我有一个 JKS 文件,它是使用密码生成的。我有一个 java 代码,它使用 keystore 连接到 url。现在,当我加载密钥库时,我错误地将 密码 作为 null 传递了。但令我惊讶的是,连接仍然正常。

KeyStore store = KeyStore.getInstance("JKS");
store.load(stream, null);

我觉得这种行为很可疑。对此有解释吗?

编辑:

如果此行为符合预期,那么为什么在通过 keytool 创建密钥库时需要密码?

嗯,我认为您传递的密码是为了确保密钥库本身的完整性(在更改内容本身时)。但是,如果没有提供密码,仍然可以读取内容 (keys/certificates) - 这就是您遇到的情况。

出于说明目的,您还可以通过 运行 命令查看密钥库的内容:keytool -list -keystore <keystore> - 这并不意味着将密码短语传递给密钥库本身。

您还可以在 KeyStore-object 此处阅读与 load() 函数本身相关的更多信息:http://docs.oracle.com/javase/7/docs/api/java/security/KeyStore.html#load(java.io.InputStream,%20char[])

stream - the input stream from which the keystore is loaded, or null

password - the password used to check the integrity of the keystore, the password used to unlock the keystore, or null

如果您不提供密码:

  1. 密钥库未在打开时 integrity-checked。
  2. 您无法访问任何 private-key 个条目。

对于用作 truststoreKeyStore,即仅包含受信任的证书,这是正常用法。

If this behavior is as expected, then why is the passphrase required when creating a keystore via keytool?

以便您可以在打开时提供一个,从而获得上述 (1) 或 (2) 中的行为。