protected void initialize() { if (isInitialized()) { logger.debug("Attempted to initialize an already-initialized StringEncryptor"); return; } if (paramsAreValid()) { if (CipherUtility.isPBECipher(algorithm)) { cipherProvider = CipherProviderFactory.getCipherProvider(KeyDerivationFunction.NIFI_LEGACY); } else { cipherProvider = CipherProviderFactory.getCipherProvider(KeyDerivationFunction.NONE); } } else { throw new EncryptionException("Cannot initialize the StringEncryptor because some configuration values are invalid"); } }
private boolean paramsAreValid() { boolean algorithmAndProviderValid = algorithmIsValid(algorithm) && providerIsValid(provider); boolean secretIsValid = false; if (CipherUtility.isPBECipher(algorithm)) { secretIsValid = passwordIsValid(password); } else if (CipherUtility.isKeyedCipher(algorithm)) { secretIsValid = keyIsValid(key, algorithm); } return algorithmAndProviderValid && secretIsValid; }
/** * Decrypts the given cipher text. * * @param cipherText the message to decrypt * @return the clear text * @throws EncryptionException if the decrypt fails */ public String decrypt(String cipherText) throws EncryptionException { try { if (isInitialized()) { byte[] plainBytes; byte[] cipherBytes = decode(cipherText); if (CipherUtility.isPBECipher(algorithm)) { plainBytes = decryptPBE(cipherBytes); } else { plainBytes = decryptKeyed(cipherBytes); } return new String(plainBytes, StandardCharsets.UTF_8); } else { throw new EncryptionException("The encryptor is not initialized"); } } catch (final Exception e) { throw new EncryptionException(e); } }
/** * Encrypts the given clear text. * * @param clearText the message to encrypt * @return the cipher text * @throws EncryptionException if the encrypt fails */ public String encrypt(String clearText) throws EncryptionException { try { if (isInitialized()) { byte[] rawBytes; if (CipherUtility.isPBECipher(algorithm)) { rawBytes = encryptPBE(clearText); } else { rawBytes = encryptKeyed(clearText); } return encode(rawBytes); } else { throw new EncryptionException("The encryptor is not initialized"); } } catch (final Exception e) { throw new EncryptionException(e); } }