neuhalje / bouncy-gpg

Make using Bouncy Castle with OpenPGP fun again!
https://neuhalje.github.io/bouncy-gpg/
Other
207 stars 54 forks source link

Failed to decrypt the file, received gpg: [don't know]: 1st length byte missing #69

Open pheely opened 2 years ago

pheely commented 2 years ago

Describe the bug A file is encrypted in the same fashion as in EncryptMain. When trying to decrypt it using gpg --decrypt command, got the following message:

gpg: [don't know]: 1st length byte missing

To Reproduce Steps to reproduce the behavior (code sample)

  public void encrypt(String plaintextFilePath, String encryptedFilePath,
      String sender, String recipient)
      throws PGPException, IOException, SignatureException, NoSuchAlgorithmException, NoSuchProviderException {

    final InputStream input = Files.newInputStream(Path.of(plaintextFilePath));
    final OutputStream output = BouncyGPG.encryptToStream()
        .withConfig(keyringConfig)
        .withStrongAlgorithms()
        .toRecipient(recipient)
        .andSignWith(sender)
        .binaryOutput()
        .andWriteTo(new BufferedOutputStream(Files.newOutputStream(Path.of(encryptedFilePath)), BUFFER_SIZE));
    Streams.pipeAll(input, output);
    output.flush();
    output.close();
  }

Expected behavior

File should be decrypted as expected.

System (please complete the following information):

For encryption

For decryption:

Additional context Add any other context about the problem here.

GiuseppeMP commented 2 years ago

Please you may try:

public void encrypt(String plaintextFilePath, String encryptedFilePath,
      String sender, String recipient)
      throws PGPException, IOException, SignatureException, NoSuchAlgorithmException, NoSuchProviderException {

    try  ( 
    InputStream input = Files.newInputStream(Path.of(plaintextFilePath));
    OutputStream output = BouncyGPG.encryptToStream()
        .withConfig(keyringConfig)
        .withStrongAlgorithms()
        .toRecipient(recipient)
        .andSignWith(sender)
        .binaryOutput()
        .andWriteTo(new BufferedOutputStream(Files.newOutputStream(Path.of(encryptedFilePath)), BUFFER_SIZE));){

      Streams.pipeAll(input, output);
      //output.flush();
      //output.close();

 }
}