Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
component output="false" displayname="SimplePGP" {
public SimplePGP function init(Required String config="PDS") {
variables.logger = application.logbox.getLogger(this);
switch(arguments.config) {
default: {
variables.publicKeyPath = expandPath('/path/to/key.pub');
variables.privateKeyPath = expandPath('/path/to/key.asc');
variables.passPhrase = 'abchey123';
break;
}
}
variables.sPGPWrapperFactory = createObject('java', 'me.sniggle.pgp.crypt.PGPWrapperFactory',['./bcpg-jdk15on-1.52.jar','./bcprov-jdk15on-1.52.jar','./simple-pgp-api-0.1.0.jar','./simple-pgp-java-0.1.0.jar','./slf4j-api-1.7.12.jar']);
return this;
}
// String passwordOfReceiversPrivateKey, InputStream privateKeyOfReceiver, InputStream publicKeyOfSender, InputStream encryptedData, OutputStream target
// OR
// String passwordOfReceiversPrivateKey, InputStream privateKeyOfReceiver, InputStream encryptedData, OutputStream target
public boolean function decrypt(
Required String encryptedFile,
Required String outputFile
) {
var success = false;
// catch any errors
try {
var sPGPMessageEncryptor = variables.sPGPWrapperFactory.getEncyptor();
var privateKeyFile = createobject("java", "java.io.File").init(variables.privateKeyPath);
var privateKeyStream = createobject("java", "java.io.FileInputStream").init(privateKeyFile);
var encryptedFileObj = createobject("java", "java.io.File").init(arguments.encryptedFile);
var encryptedStream = createobject("java", "java.io.FileInputStream").init(encryptedFileObj);
/*
var publicKeyFile = createobject("java", "java.io.File").init(variables.privateKeyPath);
var publicKeyStream = createobject("java", "java.io.FileInputStream").init(publicKeyFile);
*/
var decryptedTargetFile = createobject("java", "java.io.File").init(arguments.outputFile);
var decryptedTargetStream = createobject("java", "java.io.FileOutputStream").init(decryptedTargetFile);
success = sPGPMessageEncryptor.decrypt(
variables.passPhrase,
privateKeyStream,
encryptedStream,
decryptedTargetStream);
} catch(any e) {
variables.logger.error("Error Decrypting #arguments.encryptedFile#", e);
}
// try to close any open streams
try { privateKeyStream.close(); } catch(any e) {}
try { publicKeyStream.close(); } catch(any e) {}
try { encryptedStream.close(); } catch(any e) {}
try { decryptedTargetStream.close(); } catch(any e) {}
variables.logger.info("Successfuly Decrypted: #arguments.encryptedFile# -- Saved to: #arguments.outputFile#");
return success;
}
// InputStream publicKeyOfRecipient, String inputDataName, InputStream plainInputData, OutputStream target
// OR
// InputStream publicKeyOfRecipient, InputStream privateKeyOfSender, String userIdOfSender, String passwordOfSendersPrivateKey, String inputDataName, InputStream plainInputData, OutputStream target
public boolean function encrypt(
Required String encryptFile,
Required String outputFile
) {
var success = false;
// catch any errors
try {
var sPGPMessageEncryptor = variables.sPGPWrapperFactory.getEncyptor();
var publicKeyFile = createobject("java", "java.io.File").init(variables.publicKeyPath);
var publicKeyStream = createobject("java", "java.io.FileInputStream").init(publicKeyFile);
//var privateKeyFile = createobject("java", "java.io.File").init(variables.privateKeyEncryptPath);
//var privateKeyStream = createobject("java", "java.io.FileInputStream").init(privateKeyFile);
var encryptFileObj = createobject("java", "java.io.File").init(arguments.encryptFile);
var encryptStream = createobject("java", "java.io.FileInputStream").init(encryptFileObj);
var outputTargetFile = createobject("java", "java.io.File").init(arguments.outputFile);
var decryptedTargetStream = createobject("java", "java.io.FileOutputStream").init(outputTargetFile);
success = sPGPMessageEncryptor.encrypt(
publicKeyStream,
GetFileFromPath(arguments.encryptFile),
encryptStream,
decryptedTargetStream);
} catch(any e) {
variables.logger.error("Error Encrypting #arguments.encryptFile#", e);
}
// try to close any open streams
try { publicKeyStream.close(); } catch(any e) {}
try { privateKeyStream.close(); } catch(any e) {}
try { encryptStream.close(); } catch(any e) {}
try { decryptedTargetStream.close(); } catch(any e) {}
variables.logger.info("Successfuly Encrypted: #arguments.encryptFile# -- Saved to: #arguments.outputFile#");
return success;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment