Search in sources :

Example 1 with EncoderException

use of org.bouncycastle.util.encoders.EncoderException in project nifi by apache.

the class AESSensitivePropertyProvider method protect.

/**
 * Returns the encrypted cipher text.
 *
 * @param unprotectedValue the sensitive value
 * @return the value to persist in the {@code nifi.properties} file
 * @throws SensitivePropertyProtectionException if there is an exception encrypting the value
 */
@Override
public String protect(String unprotectedValue) throws SensitivePropertyProtectionException {
    if (unprotectedValue == null || unprotectedValue.trim().length() == 0) {
        throw new IllegalArgumentException("Cannot encrypt an empty value");
    }
    // Generate IV
    byte[] iv = generateIV();
    if (iv.length < IV_LENGTH) {
        throw new IllegalArgumentException("The IV (" + iv.length + " bytes) must be at least " + IV_LENGTH + " bytes");
    }
    try {
        // Initialize cipher for encryption
        cipher.init(Cipher.ENCRYPT_MODE, this.key, new IvParameterSpec(iv));
        byte[] plainBytes = unprotectedValue.getBytes(StandardCharsets.UTF_8);
        byte[] cipherBytes = cipher.doFinal(plainBytes);
        logger.debug(getName() + " encrypted a sensitive value successfully");
        return base64Encode(iv) + DELIMITER + base64Encode(cipherBytes);
    // return Base64.toBase64String(iv) + DELIMITER + Base64.toBase64String(cipherBytes);
    } catch (BadPaddingException | IllegalBlockSizeException | EncoderException | InvalidAlgorithmParameterException | InvalidKeyException e) {
        final String msg = "Error encrypting a protected value";
        logger.error(msg, e);
        throw new SensitivePropertyProtectionException(msg, e);
    }
}
Also used : EncoderException(org.bouncycastle.util.encoders.EncoderException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException)

Example 2 with EncoderException

use of org.bouncycastle.util.encoders.EncoderException in project nifi-registry by apache.

the class AESSensitivePropertyProvider method protect.

/**
 * Returns the encrypted cipher text.
 *
 * @param unprotectedValue the sensitive value
 * @return the value to persist in the {@code nifi.properties} file
 * @throws SensitivePropertyProtectionException if there is an exception encrypting the value
 */
@Override
public String protect(String unprotectedValue) throws SensitivePropertyProtectionException {
    if (unprotectedValue == null || unprotectedValue.trim().length() == 0) {
        throw new IllegalArgumentException("Cannot encrypt an empty value");
    }
    // Generate IV
    byte[] iv = generateIV();
    if (iv.length < IV_LENGTH) {
        throw new IllegalArgumentException("The IV (" + iv.length + " bytes) must be at least " + IV_LENGTH + " bytes");
    }
    try {
        // Initialize cipher for encryption
        cipher.init(Cipher.ENCRYPT_MODE, this.key, new IvParameterSpec(iv));
        byte[] plainBytes = unprotectedValue.getBytes(StandardCharsets.UTF_8);
        byte[] cipherBytes = cipher.doFinal(plainBytes);
        logger.info(getName() + " encrypted a sensitive value successfully");
        return base64Encode(iv) + DELIMITER + base64Encode(cipherBytes);
    // return Base64.toBase64String(iv) + DELIMITER + Base64.toBase64String(cipherBytes);
    } catch (BadPaddingException | IllegalBlockSizeException | EncoderException | InvalidAlgorithmParameterException | InvalidKeyException e) {
        final String msg = "Error encrypting a protected value";
        logger.error(msg, e);
        throw new SensitivePropertyProtectionException(msg, e);
    }
}
Also used : EncoderException(org.bouncycastle.util.encoders.EncoderException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 InvalidKeyException (java.security.InvalidKeyException)2 BadPaddingException (javax.crypto.BadPaddingException)2 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)2 IvParameterSpec (javax.crypto.spec.IvParameterSpec)2 EncoderException (org.bouncycastle.util.encoders.EncoderException)2