In this example we will see how do to the file encryption and decryption using Apache Camel using pgp
3. Create a Camel Route to start encryption
After running the above main program camel route will start and we can see the two directories are created and we can place the files which we needs to be encrypted.

1. Generate pgp keys
In order to do the encryption/decryption we required the pgp public and private keys, in this example we will use below portal to generate the keys. Once we created the files we need to place it in to our source resource folder
2. Create a maven project and add below dependencies
<properties>
<camel.version>2.13.2</camel.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-ftp</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-crypto</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>bouncycastle</groupId>
<artifactId>bcpg-jdk13</artifactId>
<version>121</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<camel.version>2.13.2</camel.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-ftp</artifactId>
<version>2.14.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-crypto</artifactId>
<version>${camel.version}</version>
</dependency>
<dependency>
<groupId>bouncycastle</groupId>
<artifactId>bcpg-jdk13</artifactId>
<version>121</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
3. Create a Camel Route to start encryption
package com.vinod.test;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.converter.crypto.PGPDataFormat;
import org.apache.camel.main.Main;
public class EncryptionTest {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Main main = new Main();
main.enableHangupSupport();
main.addRouteBuilder(new MyRouteBuilder());
main.run(args);
}
}
class MyRouteBuilder extends RouteBuilder {
@Override
public void configure() {
try {
System.out.println("My Encryption/Decryption route started");
// Encryption
PGPDataFormat encrypt = new PGPDataFormat();
encrypt.setKeyFileName("vinodpublickeyfile.pgp");
encrypt.setKeyUserid("vinod");
from("file:tobeencrypt?noop=true;delete=true").marshal(encrypt).to("file:encrypted");
// Decryption
PGPDataFormat decrypt = new PGPDataFormat();
decrypt.setKeyFileName("vinodprivatekeyfile.pgp");
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.converter.crypto.PGPDataFormat;
import org.apache.camel.main.Main;
public class EncryptionTest {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Main main = new Main();
main.enableHangupSupport();
main.addRouteBuilder(new MyRouteBuilder());
main.run(args);
}
}
class MyRouteBuilder extends RouteBuilder {
@Override
public void configure() {
try {
System.out.println("My Encryption/Decryption route started");
// Encryption
PGPDataFormat encrypt = new PGPDataFormat();
encrypt.setKeyFileName("vinodpublickeyfile.pgp");
encrypt.setKeyUserid("vinod");
from("file:tobeencrypt?noop=true;delete=true").marshal(encrypt).to("file:encrypted");
// Decryption
PGPDataFormat decrypt = new PGPDataFormat();
decrypt.setKeyFileName("vinodprivatekeyfile.pgp");
decrypt.setKeyUserid("vinod");
decrypt.setPassword("vinod@123");
from("file:tobedecrypt").unmarshal(decrypt).to("file:decrypted");
} catch (Exception e) {
e.printStackTrace();
}
}
}
decrypt.setPassword("vinod@123");
from("file:tobedecrypt").unmarshal(decrypt).to("file:decrypted");
} catch (Exception e) {
e.printStackTrace();
}
}
}
5. Run the program
After running the above main program camel route will start and we can see the two directories are created and we can place the files which we needs to be encrypted.

Once we placed the file in to tobeencrypt directory Camel route will process that file and place in to encrypted directory after encryption


No comments:
Post a Comment