TestPgpEncryptionDecryption.java

package com.hsbc.baas;

import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.bouncycastle.openpgp.PGPPrivateKey;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPSecretKey;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

import org.bouncycastle.util.encoders.Base64;

import java.util.List;

@Log
public class TestPgpEncryptionDecryption {
    public static void main(String[] args) throws IOException {

        PgpHelper pgpHelper = new PgpHelper();

        // payload is the data to be encrypted
        String payload = "ENCRYPT_ME";

        String userKeyFilePath = "keys/MattTestPrivateKey";

        String bankKeyFilePath = "keys/MattTestPublicKey";

        String password = "1password";

        String encodedFile = "";

        BufferedInputStream dataStream = null;

        log.info("Original message: " + payload);

        // Read in public key, private key and input data (to be encrypted) file.
        try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
             BufferedInputStream inputStream = new BufferedInputStream(IOUtils.toInputStream(payload));

             BufferedInputStream secretKey = new BufferedInputStream(new FileInputStream(userKeyFilePath)); BufferedInputStream bankKey = new BufferedInputStream(new FileInputStream(bankKeyFilePath));
             ByteArrayOutputStream decryptedResult = new ByteArrayOutputStream();

        ) {
            // Create PGPSecretKey object.
            List<PGPSecretKey> key = pgpHelper.readSecretKey(secretKey);

            // Get PrivateKey Objects from SecretKey using passphrase.
            List<PGPPrivateKey> clientPrivateKeys = new ArrayList<>();

            // passphrase
            for (PGPSecretKey pgpSecretKey : key) {
                clientPrivateKeys.add(pgpHelper.findSecretKey(pgpSecretKey, password.toCharArray()));
            }

            // Get PGPPublicKey.
            List<PGPPublicKey> pgpPublicKeys = pgpHelper.readPublicKey(bankKey);

            // Taking the outputStream, inputStream data and keys to encrypt the data.
            pgpHelper.encryptAndSign(outputStream, inputStream, pgpPublicKeys.get(0), clientPrivateKeys.get(0));

            encodedFile = Base64.toBase64String(outputStream.toByteArray());

            log.info("Base64 encoded payload:\n" + encodedFile);

            byte[] decodedPayload = Base64.decode(IOUtils.toByteArray(encodedFile));

            dataStream = new BufferedInputStream(new ByteArrayInputStream(decodedPayload));

            pgpHelper.decryptStream(dataStream, decryptedResult, clientPrivateKeys, pgpPublicKeys);

            payload = decryptedResult.toString(StandardCharsets.UTF_8);

            log.info("Decrypted message:" + payload);
        } catch (Exception ex) {
            ex.printStackTrace();
            log.info("**** Exception ****");
            log.info(ex.getMessage());
        } finally {
            if (dataStream != null) dataStream.close();
        }

        log.info(encodedFile);
    }
}