public static KeyStore newStoreCopyContent(KeyStore originalKeyStore,
                                               char[] currentPassword,
                                               final char[] newPassword) throws GeneralSecurityException, IOException {
        if (newPassword == null) {
            throw new IllegalArgumentException("new password cannot be null");
        }
        KeyStore newKeyStore = KeyStore.getInstance(PKCS12);
        newKeyStore.load(null, newPassword);

        final Enumeration<String> aliases = originalKeyStore.aliases();
        while (aliases.hasMoreElements()) {
            String alias = aliases.nextElement();
            if (originalKeyStore.entryInstanceOf(alias, KeyStore.PrivateKeyEntry.class)) {
                newKeyStore.setKeyEntry(
                        alias,
                        originalKeyStore.getKey(alias, currentPassword),
                        newPassword,
                        originalKeyStore.getCertificateChain(alias)
                );
            } else if (originalKeyStore.entryInstanceOf(alias, KeyStore.TrustedCertificateEntry.class)) {
                newKeyStore.setCertificateEntry(alias, originalKeyStore.getCertificate(alias));
            } else if (originalKeyStore.entryInstanceOf(alias, KeyStore.SecretKeyEntry.class)) {
                newKeyStore.setEntry(alias,
                        originalKeyStore.getEntry(alias, new KeyStore.PasswordProtection(currentPassword)),
                        new KeyStore.PasswordProtection(newPassword)
                );
            }
        }
        return newKeyStore;
    }
@Test
    void testMovingManyEntiresOfTheSameType() throws Exception {
        final char[] oldPassword = "oldPass".toCharArray();
        final char[] newPassword = "newPass".toCharArray();
        KeyStore originalKeyStore = KeyStore.getInstance(PKCS12);
        originalKeyStore.load(null, oldPassword);

        CertRequest req = CertRequest.selfSigned("localhost")
                .validity(Duration.ZERO);
        final KeyPair keyPair1 = CertificateGenerator.generate(req);
        originalKeyStore.setKeyEntry("privkey1", keyPair1.privateKey(), oldPassword, new Certificate[]{keyPair1.certificate()});
        final KeyPair keyPair2 = CertificateGenerator.generate(req);
        originalKeyStore.setKeyEntry("privkey2", keyPair2.privateKey(), oldPassword, new Certificate[]{keyPair2.certificate()});
        final KeyPair keyPair3 = CertificateGenerator.generate(req);
        originalKeyStore.setKeyEntry("privkey3", keyPair3.privateKey(), oldPassword, new Certificate[]{keyPair3.certificate()});

        final KeyStore newKeyStore = KeystoreUtils.newStoreCopyContent(originalKeyStore, oldPassword, newPassword);
        assertEquals(keyPair1.privateKey(), newKeyStore.getKey("privkey1", newPassword));
        assertEquals(keyPair2.privateKey(), newKeyStore.getKey("privkey2", newPassword));
        assertEquals(keyPair3.privateKey(), newKeyStore.getKey("privkey3", newPassword));
    }