Skip to content

Instantly share code, notes, and snippets.

@nlaney
Created May 31, 2020 23:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nlaney/6b3626f811d8d6bbc13adc252f2f4d51 to your computer and use it in GitHub Desktop.
Save nlaney/6b3626f811d8d6bbc13adc252f2f4d51 to your computer and use it in GitHub Desktop.
PGP encryption/decryption using Apache Camel
package com.nml.util;
import org.apache.camel.CamelContext;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.converter.crypto.PGPDataFormat;
import org.apache.camel.impl.DefaultCamelContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class PGPEncryptor {
static final Logger LOG = LoggerFactory.getLogger(PGPEncryptor.class);
final String originalPath = "pgp/original";
final String encryptedPath = "pgp/encrypted";
final String decryptedPath = "pgp/decrypted";
public static void main(String[] args) {
try {
PGPEncryptor pgp = new PGPEncryptor();
pgp.runEncryption();
pgp.runDecryption();
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
}
private void runEncryption() throws Exception {
CamelContext ctx = new DefaultCamelContext();
ctx.addRoutes(new RouteBuilder() {
@Override
public void configure() throws Exception {
PGPDataFormat encryptFormat = new PGPDataFormat();
encryptFormat.setKeyFileName("file:keys/keyname-pub.asc");
encryptFormat.setKeyUserid("keyid");
encryptFormat.setArmored(true);
from("file:" + originalPath + "?noop=true&charset=utf-8").marshal(encryptFormat)
.to("file:" + encryptedPath + "?charset=utf-8");
}
});
ctx.start();
// Maybe sleep a little here
Thread.sleep(4000);
ctx.stop();
try {
ctx.close();
} catch (Exception e) {
// do nothing
}
}
private void runDecryption() throws Exception {
CamelContext camelContext = new DefaultCamelContext();
camelContext.addRoutes(new RouteBuilder() {
public void configure() throws Exception {
PGPDataFormat decryptFormat = new PGPDataFormat();
decryptFormat.setKeyFileName("file:keys/keyname-sec.asc");
decryptFormat.setKeyUserid("keyid");
decryptFormat.setPassword("keypass");
decryptFormat.setArmored(true);
from("file:" + encryptedPath + "?noop=true&charset=utf-8").unmarshal(decryptFormat)
.to("file:" + decryptedPath + "?charset=utf-8");
}
});
camelContext.start();
// Maybe sleep a little here
Thread.sleep(4000);
camelContext.stop();
try {
camelContext.close();
} catch (Exception e) {
// do nothing
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment