Simple to use OpenPGP API based on Bouncycastle
Go to file
Paul Schaub dd3ef89a5c
Add (failing) test for extracting certificate from key with unknown secret key encryption method
2024-04-10 10:47:13 +02:00
.github Update issue templates 2024-02-26 11:15:18 +01:00
.reuse Add Github Issue Templates to dep5 file 2024-03-05 20:38:12 +01:00
LICENSES Fix licenses for gh pages layout stuff 2022-08-04 15:21:16 +02:00
_layouts Tweaks to layout 2022-08-04 15:48:35 +02:00
assets Add pages logo 2022-08-04 15:38:44 +02:00
config/checkstyle Bump checkstyle tool to 8.45.1 2023-06-05 21:12:04 +02:00
docs Remove notice about armor's label() option 2024-03-30 19:06:42 +01:00
gradle/wrapper Bump gradlew to 7.5 2023-01-02 15:35:55 +01:00
misc Add missing accept rule to message format diagram 2023-05-16 16:54:03 +02:00
pgpainless-cli Remove tests for armor --label 2024-03-30 19:07:12 +01:00
pgpainless-core Add (failing) test for extracting certificate from key with unknown secret key encryption method 2024-04-10 10:47:13 +02:00
pgpainless-sop Document nature of tests in pgpainless-sop 2024-03-30 19:20:12 +01:00
.editorconfig Decrease continuation_indent from 8 to 4 2023-10-24 10:21:10 +02:00
.git-blame-ignore-revs Add .git-blame-ignore-revs file 2023-10-23 14:27:54 +02:00
.gitignore Fix mermaid-cli cmd 2022-07-08 00:21:40 +02:00
.readthedocs.yaml Fix mermaid-cli cmd 2022-07-08 00:21:40 +02:00
.travis.yml Add reuse tool to CI 2021-10-07 16:28:31 +02:00 Update CHANGELOG 2024-03-24 10:52:16 +01:00
CNAME Merge gh-pages stuff into main 2022-08-04 15:15:58 +02:00 Reuse compliance 2021-10-07 16:28:31 +02:00
LICENSE Add licenses for external dependencies to LICENSE file 2021-10-07 16:28:31 +02:00 PGPainless 1.6.6 2024-02-03 11:56:53 +01:00 Update 2023-12-15 18:25:18 +01:00
_config.yml Exclude irrelevant md files from gh-pages 2022-08-04 15:33:09 +02:00
build.gradle build: add `ktfmt` via Spotless 2023-10-23 17:36:00 +05:30
gradlew Declare gradle license via .reuse/dep5 2021-10-08 14:52:37 +02:00
gradlew.bat Declare gradle license via .reuse/dep5 2021-10-08 14:52:37 +02:00
settings.gradle Moved sop-java and sop-java-picocli to its own repositories 2022-01-15 02:46:42 +01:00
version.gradle Bump sop-java to 10.0.0 2024-03-24 10:52:15 +01:00

PGPainless - Use OpenPGP Painlessly!

Build Status Coverage Status Interoperability Test-Suite PGP REUSE status Documentation Status

PGPainless is an easy-to-use OpenPGP library for Java and Android applications

Packaging status Maven Central


PGPainless aims to make using OpenPGP in Java projects as simple as possible. It does so by introducing an intuitive Builder structure, which allows easy setup of encryption/decryption operations, as well as straight forward key generation.

PGPainless is based around the Bouncy Castle java library and can be used on Android down to API level 10. It can be configured to either use the Java Cryptographic Engine (JCE), or Bouncy Castles lightweight reimplementation.

While signature verification in Bouncy Castle is limited to signature correctness, PGPainless goes much further. It also checks if signing subkeys are properly bound to their primary key, if keys are expired or revoked, as well as if keys are allowed to create signatures in the first place.

These rigorous checks make PGPainless stand out from other Java-based OpenPGP libraries and are the reason why PGPainless currently scores first place on Sequoia-PGPs Interoperability Test-Suite.

At FlowCrypt we are using PGPainless in our Kotlin code bases on Android and on server side. The ergonomics of legacy PGP tooling on Java is not very good, and PGPainless improves it greatly. We were so happy with our initial tests and with Paul - the maintainer, that we decided to sponsor further development of this library.

-Tom @

Finally, testing irrefutably confirmed that the library removes many associated difficulties with PGP use in its provision of an approachable and uncomplicated API. In this regard, Paul Schaub deserves the utmost praise.

-Mario @

Get Started

The very easiest way to start using OpenPGP on Java/Kotlin based systems is to use an implementation of sop-java. sop-java defines a very stripped down API and is super easy to get started with. Luckily PGPainless provides an implementation for the sop-java interface definitions in the form of pgpainless-sop.

If you need more flexibility, directly using pgpainless-core is the way to go.


Most of PGPainless' features can be accessed directly from the PGPainless class. If you want to get started, this class is your friend :)

For further details you should check out the javadoc!

Handle Keys

