Implementing PGP (Pretty Good Privacy) using Bouncy Castle Crypto APIs

I was just exploring the Java API's for encrypting and decrypting the file using PGP (Pretty Good Privacy), and I came across Bouncy Castle Crypto APIs. Here we will simply see how to use the Bouncy Castle APIs.

Firstly download the cypto jar from the bouncy castle site, you can find the links in the end.

1.       Extract the Jar “crypto-145” at  
2.    The java classes are at location “.crypto-145\src\org\bouncycastle\openpgp\examples”

Use the jar’s at “crypto-145\jars” as per your SDK:-

I have used bcpg-jdk16-145.jar and bcprov-jdk16-145.jar

You need to first compile the JAVA classes.

Lets create the public and private key:-

1. We will use  RSAKeyPairGenerator.java java class to generate the key’s
     
 java org.bouncycastle.openpgp.examples.RSAKeyPairGenerator charlie "open sesame"
   ---where
           -- Charlie is identity 
       -- “open sesame” is passName
Example:
java -cp "D:\pgp\crypto-145\crypto-145\jars\bcpg-jdk16-145.jar";"D:\pgp\crypto-145\crypto-145\jars\bcprov-jdk16-145.jar";. RSAKeyPairGenerator systemA "A"

The public and private keys will appear as pub.bpg, and secret.bpg

To Encrypt the message in file:

1. Create  public and private key using above steps.
2. You can use KeyBasedFileProcessor.java to encrypt and decrypt 
3. Use following command:-
4. java -cp "D:\pgp\crypto-145\crypto-145\jars\bcpg-jdk16-145.jar";"D:\pgp\crypto-145\crypto-145\jars\bcprov-jdk16-145.jar";. KeyBasedFileProcessor -e msg.txt .\publickeys\SystemA\pub.bpg
5. –e for encryption
6. Msg.txt-file to be encrypted 
7. Pub.bpg-public key

To Decrypt the encrypted msg:-

1. java -cp "D:\pgp\crypto-145\crypto-145\jars\bcpg-jdk16-145.jar";"D:\pgp\crypto-145\crypto-145\jars\bcprov-jdk16-145.jar";. KeyBasedFileProcessor -d msg.txt.bpg .\SystemA\secret.bpg "A"
2. –d for decrypt
3. msg.txt.bpg-encrypted file
4. secret.bpg- private key
5. “A”- passName used while creating Keys

Refer Following Links for more details:-


Popular posts from this blog

JAVA embedding in Oracle SOA 12c

Passing/Receiving HTTP header in SOA BPEL

Integrating Weblogic with IBM MQ over JMS