Encrypt

Examples ›› Programs ››
Parent Previous Next

Encryption is the most used function of the OpenPGP cryptography. In order to encrypt a file we need the public key of the recipient.


With OpenPGP Library for Java we have two options. The first one is to keep the recipient’s public key in a file on the disk. The second option is to store it in a KeyStore object.


1) Encrypt file with recipient’s public key located in a file


This example shows how to encrypt a data file, having the recipient’s public key in a file. In our case the recipient’s public key file has extension .key, but it can be anything else. The most common public key file name extensions are: *.asc, *.pkr, *.pubkr.


import com.didisoft.pgp.PGPLib;

public class EncryptFile {

 public static void main(String[] args) throws Exception{

   // create an instance of the library

   PGPLib pgp = new PGPLib();

 

   // is output ASCII or binary

   boolean asciiArmor = false;

   // should integrity check information be added

   boolean withIntegrityCheck = false;

   pgp.encryptFile("INPUT.txt",

                  "public.key",

                  "OUTPUT.pgp",

                   asciiArmor,

                   withIntegrityCheck);    

 }

}


All encrypt methods have two additional parameters:


asciiArmor specifies the format of the result file, when true the file is in ASCII armored format suitable for Email attachments, when false the output file is in binary format.


When withIntegrityCheck is true additional integrity check information is appended to the encrypted file.


2) Encrypt file with recipient’s public key located in a KeyStore


We should choose to store our OpenPGP keys in a KeyStore object when we need additional layer of security. This example shows how to encrypt a file with public key located in a Key store. ( Note that a key with UserId  demo@didisoft.com should already be imported in the KeyStore file.)


import com.didisoft.pgp.KeyStore;

import com.didisoft.pgp.PGPLib;

public class KeystoreEncryptFile {

public static void main(String[] args) throws Exception{

 // create an instance of the KeyStore

 KeyStore keyStore = new KeyStore("pgp.keystore", "changeit");

 // create an instance of the library

 PGPLib pgp = new PGPLib();        

 String recipientUserId = "demo@didisoft.com";

 // is output ASCII or binary

 boolean asciiArmor = true;

 // should integrity check information be added

 boolean withIntegrityCheck = true;

 pgp.encryptFile("INPUT.txt",

               keyStore,

               recipientUserId,

               "encrypted.pgp",

               asciiArmor,

               withIntegrityCheck);

 }

}


3) Encrypt stream with recipient’s public key located in a file


This example shows how to encrypt a stream. This way we can encrypt not only files but any other source that can be read as stream.


import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import com.didisoft.pgp.PGPLib;

public class EncryptStream {

public static void main(String[] args) throws Exception{

 // create an instance of the library

 PGPLib pgp = new PGPLib();

 // is output ASCII or binary

 boolean asciiArmor = true;

 // should integrity check information be added

 boolean withIntegrityCheck = true;

 // obtain the streams

 InputStream inStream = new FileInputStream("INPUT.txt");

 InputStream keyStream = new FileInputStream("public.key");

 OutputStream outStream = new FileOutputStream("encrypted.pgp");

 // Here "INPUT.txt" is just a string to be written in the

 // message OpenPGP packet which contains:

 // file name string, timestamp, and the actual data bytes

 pgp.encryptStream(inStream, "INPUT.txt",

                   keyStream,

                   outStream,

                   asciiArmor,

                   withIntegrityCheck);

}

}


4) Encrypt stream with recipient’s public key located in a KeyStore


In this example the message source and the encrypted output are streams too. The public key of the recipient is located in a KeyStore file.


import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import com.didisoft.pgp.KeyStore;

import com.didisoft.pgp.PGPLib;

public class KeyStoreEncryptStream {

public static void main(String[] args) throws Exception{

 // create an instance of the KeyStore

 KeyStore keyStore = new KeyStore("pgp.keystore", "changeit");

 String recipientUserId = "demo@didisoft.com";

 // create an instance of the library

 PGPLib pgp = new PGPLib();

 // is output ASCII or binary

 boolean asciiArmor = true;

 // should integrity check information be added

 boolean withIntegrityCheck = true;

 // obtain the streams

 InputStream inStream = new FileInputStream("INPUT.txt");

 OutputStream outStream = new FileOutputStream("encrypted.pgp");

 // Here "INPUT.txt" is just a string to be written in the

 // message OpenPGP packet which contains:

 // file name string, timestamp, and the actual data bytes

 pgp.encryptStream(inStream, "INPUT.txt",

                       keyStore,

                       recipientUserId,

                       outStream,

                       asciiArmor,

                       withIntegrityCheck);

}

}


5) Exception handling


All encrypt methods throw com.didisoft.pgp.PGPException.


We can catch a few sub classes of PGPException that reside in package com.didisoft.pgp.exceptions, in order to identify concrete error issues. In that case PGPException must be caught last.


Below is a part of an example code that illustrates the various exception sub classes that we can expect from the encrypt methods:


import com.didisoft.pgp.*;

import com.didisoft.pgp.exceptions.*;

public class ExceptionDemo {

public static void main(String[] a) {

  PGPLib pgp = new PGPLib();

  try {

    pgp.encrypt...

  } catch (NoPublicKeyFoundException e) {

    // the supplied public key source does not contain a public key

  } catch (KeyIsExpiredException e) {

    // the supplied public key is expired

  } catch (KeyIsRevokedException e) {

    // the supplied public key is revoked

  } catch (PGPException e) {

    // general OpenPGP encryption error

  }

}

}

For general information on the available exceptions that sub class com.didisoft.pgp.PGPException, please refer to the exception handling section.