Search in sources :

Example 1 with EncryptionInitializationException

use of org.jasypt.exceptions.EncryptionInitializationException in project cas by apereo.

the class JasyptTestAlgorithmsCommand method testAlgorithms.

/**
 * List algorithms you can use Jasypt.
 * @param includeBC      whether to include the BouncyCastle provider
 */
@CliCommand(value = "jasypt-test-algorithms", help = "Test encryption alogrithms you can use with Jasypt to make sure encryption and decryption both work")
public void testAlgorithms(@CliOption(key = { "includeBC" }, mandatory = false, help = "Include Bouncy Castle provider", specifiedDefaultValue = "true", unspecifiedDefaultValue = "false") final boolean includeBC) {
    final String[] providers;
    if (includeBC) {
        if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null) {
            Security.addProvider(new BouncyCastleProvider());
        }
        providers = new String[] { BouncyCastleProvider.PROVIDER_NAME, "SunJCE" };
    } else {
        Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME);
        providers = new String[] { "SunJCE" };
    }
    LOGGER.info("==== JASYPT Password Based Encryption Algorithms ====\n");
    final String password = "SecretKeyValue";
    final String value = "ValueToEncrypt";
    final Set<String> pbeAlgos = AlgorithmRegistry.getAllPBEAlgorithms();
    for (final String provider : providers) {
        for (final String algorithm : pbeAlgos) {
            final CasConfigurationJasyptCipherExecutor cipher = new CasConfigurationJasyptCipherExecutor(this.environment);
            cipher.setPassword(password);
            cipher.setKeyObtentionIterations("1");
            cipher.setAlgorithm(algorithm);
            cipher.setProviderName(provider);
            try {
                final String encryptedValue;
                try {
                    encryptedValue = cipher.encryptValuePropagateExceptions(value);
                } catch (final EncryptionInitializationException e) {
                    // encryption doesn't work for this algorithm/provider combo
                    continue;
                }
                LOGGER.info("Provider: [{}] Algorithm: [{}]", provider, algorithm);
                try {
                    cipher.decryptValuePropagateExceptions(encryptedValue);
                    LOGGER.info("Encrypted Value: [{}] Decryption Succeeded", encryptedValue);
                } catch (final Exception e) {
                    LOGGER.info("Encrypted Value: [{}] Decryption Failed", encryptedValue);
                }
            } catch (final EncryptionInitializationException e) {
                if (e.getCause() instanceof NoSuchAlgorithmException) {
                    LOGGER.info("Provider: [{}] does not support Algorithm: [{}]", provider, algorithm);
                } else {
                    LOGGER.info("Error encrypting using provider: [{}] and algorithm: [{}], Message: {}", provider, algorithm, e.getMessage());
                }
            }
        }
    }
}
Also used : EncryptionInitializationException(org.jasypt.exceptions.EncryptionInitializationException) CasConfigurationJasyptCipherExecutor(org.apereo.cas.configuration.support.CasConfigurationJasyptCipherExecutor) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) EncryptionInitializationException(org.jasypt.exceptions.EncryptionInitializationException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider) CliCommand(org.springframework.shell.core.annotation.CliCommand)

Aggregations

NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 CasConfigurationJasyptCipherExecutor (org.apereo.cas.configuration.support.CasConfigurationJasyptCipherExecutor)1 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)1 EncryptionInitializationException (org.jasypt.exceptions.EncryptionInitializationException)1 CliCommand (org.springframework.shell.core.annotation.CliCommand)1