Java OpenPGP Examples
Examples without using Key Store.
Encrypt file
Decrypt file
Sign file
Verify file
Sign and Encrypt file
Decrypt and Verify file
Examples with Key Store
Generate Key pair
Import public key
Import private key
Export public key
Export private key
Encrypt file
This example demonstrates OpenPGP file encryption, providing public key stored directly in a file.
import com.didisoft.pgp.PGPLib; public class EncryptWithoutKeystore { public static void main(String[] args) throws Exception{ PGPLib pgp = new PGPLib(); boolean armor = false; boolean withIntegrityCheck = false; pgp.encryptFile("INPUT.txt", "public.key", "OUTPUT.pgp", armor, withIntegrityCheck); } }
Decrypt file
This example demonstrates OpenPGP file decryption, providing private key stored directly in a file.
The Private key password is needed, in order the key to be used.
import com.didisoft.pgp.PGPLib; public class DecryptWithoutKeystore { public static void main(String[] args) throws Exception{ PGPLib pgp = new PGPLib(); pgp.decryptFile("OUTPUT.pgp", "private.key", "changeit", "OUTPUT.txt"); } }
Sign file
This example demonstrates OpenPGP file signing providing private key stored directly in a file.
The Private key password is needed, in order the key to be used.
import com.didisoft.pgp.PGPLib; public class SignFileWithoutKeystore { public static void main(String[] args) throws Exception{ PGPLib pgp = new PGPLib(); boolean armor = false; pgp.signFile("INPUT.txt", "private.key", "changeit", "OUTPUT.pgp", armor); } }
Verify file
This example demonstrates signed OpenPGP file verification.
Having the public key of the other party we verify that they are the source of this signed file.
import com.didisoft.pgp.PGPLib; public class VerifyWithoutKeystore { public static void main(String[] args) throws Exception{ PGPLib pgp = new PGPLib(); System.out.println(pgp.verifyFile("OUTPUT.pgp", "public.key")); } }
Sign and Encrypt file
This example demonstrates One Pass signing end encryption.
import com.didisoft.pgp.PGPLib; public class SignAndEncryptWithoutKeystore { public static void main(String[] args) throws Exception{ PGPLib pgp = new PGPLib(); boolean armor = false; boolean withIntegrityCheck = false; pgp.signAndEncryptFile("INPUT.txt", "private.key", "changeit", "public.key", "OUTPUT.pgp", armor, withIntegrityCheck); } }
Decrypt and Verify file
This example demonstrates OpenPGP decryption and verification of one pass signed and encrypted file.
Having the public key of the other party we decrypt and verify that they are the source of this signed file.
import com.didisoft.pgp.PGPLib; public class DecryptAndVerifyWithoutKeystore { public static void main(String[] args) throws Exception{ PGPLib pgp = new PGPLib(); boolean validSigning = pgp.decryptAndVerifyFile("OUTPUT.txt.pgp", "private.key", "changeit", "public.key", "OUTPUT.txt"); System.out.println(validSigning); } }
Generate Keypair
This example demonstrates how to generate OpenPGP keypair using the library. Key algorithm is RSA.
import com.didisoft.pgp.KeyStore; import com.didisoft.pgp.PGPLib; public class GenerateKeys { public static void main(String[] args) throws Exception{ KeyStore keyStore = new KeyStore("pgp1.keystore", "changeit"); keyStore.generateKeyPair(1024, "test@gmail.com", "initinit"); keyStore.listKeys(); } }
Import public key in KeyStore
This example demonstrates how to import OpenPGP public key into a Key Store repository.
import com.didisoft.pgp.KeyStore; public class ImportPublicKey { public static void main(String[] args) throws Exception{ KeyStore keyStore = new KeyStore("pgp.keystore", "changeit"); keyStore.importPublicKey("public.key"); } }
Import private key in KeyStore
This example demonstrates how to import OpenPGP private key into a Key Store repository.
import com.didisoft.pgp.KeyStore; public class ImportPrivateKey { public static void main(String[] args) throws Exception { KeyStore keyStore = new KeyStore("pgp.keystore", "changeit"); keyStore.importPrivateKey("private.key"); } }
Export public key from a Key Store
This example demonstrates how to export OpenPGP public key into a file.
import com.didisoft.pgp.KeyStore; public class ExportPublicKey { public static void main(String[] args) throws Exception{ KeyStore keyStore = new KeyStore("pgp1.keystore", "changeit"); boolean armor = true; keyStore.exportPublicKey("test_public_key.key", "test@gmail.com", armor); } }
Export private key from a Key Store
This example demonstrates how to export OpenPGP private key into a file.
import com.didisoft.pgp.KeyStore; public class ExportPrivateKey { public static void main(String[] args) throws Exception{ KeyStore keyStore = new KeyStore("pgp1.keystore", "changeit"); boolean armor = true; keyStore.exportPrivateKey("test_secret_key.key", "test@gmail.com", armor); } }