Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
pheely opened this issue Mar 8, 2022 · 1 comment

Comments

@pheely
Copy link

pheely commented Mar 8, 2022

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

  • Device: MacBook Pro
  • OS: Linux
  • Java: AdoptOpenJDK 11
  • Version: 2.3.0

For decryption:

  • Device: Google Cloud VM
  • OS: RedHat Enterprise 8
  • Java: n/a
  • Version: n/a
  • GPG: gpg (GnuPG) 2.2.20

Additional context
Add any other context about the problem here.

@GiuseppeMP
Copy link

GiuseppeMP commented Mar 20, 2022

Please you may try:

  • Removing final modifier
  • Removing output flush and close, add try-resource to do this.
  • Change Path.of(String), to FileSystems.getDefault().getPath(String) for safier fs lookup.
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();

 }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants