Search in sources :

Example 6 with FunctionCallException

use of org.jaxen.FunctionCallException in project wso2-synapse by wso2.

the class EncryptFunction method encrypt.

/**
 * Encrypt a given plain text
 *
 * @param plainTextBytes The plaintext bytes to be encrypted
 * @return The cipher text
 * @throws FunctionCallException On error during encryption
 */
private String encrypt(byte[] plainTextBytes, String keyStorePath, String keyStorePassword, String alias, String keyStoreType, String algorithm) throws FunctionCallException {
    if (plainTextBytes == null) {
        throw new FunctionCallException("Plaintext can't be null.");
    }
    try {
        Cipher cipher = getCipherInstance(algorithm);
        KeyStore keyStore = KeyStoreManager.getKeyStore(keyStorePath, keyStorePassword, keyStoreType);
        Certificate certificate = KeyStoreManager.getCertificateFromStore(keyStore, alias);
        if (log.isDebugEnabled()) {
            log.debug("Certificate used for encrypting : " + certificate);
        }
        cipher.init(Cipher.ENCRYPT_MODE, certificate.getPublicKey());
        byte[] cipherText = cipher.doFinal(plainTextBytes);
        if (log.isDebugEnabled()) {
            log.debug(String.format("Successfully encrypted data using the algorithm '%s'", algorithm));
        }
        return Base64.getEncoder().encodeToString(cipherText);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | KeyStoreException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) {
        throw new FunctionCallException("An error occurred while encrypting data.", e);
    }
}
Also used : FunctionCallException(org.jaxen.FunctionCallException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) KeyStore(java.security.KeyStore) Certificate(java.security.cert.Certificate)

Example 7 with FunctionCallException

use of org.jaxen.FunctionCallException in project wso2-synapse by wso2.

the class Base64DecodeFunction method decode.

private Object decode(boolean debugOn, String charset, String value) throws FunctionCallException {
    if (value == null || value.isEmpty()) {
        if (debugOn) {
            log.debug("Non empty string value should be provided for decode");
        }
        return NULL_STRING;
    }
    byte[] decodedValue;
    try {
        decodedValue = new Base64().decode(value.getBytes(charset));
    } catch (UnsupportedEncodingException e) {
        String msg = "Unsupported Charset";
        log.error(msg, e);
        throw new FunctionCallException(msg, e);
    }
    String decodedString;
    try {
        decodedString = new String(decodedValue, charset).trim();
    } catch (UnsupportedEncodingException e) {
        String msg = "Unsupported Charset";
        log.error(msg, e);
        throw new FunctionCallException(msg, e);
    }
    if (debugOn) {
        log.debug("Decoded base64 encoded value: " + value + " with charset: " + charset + " to String: " + decodedString);
    }
    return decodedString;
}
Also used : Base64(org.apache.commons.codec.binary.Base64) FunctionCallException(org.jaxen.FunctionCallException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 8 with FunctionCallException

use of org.jaxen.FunctionCallException in project wso2-synapse by wso2.

the class DecryptFunction method decrypt.

/**
 * Decrypt a given cipher text
 *
 * @param encryptedText The encrypted bytes to be decrypted
 * @return The plain text string
 * @throws FunctionCallException On error during decryption
 */
private String decrypt(byte[] encryptedText, String keyStorePath, String keyStorePassword, String alias, String keyStoreType, String algorithm) throws FunctionCallException {
    if (encryptedText == null) {
        throw new FunctionCallException("Encrypted text can't be null.");
    }
    try {
        Cipher cipher = getCipherInstance(algorithm);
        KeyStore keyStore = KeyStoreManager.getKeyStore(keyStorePath, keyStorePassword, keyStoreType);
        Certificate certificate = KeyStoreManager.getCertificateFromStore(keyStore, alias);
        if (log.isDebugEnabled()) {
            log.debug("Certificate used for encrypting : " + certificate);
        }
        cipher.init(Cipher.DECRYPT_MODE, KeyStoreManager.getPrivateKeyFromKeyStore(keyStore, keyStorePassword, alias));
        byte[] decodedText = Base64.getDecoder().decode(encryptedText);
        byte[] cipherText = cipher.doFinal(decodedText);
        if (log.isDebugEnabled()) {
            log.debug(String.format("Successfully encrypted data using the algorithm '%s'", algorithm));
        }
        return new String(cipherText);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | KeyStoreException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | UnrecoverableKeyException e) {
        throw new FunctionCallException("An error occurred while encrypting data.", e);
    }
}
Also used : FunctionCallException(org.jaxen.FunctionCallException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) KeyStore(java.security.KeyStore) UnrecoverableKeyException(java.security.UnrecoverableKeyException) Cipher(javax.crypto.Cipher) Certificate(java.security.cert.Certificate)

Example 9 with FunctionCallException

use of org.jaxen.FunctionCallException in project wso2-synapse by wso2.

the class Base64EncodeFunction method encode.

private Object encode(boolean debugOn, String encoding, String value) throws FunctionCallException {
    if (value == null || "".equals(value)) {
        if (debugOn) {
            log.debug("Non emprty string value should be provided for encoding");
        }
        return NULL_STRING;
    }
    byte[] encodedValue;
    try {
        encodedValue = new Base64().encode(value.getBytes(encoding));
    } catch (UnsupportedEncodingException e) {
        String msg = "Unsupported Encoding";
        log.error(msg, e);
        throw new FunctionCallException(msg, e);
    }
    String encodedString;
    try {
        encodedString = new String(encodedValue, encoding).trim();
        encodedString = encodedString.replace("\r\n", "");
    } catch (UnsupportedEncodingException e) {
        String msg = "Unsupported Encoding";
        log.error(msg, e);
        throw new FunctionCallException(msg, e);
    }
    if (debugOn) {
        log.debug("Converted string: " + value + " with encoding: " + encoding + " to base64 encoded value: " + encodedString);
    }
    return encodedString;
}
Also used : Base64(org.apache.commons.codec.binary.Base64) FunctionCallException(org.jaxen.FunctionCallException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

FunctionCallException (org.jaxen.FunctionCallException)9 InvalidKeyException (java.security.InvalidKeyException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 URL (java.net.URL)2 KeyStore (java.security.KeyStore)2 KeyStoreException (java.security.KeyStoreException)2 Certificate (java.security.cert.Certificate)2 BadPaddingException (javax.crypto.BadPaddingException)2 Cipher (javax.crypto.Cipher)2 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)2 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)2 Base64 (org.apache.commons.codec.binary.Base64)2 PsiElement (com.intellij.psi.PsiElement)1 PsiFile (com.intellij.psi.PsiFile)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Method (java.lang.reflect.Method)1 MalformedURLException (java.net.MalformedURLException)1