We can decrypt an OpenPGP encrypted file if it is encrypted with a public key and we have the corresponding private key. Below you will find examples that demonstrate in practice how to decrypt with DidiSoft OpenPGP Library for Java.
1) Decrypt file with private key located in file
This example demonstrates OpenPGP file decryption with a private key stored in a file. The private key password is also needed, in order the key to be used.
The last parameter is the location where we want the decrypted file to be stored. The DecryptFile method returns a String that represent the original file name of the content that was encrypted, which we can use lately to rename OUTPUT.txt with.
import com.didisoft.pgp.PGPLib;
public class DecryptFile {
public static void main(String[] args) throws Exception{
// initialize the library instance
PGPLib pgp = new PGPLib();
String privateKeyFile = "private.key";
String privateKeyPass = "changeit";
String originalFileName = pgp.decryptFile("encrypted.pgp",
privateKeyFile,
privateKeyPass,
"OUTPUT.txt");
}
}
2) Decrypt file with private key located in a KeyStore
This example shows how to decrypt a file with private key stored in a Key store. Keeping our private keys in a KeyStore gives us extra layer of security.
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 KeystoreDecryptFile {
public static void main(String[] args) throws Exception {
// initialize the KeyStore instance
KeyStore keyStore = new KeyStore("pgp.keystore", "changeit");
// initialize the library instance
PGPLib pgp = new PGPLib();
// The decrypt method returns the original name of the file
// that was encrypted. We can use it afterwards,
// to rename OUTPUT.txt to it for example.
String originalFileName = pgp.decryptFile("encrypted.pgp",
keyStore,
"changeit",
"OUTPUT.txt");
}
}
3) Decrypt stream with private key located in stream
Sometimes we may receive an encrypted data in a way suitable to be read as an input stream, in that case it is easier to decrypt it directly instead of writing it to a file before that. The example below shows how to achieve this:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import com.didisoft.pgp.PGPLib;
public class DecryptStream {
public static void main(String[] args) throws Exception{
// create instance of the library
PGPLib pgp = new PGPLib();
// obtain an encrypted data stream
InputStream encryptedStream = new FileInputStream("encrypted.pgp");
InputStream privateKeyStream = new FileInputStream("private.key");
String privateKeyPassword = "changeit";
// specify the destination stream of the decrypted data
OutputStream decryptedStream = new FileOutputStream("OUTPUT.txt");
pgp.decryptStream(encryptedStream,
privateKeyStream,
privateKeyPassword,
decryptedStream);
}
}
4) Decrypt stream with private key located in a KeyStore
This example shows how to decrypt an OpenPGP encrypted stream when our private decryption key is stored in a KeyStore object.
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 KeyStoreDecryptStream {
public static void main(String[] args) throws Exception {
// initialize the KeyStore instance
KeyStore keyStore = new KeyStore("pgp.keystore", "changeit");
// initialize the library instance
PGPLib pgp = new PGPLib();
// obtain the encrypted stream
InputStream encryptedStream = new FileInputStream("encrypted.pgp");
// specify the decrypted output stream
OutputStream decryptedStream = new FileOutputStream("OUTPUT.txt");
String decryptionKeyPassword = "changeit";
pgp.decryptStream(encryptedStream,
keyStore,
decryptionKeyPassword,
decryptedStream);
}
}
5) Exception handling
The quick exception handling solution for the decryption methods, is to catch only com.didisoft.pgp.PGPException.
However we can also catch a number of PGPException sub classes that can reveal further the cause of the error condition. In that case PGPException must be caught last.
import java.io.IOException;
import com.didisoft.pgp.*;
import com.didisoft.pgp.exceptions.*;
public class ExceptionHandlingDemo {
public static void main(String[] a) {
PGPLib pgp = new PGPLib();
try {
pgp.decrypt...
} catch (IOException e) {
// error reading input or writing output
} catch (NonPGPDataException e) {
// the passed encrypted input is not a valid OpenPGP archive
} catch (IntegrityCheckException e) {
// the passed encrypted input is corrupted
} catch (FileIsPBEEncryptedException e) {
// the passed encrypted input is encrypted with a password,
// but we try to decrypt it with a private key
} catch (WrongPrivateKeyException e) {
// the encrypted input was encrypted with a different private key
// than the provided one
} catch (WrongPasswordException e) {
// the password for the provided private key is wrong
} catch (DetachedSignatureException e) {
// the input is not an encrypted message, but a detached OpenPGP signature
} catch (PGPException e) {
// general decryption error not among the above ones
}
}
}