Reading keys from ASCII armored strings or from binary files is easy:

        String key = "-----BEGIN PGP PRIVATE KEY BLOCK-----\n"...
        PGPSecretKeyRing secretKey = PGPainless.readKeyRing()

Similarly, keys can quickly be exported::

        PGPSecretKeyRing secretKey = ...;
        String armored = PGPainless.asciiArmor(secretKey);
        ByteArrayOutputStream binary = new ByteArrayOutputStream();

Extract a public key certificate from a secret key:

        PGPSecretKeyRing secretKey = ...;
        PGPPublicKeyRing certificate = PGPainless.extractCertificate(secretKey);

Easily Generate Keys

PGPainless comes with a simple to use KeyRingBuilder class that helps you to quickly generate modern OpenPGP keys. There are some predefined key archetypes, but it is possible to fully customize key generation to your needs.

        // RSA key without additional subkeys
        PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing()
                .simpleRsaKeyRing("Juliet <juliet@montague.lit>", RsaLength._4096);
        // EdDSA primary key with EdDSA signing- and XDH encryption subkeys
        PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing()
                .modernKeyRing("Romeo <romeo@montague.lit>", "I defy you, stars!");

        // Customized key
        PGPSecretKeyRing keyRing = PGPainless.buildKeyRing()
                        KeyFlag.SIGN_DATA, KeyFlag.CERTIFY_OTHER))
                        KeySpec.getBuilder(ECDSA.fromCurve(EllipticCurve._P256), KeyFlag.SIGN_DATA)
                                        KeyFlag.ENCRYPT_COMMS, KeyFlag.ENCRYPT_STORAGE)
                ).addUserId("Juliet <juliet@montague.lit>")

Encrypt and Sign Data

PGPainless makes it easy and painless to encrypt and/or sign data. Passed in keys are automatically evaluated, so that you don't accidentally encrypt to revoked or expired keys. PGPainless will furthermore detect which algorithms are supported by recipient keys and will negotiate algorithms accordingly. Still it allows you to manually specify which algorithms to use of course.

        EncryptionStream encryptionStream = PGPainless.encryptAndOrSign()
                                new EncryptionOptions()
                                        // optionally encrypt to a passphrase
                                        // optionally override symmetric encryption algorithm
                                new SigningOptions()
                                        // Sign in-line (using one-pass-signature packet)
                                        .addInlineSignature(secretKeyDecryptor, aliceSecKey, signatureType)
                                        // Sign using a detached signature
                                        .addDetachedSignature(secretKeyDecryptor, aliceSecKey, signatureType)
                                        // optionally override hash algorithm
                        ).setAsciiArmor(true) // Ascii armor or not

        Streams.pipeAll(plaintextInputStream, encryptionStream);

        // Information about the encryption (algorithms, detached signatures etc.)
        EncryptionResult result = encryptionStream.getResult();

Decrypt and Verify Signatures

Decrypting data and verifying signatures is being done similarly. PGPainless will not only verify correctness of signatures, but also if the signing key was allowed to create the signature. A key might not be allowed to create signatures if, for example, it expired or was revoked, or was not properly bound to the key ring. Furthermore, PGPainless will reject signatures made using weak algorithms like SHA-1. This behaviour can be modified though using the Policy class.

        DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
                .withOptions(new ConsumerOptions()
                        .addDecryptionKey(bobSecKeys, secretKeyProtector)

        Streams.pipeAll(decryptionStream, outputStream);

        // Result contains information like signature status etc.
        MessageMetadata metadata = decryptionStream.getMetadata();

After the DecryptionStream was closed, you can get metadata about the processed data by retrieving the MessageMetadata. Again, this object will contain information about how the message was encrypted, who signed it and so on.

Many more examples can be found in the examples package!!!

Include PGPainless in your Project

PGPainless is available on maven central. In order to include it in your project, just add the maven central repository and add PGPainless as a dependency.

repositories {

dependencies {
	implementation 'org.pgpainless:pgpainless-core:1.6.6'

Professional Support

Do you need a custom feature? Are you unsure of what's the best way to integrate PGPainless into your product? We offer paid professional services. Don't hesitate to send an inquiry to


Join the projects IRC channel #pgpainless on OFTC if you have any questions!

PGPainless is developed in - and accepts contributions from - the following places:

We are using SemVer (MAJOR.MINOR.PATCH) versioning, although MINOR releases could contain breaking changes from time to time.

If you want to contribute a bug fix, please check the release/X.Y branches first to see, what the oldest release is which contains the bug you are fixing. That way we can update older revisions of the library easily.

Please follow the code of conduct if you want to be part of the project.


Development on PGPainless is generously sponsored by Thank you very very very much! FlowCrypt Logo

Parts of PGPainless development (project page) will be funded by NGI Assure through NLNet.
NGI Assure is made possible with financial support from the European Commission's Next Generation Internet programme, under the aegis of DG Communications Networks, Content and Technology. NGI Assure Logo

Thanks to YourKit for providing a free license of the YourKit Java Profiler to support PGPainless Development! YourKit Logo

Big thank you also to those who decided to support the work by donating!
Notably @msfjarvis

You make my day!