// Path: modules/migration/oaep-data-migration/src/main/java/org/wso2/migration/util/DataMigrationUtil.java
// public class DataMigrationUtil {
// 
//     private DataMigrationUtil() {
// 
//     }
// 
//     public static boolean isNewlyEncrypted(String encryptedValue) throws DataMigrationException, CryptoException {
//         CryptoUtil cryptoUtil;
//         try {
//             cryptoUtil = CryptoUtil.getDefaultCryptoUtil(CarbonCoreDataHolder.getInstance().
//                             getServerConfigurationService(),
//                     CarbonCoreDataHolder.getInstance().getRegistryService());
//         } catch (Exception e) {
//             throw new DataMigrationException("Error while initializing cryptoUtil", e);
//         }
//         return cryptoUtil.base64DecodeAndIsSelfContainedCipherText(encryptedValue);
//     }
// 
//     public static String reEncryptByNewAlgorithm(String value) throws CryptoException {
//         byte[] decryptedValue = CryptoUtil.getDefaultCryptoUtil().base64DecodeAndDecrypt(
//                 value, "RSA");
//         return CryptoUtil.getDefaultCryptoUtil()
//                 .encryptAndBase64Encode(decryptedValue);
//     }
// }


import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.core.util.CryptoException;
import org.wso2.carbon.registry.core.Collection;
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.Resource;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.user.api.Tenant;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.migration.exception.DataMigrationException;
import org.wso2.migration.internal.MigrationServiceDataHolder;
import org.wso2.migration.util.DataMigrationUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.wso2.carbon.base.MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
import static org.wso2.carbon.base.MultitenantConstants.SUPER_TENANT_ID;
        }
    }

    private void migrateKeyStorePasswordForTenant(int tenantId) throws RegistryException, CryptoException,
            DataMigrationException {

        Registry registry = registryService.getGovernanceSystemRegistry(tenantId);
        if (registry.resourceExists(KEYSTORE_RESOURCE_PATH)) {
            Collection keyStoreCollection = (Collection) registry.get(KEYSTORE_RESOURCE_PATH);
            for (String keyStorePath : keyStoreCollection.getChildren()) {
                updateRegistryProperties(registry, keyStorePath,
                        new ArrayList<>(Arrays.asList(PASSWORD, PRIVATE_KEY_PASS)));
            }
        }
    }

    private void updateRegistryProperties(Registry registry, String resource, List<String> properties)
            throws RegistryException, CryptoException, DataMigrationException {
        String newValue;

        if (registry == null || StringUtils.isEmpty(resource) || CollectionUtils.isEmpty(properties)) {
            return;
        }

        if (registry.resourceExists(resource)) {
            try {
                registry.beginTransaction();
                Resource resourceObj = registry.get(resource);
                for (String encryptedPropertyName : properties) {
                    String oldValue = resourceObj.getProperty(encryptedPropertyName);
// Path: modules/migration/oaep-data-migration/src/main/java/org/wso2/migration/util/DataMigrationUtil.java
// public class DataMigrationUtil {
// 
//     private DataMigrationUtil() {
// 
//     }
// 
//     public static boolean isNewlyEncrypted(String encryptedValue) throws DataMigrationException, CryptoException {
//         CryptoUtil cryptoUtil;
//         try {
//             cryptoUtil = CryptoUtil.getDefaultCryptoUtil(CarbonCoreDataHolder.getInstance().
//                             getServerConfigurationService(),
//                     CarbonCoreDataHolder.getInstance().getRegistryService());
//         } catch (Exception e) {
//             throw new DataMigrationException("Error while initializing cryptoUtil", e);
//         }
//         return cryptoUtil.base64DecodeAndIsSelfContainedCipherText(encryptedValue);
//     }
// 
//     public static String reEncryptByNewAlgorithm(String value) throws CryptoException {
//         byte[] decryptedValue = CryptoUtil.getDefaultCryptoUtil().base64DecodeAndDecrypt(
//                 value, "RSA");
//         return CryptoUtil.getDefaultCryptoUtil()
//                 .encryptAndBase64Encode(decryptedValue);
//     }
// }


// Path: modules/migration/oaep-data-migration/src/main/java/org/wso2/migration/service/KeyStoreAndTrustStoreMigration.java
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.core.util.CryptoException;
import org.wso2.carbon.registry.core.Collection;
import org.wso2.carbon.registry.core.Registry;
import org.wso2.carbon.registry.core.Resource;
import org.wso2.carbon.registry.core.exceptions.RegistryException;
import org.wso2.carbon.registry.core.service.RegistryService;
import org.wso2.carbon.user.api.Tenant;
import org.wso2.carbon.user.api.UserStoreException;
import org.wso2.migration.exception.DataMigrationException;
import org.wso2.migration.internal.MigrationServiceDataHolder;
import org.wso2.migration.util.DataMigrationUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static org.wso2.carbon.base.MultitenantConstants.SUPER_TENANT_DOMAIN_NAME;
import static org.wso2.carbon.base.MultitenantConstants.SUPER_TENANT_ID;

        }
    }

    private void migrateKeyStorePasswordForTenant(int tenantId) throws RegistryException, CryptoException,
            DataMigrationException {

        Registry registry = registryService.getGovernanceSystemRegistry(tenantId);
        if (registry.resourceExists(KEYSTORE_RESOURCE_PATH)) {
            Collection keyStoreCollection = (Collection) registry.get(KEYSTORE_RESOURCE_PATH);
            for (String keyStorePath : keyStoreCollection.getChildren()) {
                updateRegistryProperties(registry, keyStorePath,
                        new ArrayList<>(Arrays.asList(PASSWORD, PRIVATE_KEY_PASS)));
            }
        }
    }

    private void updateRegistryProperties(Registry registry, String resource, List<String> properties)
            throws RegistryException, CryptoException, DataMigrationException {
        String newValue;

        if (registry == null || StringUtils.isEmpty(resource) || CollectionUtils.isEmpty(properties)) {
            return;
        }

        if (registry.resourceExists(resource)) {
            try {
                registry.beginTransaction();
                Resource resourceObj = registry.get(resource);
                for (String encryptedPropertyName : properties) {
                    String oldValue = resourceObj.getProperty(encryptedPropertyName);
                    if (oldValue != null && !DataMigrationUtil.isNewlyEncrypted(oldValue)) {