Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9196345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:49:49+00:00 2026-06-17T21:49:49+00:00

I am trying to encrypt a file with PGPEncryption using BouncyCastle. When I format

  • 0

I am trying to encrypt a file with PGPEncryption using BouncyCastle.
When I format the encrypted data to AsciiArmored, decryption is also working fine.

But when I disable AsciiArmored, it’s generating around 12kb file [encrypted formated].
I tried to decrypt the same with same code, it’s now throwing below error

java.io.IOException: Stream closed
at java.io.BufferedInputStream.getInIfOpen(Unknown Source)
at java.io.BufferedInputStream.available(Unknown Source)
at org.bouncycastle.openpgp.PGPUtil$BufferedInputStreamExt.available(Unknown Source)
at org.bouncycastle.bcpg.BCPGInputStream.available(Unknown Source)
at org.bouncycastle.bcpg.BCPGInputStream$PartialInputStream.available(Unknown Source)
at org.bouncycastle.bcpg.BCPGInputStream.available(Unknown Source)
at java.io.FilterInputStream.available(Unknown Source)
at org.bouncycastle.crypto.io.CipherInputStream.nextChunk(Unknown Source)
at org.bouncycastle.crypto.io.CipherInputStream.read(Unknown Source)
at org.bouncycastle.bcpg.BCPGInputStream.read(Unknown Source)
at org.bouncycastle.openpgp.PGPEncryptedData$TruncatedStream.read(Unknown Source)
at java.io.InputStream.read(Unknown Source)
at org.bouncycastle.util.io.TeeInputStream.read(Unknown Source)
at org.bouncycastle.bcpg.BCPGInputStream.read(Unknown Source)
at org.bouncycastle.openpgp.PGPCompressedData$1.fill(Unknown Source)
at java.util.zip.InflaterInputStream.read(Unknown Source)
at org.bouncycastle.bcpg.BCPGInputStream.read(Unknown Source)
at org.bouncycastle.bcpg.BCPGInputStream$PartialInputStream.read(Unknown Source)
at org.bouncycastle.bcpg.BCPGInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)

Test Class:

public void testDecrypt() throws Exception {
        this.pgpFileProcessor.setPassphrase(PASSPHRASE);
        this.pgpFileProcessor.setSecretKeyFileName(PRI_KEY_FILE);
        this.pgpFileProcessor.setAsciiArmored(false);

        this.pgpFileProcessor.setInputFileName(OUTPUT);
        this.pgpFileProcessor.setOutputFileName(TEMP_SG);
        InputStream is = this.pgpFileProcessor.decryptToStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));

        StringBuilder sb = new StringBuilder();

        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line + "\n");
        }

        System.out.println(sb.toString());

        br.close();
        is.close();
    }

Util class

public InputStream decryptToStream() throws Exception {
    FileInputStream in = new FileInputStream(this.inputFileName);
    FileInputStream keyIn = new FileInputStream(this.secretKeyFileName);
    InputStream inputStream = this.pgpUtils.decryptFile(in, keyIn, this.passphrase.toCharArray());
    in.close();
    keyIn.close();
    return inputStream;
}

Actual Code:

public InputStream decryptFile(InputStream in, InputStream keyIn, char[] passwd) throws Exception {
    InputStream decryptedData = null;

    Security.addProvider(new BouncyCastleProvider());
    in = org.bouncycastle.openpgp.PGPUtil.getDecoderStream(in);
    PGPObjectFactory pgpF = new PGPObjectFactory(in);
    PGPEncryptedDataList enc;

    Object o = pgpF.nextObject();
    if (o instanceof PGPEncryptedDataList) {
        enc = (PGPEncryptedDataList) o;
    } else {
        enc = (PGPEncryptedDataList) pgpF.nextObject();
    }

    Iterator<PGPPublicKeyEncryptedData> it = enc.getEncryptedDataObjects();
    PGPPrivateKey sKey = null;
    PGPPublicKeyEncryptedData pbe = null;

    while (sKey == null && it.hasNext()) {
        pbe = it.next();

        sKey = findPrivateKey(keyIn, pbe.getKeyID(), passwd);
    }

    if (sKey == null) {
        in.close();
        throw new IllegalArgumentException("Secret key for message not found.");
    }
    if (pbe == null) {
        in.close();
        throw new IllegalArgumentException("Encrypted Data is Malformed");
    }

    InputStream clear = pbe.getDataStream(new BcPublicKeyDataDecryptorFactory(sKey));

    PGPObjectFactory plainFact = new PGPObjectFactory(clear);

    Object message = plainFact.nextObject();

    if (message instanceof PGPCompressedData) {
        PGPCompressedData cData = (PGPCompressedData) message;
        PGPObjectFactory pgpFact = new PGPObjectFactory(cData.getDataStream());

        message = pgpFact.nextObject();
    }

    if (message instanceof PGPLiteralData) {
        PGPLiteralData ld = (PGPLiteralData) message;

        decryptedData = ld.getInputStream();
        /*
         * int ch; while ((ch = unc.read()) >= 0) { out.write(ch); }
         */

    } else if (message instanceof PGPOnePassSignatureList) {
        in.close();
        clear.close();
        throw new PGPException("Encrypted message contains a signed message - not literal data.");
    } else {
        in.close();
        clear.close();
        throw new PGPException("Message is not a simple encrypted file - type unknown.");
    }
    if (pbe.isIntegrityProtected()) {
        if (!pbe.verify()) {
            in.close();
            clear.close();
            throw new PGPException("Message failed integrity check");
        }
    }
    clear.close();
    in.close();
    return decryptedData;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:49:51+00:00Added an answer on June 17, 2026 at 9:49 pm

    You are closing the stream in the last line of decryptFile():

    in.close();
    return decryptedData;
    

    Do not close stream there. Generally the rule is: close stream in the same method where you open it. The pattern should look like the following:

    InputStream in = null;
    try {
        in = ...
        // use the stream here.
    } finally {
        if (in != null) {
            in.close();
        }
    }
    

    Or if you are using java 7:

    try (
        InputStream in = ...; // initialize stream here
    
    ) {
        // use the stream here.
    }
    

    You do not have to close the stream initialized try (). It will be closed automatically. Enjoy java 7!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to read from a file and encrypt the data using AES
I am trying to use BouncyCastle to encrypt a file using the PKCS 7
Hi ive been trying to use System.Security.Cryptography to encrypt and decrypt a file but
I'm trying to encrypt an array of 256 bytes by using RSACryptoServiceProvider but I'm
I'm trying to encrypt a text file using Perl and then decrypt it using
I'm trying to encrypt and decrypt a file stream over a socket using RijndaelManaged,
I'm trying to encrypt the contents of one file into another file using a
I'm trying to encrypt a file using the implementation I found here: http://msdn.microsoft.com/en-us/magazine/cc164055.aspx This
I am trying to encrypt a text file using a .pfx certificate file using
i am trying to use CCCrypt to encrypt my data , but something wrong

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.