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);
}
}
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;
}
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);
}
}
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;
}
Aggregations