Java Key Store 总是以空别名结尾

Java Key Store always ends up with null aliases

我已经尝试了几天了,但我无可救药地卡住了。 为了完全理解 java 密钥库的工作原理,我一直在尝试创建自己的密钥库,将一些东西放入其中,然后从另一个程序中检索它们。

这是我的密钥库生成器:

{

    //generate a X509 certificate
    Security.addProvider(new BouncyCastleProvider());
    CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509", "BC");
    X509Certificate certificate = (X509Certificate) certificateFactory.generateCertificate(new FileInputStream("certificate.cer"));

    LOGGER.debug("BouncyCastle provider & X509 certificate added.");

    //generate a private & a public key
    KeyPair keyPair = generateRSAKeyPair();
    RSAPrivateKey priv = (RSAPrivateKey) keyPair.getPrivate();
    RSAPublicKey pub = (RSAPublicKey) keyPair.getPublic();

    //generate a keystore
    KeyStore ks = KeyStore.getInstance("PKCS12");
    char[] keyStorePassword = "keystore_password".toCharArray();
    ks.load(null, keyStorePassword);
    try (FileOutputStream fos = new FileOutputStream("TestKeyStore.jks")) {
        ks.store(fos, keyStorePassword);
    }

    ks.load(new FileInputStream("TestKeyStore.jks"), keyStorePassword);

    //Symmetric key
    SecretKey secretKey = KeyGenerator.getInstance("AES").generateKey();
    KeyStore.SecretKeyEntry secretKeyEntry = new KeyStore.SecretKeyEntry((secretKey));
    KeyStore.ProtectionParameter protectionParameter = new KeyStore.PasswordProtection(keyStorePassword);
    ks.setEntry("symmetric_key", secretKeyEntry, protectionParameter);

    //Asymmetric key
    X509Certificate[] x509Certificates = new X509Certificate[1];
    x509Certificates[0] = certificate;
    ks.setKeyEntry("asymmetric key", priv, keyStorePassword, x509Certificates);

    //certificate
    ks.setCertificateEntry("test_certif", certificate);

    Key key = ks.getKey("symmetric_key", keyStorePassword);
    System.out.println("I have this symmetric key : " + key);
    X509Certificate certificate1 = (X509Certificate) ks.getCertificate("test_certif");
    System.out.println("I have this certificate : " + certificate1);

    System.out.println(ks.aliases().nextElement());


    LOGGER.debug("all went well");
}

正如您可能注意到的那样,它并不完美:我的目标目前只是将一些东西放入密钥库中。但是这里的重点,从上一个System.out.println(ks.aliases().nextElement());开始,就是要看keystore里面确实有东西。这工作得很好,它返回 symmetric_key.

现在,同一个文件夹中有另一个 class 应该从该密钥库中读取。

注意:文件没有问题(我已经通过移动其本地化进行了测试)所以不可能是那样。

KeyStore ks = KeyStore.getInstance("PKCS12");
        char[] keyStorePassword = "keystore_password".toCharArray();
        ks.load(new FileInputStream("TestKeyStore.jks"), keyStorePassword);
        System.out.println(ks.containsAlias("symmetric_key"));

这总是让我:false

如果我尝试这样做:System.out.println(ks.aliases());,它总是 null

如果我试试这个:

if (!keystore.aliases().hasMoreElements()) {
    System.out.println("nothing inside the keystore");
}

它给了我回报 nothing inside the keystore

尽管在生成器中并非如此。

有线索吗? 谢谢

问题是您在写入密钥库后设置条目。

如果你移动:

try (FileOutputStream fos = new FileOutputStream("TestKeyStore.jks")) {
    ks.store(fos, keyStorePassword);
}

直到这一行之后:

ks.setCertificateEntry("test_certif", certificate);

一切正常。

下面是一个工作示例,为清楚起见删除了一些代码:

public static void main(String[] args) throws Exception{

    final KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
    keyGen.initialize(2048);
    final KeyPair keyPair = keyGen.genKeyPair();

    RSAPrivateKey priv = (RSAPrivateKey) keyPair.getPrivate();

    //generate a keystore
    KeyStore ks = KeyStore.getInstance("PKCS12");
    char[] keyStorePassword = PASSWORD;
    ks.load(null, keyStorePassword);

    X509Certificate[] chain = {generateCertificate("cn=Unknown", keyPair, 365, "SHA256withRSA")};

    // saving one keypair in keystore object
    ks.setKeyEntry("asymmetric key", keyPair.getPrivate(), keyStorePassword, chain);

    //Symmetric key
    SecretKey secretKey = KeyGenerator.getInstance("AES").generateKey();
    KeyStore.SecretKeyEntry secretKeyEntry = new KeyStore.SecretKeyEntry((secretKey));
    KeyStore.ProtectionParameter protectionParameter = new KeyStore.PasswordProtection(keyStorePassword);
    // saving symmetric key in keystore object
    ks.setEntry("symmetric_key", secretKeyEntry, protectionParameter);

    // Saving our keystore object into the filesystem
    try (FileOutputStream fos = new FileOutputStream("TestKeyStore.p12")) {
        ks.store(fos, keyStorePassword);
    }

// 剩下的代码 }

之所以在保存密钥库的方法中有效,是因为更改后的密钥库仍在内存中,但不在文件系统中。此外,由于您正在创建一个 PKCS12 密钥库,我会避免使用 .jks 扩展名并选择 .pkcs12 之类的东西。