Is it possible to create JKS keystore file without a password?

若如初见. 提交于 2019-12-05 03:06:31
Balazs Zsoldos

You cannot create a keystore with a blank password with keytool since a while, but you can still do it programmatically.

Read a cert like this:

private static Certificate readCert(String path) throws IOException, CertificateException {
    try (FileInputStream fin = new FileInputStream(path)) {
        return CertificateFactory.getInstance("X.509").generateCertificate(fin);
    }
}

Than create the keystore with the empty password like this:

try {
    // Reading the cert
    Certificate cert = readCert("/tmp/cert.cert");

    // Creating an empty JKS keystore
    KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
    keystore.load(null, null);

    // Adding the cert to the keystore
    keystore.setCertificateEntry("somecert", cert);

    // Saving the keystore with a zero length password
    FileOutputStream fout = new FileOutputStream("/tmp/keystore");
    keystore.store(fout, new char[0]);
} catch (GeneralSecurityException | IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Run the command:

keytool -list -keystore keystore

It will ask for a password but you can simply push an enter. You will get the following warning, but the content of the keystore will be listed:

*****************  WARNING WARNING WARNING  *****************
* The integrity of the information stored in your keystore  *
* has NOT been verified!  In order to verify its integrity, *
* you must provide your keystore password.                  *
*****************  WARNING WARNING WARNING  *****************

This might work for you.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!