justinludwig / jpgpj

Java Pretty Good Privacy Jig
MIT License
74 stars 20 forks source link

Example on how to use encrypt and decrypt using Encryptor.prepareCiphertextOutputStream #31

Closed Jasperav closed 3 years ago

Jasperav commented 3 years ago

I am having trouble using Encryptor.prepareCiphertextOutputStream (maybe because I am new to PGP). For demo purposes, I am writing the output to a ByteArrayOutputStream and reading from it. This is what I did:

  1. I generated a public and private key
  2. Added this code:
package com.keylane.pgpencryption;

import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;

import org.bouncycastle.openpgp.PGPException;
import org.c02e.jpgpj.Decryptor;
import org.c02e.jpgpj.Encryptor;
import org.c02e.jpgpj.Key;
import org.c02e.jpgpj.Ring;
import org.c02e.jpgpj.key.KeyForEncryption;
import org.junit.jupiter.api.Test;

public class DemoTest {

    private String pathToFile(String file) {
        return FileSystems.getDefault().getPath(System.getProperty("user.dir"), file).toString();
    }

    @Test
    void testEncryptDecrypt() throws IOException, PGPException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        Encryptor encryptor = new Encryptor(
                new Key(new File(pathToFile("privatekey")), "test1"),
                new Key(new File(pathToFile("publickey")))
        );

        encryptor.prepareCiphertextOutputStream(baos, null, true);

        PrintWriter printWriter = new PrintWriter(
                new BufferedWriter(
                        new OutputStreamWriter(
                                baos,
                                StandardCharsets.UTF_8.name())));

        String data = "Some very sensitive data";

        printWriter.print(data);
        printWriter.close();

        if (baos.toString(StandardCharsets.UTF_8.name()).equals(data)) {
            throw new RuntimeException("baos not encrypted");
        }

        // Try to decrypt it
        Decryptor decryptor = new Decryptor(
                new Key(new File(pathToFile("privatekey")), "test1"),
                new Key(new File(pathToFile("publickey")))
        );

        ByteArrayInputStream inputStream = new ByteArrayInputStream(baos.toByteArray());

        ByteArrayOutputStream decrypted = new ByteArrayOutputStream();
        decryptor.decrypt(inputStream, decrypted);

        String decryptedString = decrypted.toString(StandardCharsets.UTF_8.name());

        if (!decryptedString.equals(data)) {
            throw new RuntimeException("Not successfully decrypted");
        }
    }
}

It fails with this error:

Exception starting decryption
org.bouncycastle.openpgp.PGPException: Exception starting decryption
    at org.bouncycastle.openpgp.PGPPublicKeyEncryptedData.getDataStream(Unknown Source)
    at org.c02e.jpgpj.Decryptor.decrypt(Decryptor.java:546)
    at org.c02e.jpgpj.Decryptor.decrypt(Decryptor.java:518)
    at org.c02e.jpgpj.Decryptor.unpack(Decryptor.java:438)
    at org.c02e.jpgpj.Decryptor.decryptWithFullDetails(Decryptor.java:387)
    at org.c02e.jpgpj.Decryptor.decrypt(Decryptor.java:357)
    at com.keylane.pgpencryption.DemoTest.testEncryptDecrypt(DemoTest.java:62)

I am not sure how I am supposed to use this and how to make it work.

Jasperav commented 3 years ago

Got it working by generating the keys from https://pgpkeygen.com/ (there was something wrong with my key)

Edit: Encrypting works, but decrypting doesn't...