use of org.springframework.shell.standard.ShellMethod in project cas by apereo.
the class StringableCipherExecutorCommand method decipher.
/**
* Decipher.
*
* @param value the value
* @param secretKeyEncryption the secret key encryption
* @param secretKeySigning the secret key signing
* @param secretKeyEncryptionSize the secret key encryption size
* @param secretKeySigningSize the secret key signing size
* @param encryptionEnabled the encryption enabled
* @param signingEnabled the signing enabled
* @return the string
*/
@SneakyThrows
@ShellMethod(key = { "decipher-text", "decode-text" }, value = "Decrypt and verify text data using keys")
public String decipher(@ShellOption(value = { "value", "--value" }, defaultValue = ShellOption.NULL, help = "Value to put through the cipher") final String value, @ShellOption(value = { "encryption-key", "--encryption-key" }, defaultValue = ShellOption.NULL, help = "Encryption key") final String secretKeyEncryption, @ShellOption(value = { "signing-key", "--signing-key" }, defaultValue = ShellOption.NULL, help = "Signing key") final String secretKeySigning, @ShellOption(value = { "encryption-key-size", "--encryption-key-size" }, defaultValue = StringUtils.EMPTY + CipherExecutor.DEFAULT_STRINGABLE_ENCRYPTION_KEY_SIZE, help = "Encryption key size") final int secretKeyEncryptionSize, @ShellOption(value = { "signing-key-size", "--signing-key-size" }, defaultValue = StringUtils.EMPTY + CipherExecutor.DEFAULT_STRINGABLE_SIGNING_KEY_SIZE, help = "Signing key size") final int secretKeySigningSize, @ShellOption(value = { "enable-encryption", "--enable-encryption" }, defaultValue = "true", help = "Whether value should be encrypted") final boolean encryptionEnabled, @ShellOption(value = { "enable-signing", "--enable-signing" }, defaultValue = "true", help = "Whether value should be signed") final boolean signingEnabled) {
var toEncode = value;
if (value != null && new File(value).exists()) {
toEncode = FileUtils.readFileToString(new File(value), StandardCharsets.UTF_8);
}
if (StringUtils.isNotBlank(toEncode)) {
val cipher = new ShellStringCipherExecutor(secretKeyEncryption, secretKeySigning, encryptionEnabled, signingEnabled, secretKeySigningSize, secretKeyEncryptionSize);
val decoded = cipher.decode(toEncode);
LOGGER.info("Decoded value: [{}]", decoded);
return decoded;
}
return null;
}
use of org.springframework.shell.standard.ShellMethod in project cas by apereo.
the class JasyptListAlgorithmsCommand method listAlgorithms.
/**
* List algorithms you can use Jasypt.
*
* @param includeBC whether to include the BouncyCastle provider
*/
@ShellMethod(key = "jasypt-list-algorithms", value = "List alogrithms you can use with Jasypt for property encryption")
public void listAlgorithms(@ShellOption(value = { "includeBC", "--includeBC" }, help = "Include Bouncy Castle provider", defaultValue = "false") final Boolean includeBC) {
if (includeBC) {
Security.addProvider(new BouncyCastleProvider());
} else {
Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME);
}
val providers = Security.getProviders();
LOGGER.info("Loaded providers: ");
for (val provider : providers) {
LOGGER.info("Provider: [{}] [{}]", provider.getName(), provider.getClass().getName());
}
val pbeAlgos = AlgorithmRegistry.getAllPBEAlgorithms();
LOGGER.info("==== JASYPT Password Based Encryption Algorithms ====\n");
for (val pbeAlgo : pbeAlgos) {
LOGGER.info(pbeAlgo.toString());
}
}
use of org.springframework.shell.standard.ShellMethod in project cas by apereo.
the class JasyptTestAlgorithmsCommand method validateAlgorithms.
/**
* List algorithms you can use Jasypt.
*/
@ShellMethod(key = "jasypt-test-algorithms", value = "Test encryption algorithms you can use with Jasypt to make sure encryption and decryption both work")
public void validateAlgorithms() {
val providers = new String[] { BouncyCastleProvider.PROVIDER_NAME, "SunJCE" };
LOGGER.info("==== JASYPT Password Based Encryption Algorithms ====\n");
val password = "SecretKeyValue";
val value = "ValueToEncrypt";
val pbeAlgos = AlgorithmRegistry.getAllPBEAlgorithms();
for (val provider : providers) {
LOGGER.trace("Testing provider [{}]", provider);
for (val algorithm : pbeAlgos) {
val cipher = new CasConfigurationJasyptCipherExecutor(this.environment);
val algorithmStr = algorithm.toString();
cipher.setPassword(password);
cipher.setKeyObtentionIterations("1");
cipher.setProviderName(provider);
if (cipher.isVectorInitializationRequiredFor(algorithmStr)) {
cipher.configureInitializationVector();
}
try {
var encryptedValue = StringUtils.EMPTY;
try {
LOGGER.trace("Testing algorithm [{}]", algorithmStr);
cipher.setAlgorithm(algorithmStr);
encryptedValue = cipher.encryptValuePropagateExceptions(value);
} catch (final Exception e) {
LOGGER.trace(e.getMessage(), e);
continue;
}
LOGGER.info("Provider: [{}] Algorithm: [{}]", provider, algorithmStr);
try {
cipher.decryptValuePropagateExceptions(encryptedValue);
LOGGER.info("Encrypted Value: [{}] Decryption succeeded", encryptedValue);
} catch (final Exception e) {
LOGGER.warn("Encrypted Value: [{}] Decryption Failed", encryptedValue);
}
} catch (final Exception e) {
if (e.getCause() instanceof NoSuchAlgorithmException) {
LOGGER.warn("Provider: [{}] does not support Algorithm: [{}]", provider, algorithmStr);
} else {
LOGGER.warn("Error encrypting using provider: [{}] and algorithm: [{}], Message: [{}]", provider, algorithmStr, e.getMessage());
}
}
}
}
}
Aggregations