OpenPGP Integration (Java and JavaScript): PGP exchange between Java and JavaScript

Secure communication

Finally, we get to the last step of our mission: implementing a working PGP exchange between Java and JavaScript. To be honest, we have already completed all the necessary steps in this tutorial’s previous posts. We now simply need to put it all together.

Here’s a quick recap of the previous episodes:

Here we go!

PGP exchange

We start by adding some values to our Identities class, purely for convenience. We specify the user ID details, keys and passphrase of our “JavaScript” user. Remember, we generated those details using OpenPGP.js during this tutorial’s previous post.

We will need those details for two reasons:

  1. We want to test the OpenPGP.js-generated keys and see if Java is able to use them for encryption / decryption work. Please note that in a normal “exchange” situation we do NOT need the sender’s private keys or passphrase, but only its public keys to verify the received message’s signature!
  2. We want to test whether a message encrypted by “JavaScript” can be decrypted by “Java”.

So how shall we proceed?

Java – JavaScript artifacts compatibility

Let’s start by checking if we can encrypt and decrypt messages using “JavaScript”‘s keys.

In the file, the test named encryptMessageAsJavascriptAndDecryptItAsJava() does exactly that. It uses “JavaScript”‘s artifacts to encrypt a message to Java:

String encryptedMessage = openPgp.encryptAndSign(

We then make “Java” decrypt the message, using “JavaScript”‘s public keys to verify the message’s signature.

String messageDecryptedByJava = openPgp.decryptAndVerify(

As you probably noticed, we leverage that OpenPGP class we wrote for this previous post. Easy-lazy!

We can of course try this the other way around, encrypting the message as “Java” and decrypting it as “JavaScript”. You can find the test for that in the encryptMessageAsJavaAndDecryptItAsJavascript() method.

Exchanging those messages

And so we swiftly move to our final milestone: make Java decrypt messages sent using OpenPGP.js and vice-versa.

Remember that, in this post, “JavaScript” encrypted a message addressed to “Java”, using “Java”‘s public keys? Well let’s start by copying the generated message into our OpenPGPTest class:

private static final String JAVASCRIPT_ENCRYPTED_MESSAGE_TO_JAVA = 
    "-----BEGIN PGP MESSAGE-----\n" +
    "Version: OpenPGP.js v3.0.11\n" +

We can now attempt to decrypt that message with Java, as described here:

public void decryptJavascriptMessageToJava() throws IOException, PGPException, NoSuchProviderException {

    String decryptedMessage = openPgp.decryptAndVerify(


Go ahead and run that test: if all went well, it should be green. That means we have successfully managed to decrypt in Java a message that was encrypted in JavaScript!

But does it work the other way around? To verify that, we copy the message we generated in encryptMessageAsJavaAndDecryptItAsJavascript() into a decrypt-java-to-javascript.html file and attempt to decrypt it using OpenPGP.js and “JavaScript”‘s keys:

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <script src="node_modules/openpgp/dist/openpgp.min.js"></script>
    <h3>Unencrypted message:</h3>
    <div id="unencryptedMessage"></div>
        const javaUserId = {name: 'java', email: ''};
        const javaPassphrase = 'java passphrase';
        const javaNumBits = 2048;
        javaPublicKeysArmored = `...`;
        const javascriptUserId = {name: 'javascript', email: ''};
        const javascriptPassphrase = 'javascript passphrase';
        const javascriptNumBits = 2048;
        const javascriptPublicKeysArmored = `...`;
        const javascriptPrivateKeysArmored = `...`;
        const javaMessageToJavascript = `-----BEGIN PGP MESSAGE-----
lots of characters here...
        // Initialize openpgp
        let openpgp = window.openpgp;
        openpgp.initWorker({path: 'node_modules/openpgp/dist/openpgp.worker.min.js'});
        // Decrypt
        let javascriptPrivateKeys = openpgp.key.readArmored(javascriptPrivateKeysArmored);
        let javascriptUnencryptedPrivateKeysPromises = => {
            return javascriptPrivateKey.decrypt(javascriptPassphrase);
        Promise.all(javascriptUnencryptedPrivateKeysPromises).then(() => {
            return openpgp.decrypt({
                message: openpgp.message.readArmored(javaMessageToJavascript),
                publicKeys: openpgp.key.readArmored(javaPublicKeysArmored).keys,
                privateKeys: javascriptPrivateKeys.keys
        }).then((unencryptedMessage) => {
            document.getElementById('unencryptedMessage').innerHTML =;

Now access that page with your browser and watch our message unfold.

Hurrah !!!

Yes, we did it: we managed to have Java and JavaScript exchange PGP-encrypted messages!

The whole code that sums up this tutorial is available on GitHub, so feel free to check it out and adapt it as you deem fit. Be patient though! Encryption is a sensitive beast that requires patience and… well, lots of unit tests !

Until next time,


Leave A Comment

Please be polite. We appreciate that. Your email address will not be published and required fields are marked

This site uses Akismet to reduce spam. Learn how your comment data is processed.