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 testDifferentEntriesMoving() throws Exception {
        final char[] oldPassword = "oldPass".toCharArray();
        final char[] newPassword = "newPass".toCharArray();
        KeyStore originalKeyStore = KeyStore.getInstance(PKCS12);
        originalKeyStore.load(null, oldPassword);

        final byte[] originalSecretKey = {0x54, 0x68, 0x61, 0x74, 0x73, 0x20, 0x6D, 0x79, 0x20, 0x4B, 0x75, 0x6E, 0x67, 0x20, 0x46, 0x75};
        KeyStore.SecretKeyEntry secret
                = new KeyStore.SecretKeyEntry(new SecretKeySpec(originalSecretKey,
                "AES"));
        originalKeyStore.setEntry("secretEntry", secret, new KeyStore.PasswordProtection(oldPassword));
        CertRequest req = CertRequest.selfSigned("darknet.net")
                .validity(Duration.ZERO);
        final X509Certificate trustedCA = CertificateGenerator.generate(req).certificate();
        originalKeyStore.setCertificateEntry("trusted-certificate", trustedCA);
        req = CertRequest.selfSigned("localhost")
                .validity(Duration.ZERO);
        final KeyPair keyPair = CertificateGenerator.generate(req);
        originalKeyStore.setKeyEntry("privkey", keyPair.privateKey(), oldPassword, new Certificate[]{keyPair.certificate()});

        final KeyStore newKeyStore = KeystoreUtils.newStoreCopyContent(originalKeyStore, oldPassword, newPassword);
        final KeyStore.Entry secretRetrieved = newKeyStore.getEntry("secretEntry", new KeyStore.PasswordProtection(newPassword));
        final Certificate certificateRetrieved = newKeyStore.getCertificate("trusted-certificate");
        final Key privkeyRetrieved = newKeyStore.getKey("privkey", newPassword);

        //Verify if all 3 entry types have been moved without any changes
        assertArrayEquals(originalSecretKey, ((KeyStore.SecretKeyEntry) secretRetrieved).getSecretKey().getEncoded());
        assertEquals(trustedCA, certificateRetrieved);
        assertEquals(keyPair.privateKey(), privkeyRetrieved);

    }