Request for Help on the Issue with Java BC

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Request for Help on the Issue with Java BC

Shanmugaraja Chellappen
I am getting the following exception, while decryption the data file, What should I do to over the issue. any help is appreciated.

--------------------------------------------------------------------------------------------------------------

Caused by: java.io.EOFException: Unexpected end of ZIP input stream
        at org.bouncycastle.openpgp.PGPCompressedData$2.fill(Unknown Source)
        at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
        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(StreamDecoder.java:284)
        at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
        at sun.nio.cs.StreamDecoder.read0(StreamDecoder.java:127)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:112)
        at java.io.InputStreamReader.read(InputStreamReader.java:168)
        at org.beanio.stream.fixedlength.FixedLengthReader.read(FixedLengthReader.java:162)
        at org.beanio.stream.fixedlength.FixedLengthReader.read(FixedLengthReader.java:38)
        at org.beanio.internal.parser.UnmarshallingContext.nextRecord(UnmarshallingContext.java:423)
        ... 52 more
Reply | Threaded
Open this post in threaded view
|

RE: Request for Help on the Issue with Java BC

Eckenfels. Bernd

Please provide a minimum viable test case including source code and test input.

 

From: Shanmugaraja Chellappen [mailto:[hidden email]]
Sent: Friday, April 28, 2017 7:40 PM
To: [hidden email]
Subject: [dev-crypto] Request for Help on the Issue with Java BC

 

I am getting the following exception, while decryption the data file, What should I do to over the issue. any help is appreciated.

 

--------------------------------------------------------------------------------------------------------------

 

Caused by: java.io.EOFException: Unexpected end of ZIP input stream
        at org.bouncycastle.openpgp.PGPCompressedData$2.fill(Unknown Source)
        at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
        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(StreamDecoder.java:284)
        at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
        at sun.nio.cs.StreamDecoder.read0(StreamDecoder.java:127)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:112)
        at java.io.InputStreamReader.read(InputStreamReader.java:168)
        at org.beanio.stream.fixedlength.FixedLengthReader.read(FixedLengthReader.java:162)
        at org.beanio.stream.fixedlength.FixedLengthReader.read(FixedLengthReader.java:38)
        at org.beanio.internal.parser.UnmarshallingContext.nextRecord(UnmarshallingContext.java:423)
        ... 52 more






     


SEEBURGER AG   Vorstand/SEEBURGER Executive Board:
Sitz der Gesellschaft/Registered Office:   Axel Haas, Michael Kleeberg, Friedemann Heinz, Dr. Martin Kuntz, Matthias Feßenbecker
Edisonstr. 1  
D-75015 Bretten Vorsitzende des Aufsichtsrats/Chairperson of the SEEBURGER Supervisory Board:
Tel.: 07252 / 96 - 0 Prof. Dr. Simone Zeuchner
Fax: 07252 / 96 - 2222
Internet: http://www.seeburger.de Registergericht/Commercial Register:
e-mail: [hidden email] HRB 240708 Mannheim


Dieses E-Mail ist nur für den Empfänger bestimmt, an den es gerichtet ist und kann vertrauliches bzw. unter das Berufsgeheimnis fallendes Material enthalten. Jegliche darin enthaltene Ansicht oder Meinungsäußerung ist die des Autors und stellt nicht notwendigerweise die Ansicht oder Meinung der SEEBURGER AG dar. Sind Sie nicht der Empfänger, so haben Sie diese E-Mail irrtümlich erhalten und jegliche Verwendung, Veröffentlichung, Weiterleitung, Abschrift oder jeglicher Druck dieser E-Mail ist strengstens untersagt. Weder die SEEBURGER AG noch der Absender (Eckenfels. Bernd) übernehmen die Haftung für Viren; es obliegt Ihrer Verantwortung, die E-Mail und deren Anhänge auf Viren zu prüfen.

This email is intended only for the recipient(s) to whom it is addressed. This email may contain confidential material that may be protected by professional secrecy. Any fact or opinion contained, or expression of the material herein, does not necessarily reflect that of SEEBURGER AG. If you are not the addressee or if you have received this email in error, any use, publication or distribution including forwarding, copying or printing is strictly prohibited. Neither SEEBURGER AG, nor the sender (Eckenfels. Bernd) accept liability for viruses; it is your responsibility to check this email and its attachments for viruses.

Reply | Threaded
Open this post in threaded view
|

Re: Request for Help on the Issue with Java BC

Lothar Kimmeringer-4
In reply to this post by Shanmugaraja Chellappen
Hi,

Am 28.04.2017 um 19:40 schrieb Shanmugaraja Chellappen:
> I am getting the following exception, while decryption the data file,
>  What should I do to over the issue. any help is appreciated.
>
> --------------------------------------------------------------------------------------------------------------
>
> Caused by: java.io.EOFException: Unexpected end of ZIP input stream

My crystal ball revealed to me that you read in bytes as a string, that
corrupted data since there is a byte-to-char-conversion happening that
is dependent on the system-charset because you haven't used the constructor
allowing you to specify the charset.

If that's not the reason,

  - my crystal ball needs a checkup
  - you need to provide more information as Bernd said


Cheers, Lothar

Reply | Threaded
Open this post in threaded view
|

RE: Request for Help on the Issue with Java BC

Eckenfels. Bernd
In reply to this post by Eckenfels. Bernd
Hello,

The two different versions you sent me differ in the compressed entity handling. I think it is quite normal that you have to read or skip the compressed data. You cannot skipmto the next object (MIc record) and after that read from the zipped content.

Gruss
Bernd

--
http://www.seeburger.com
________________________________________
From: Shanmugaraja Chellappen [[hidden email]]
Sent: Saturday, April 29, 2017 23:25
To: Eckenfels. Bernd
Subject: Re: [dev-crypto] Request for Help on the Issue with Java BC

Hello Bernd,

I am using the following code to Decrypt:  I may not able to share the input file due to sensitivity. How ever I am able to decryprt the file using the method 2.  In method one I have changed the return type as BufferedInputStream  and I need to use the BufferedInputStream  to further process the data to Database.

Any help is highly appreciated.

method 1:

@SuppressWarnings("unchecked")
 public static BufferedInputStream decryptFileOld(InputStream in, InputStream keyIn, char[] passwd)  throws Exception {


     logger.info("Staring PGP decryption ....");

     BufferedInputStream buffInStrm;
     InputStream unc;

     Security.addProvider(new BouncyCastleProvider());
     InputStream ins = org.bouncycastle.openpgp.PGPUtil.getDecoderStream(in);
        PGPObjectFactory pgpF = new PGPObjectFactory(ins, null);
        PGPEncryptedDataList enc;
        Object o = pgpF.nextObject();
        //
        // the first object might be a PGP marker packet.
        //
        if (o instanceof  PGPEncryptedDataList) {
            enc = (PGPEncryptedDataList) o;
        } else {
            enc = (PGPEncryptedDataList) pgpF.nextObject();
        }
        //
        // find the secret key
        //
        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) {
            throw new IllegalArgumentException("Secret key for message not found.");
        }
        InputStream clear = pbe.getDataStream(new BcPublicKeyDataDecryptorFactory(sKey));
        PGPObjectFactory plainFact = new PGPObjectFactory(clear, new BcKeyFingerprintCalculator());
        Object message = plainFact.nextObject();
        logger.info("message  ...."+message);
        if (message instanceof  PGPCompressedData) {
         logger.info("Staring PGP decryption PGPCompressedData ....");
            PGPCompressedData cData = (PGPCompressedData) message;
            PGPObjectFactory pgpFact = new PGPObjectFactory(cData.getDataStream(), new BcKeyFingerprintCalculator());
            message = pgpFact.nextObject();
        }
        if (message instanceof  PGPLiteralData) {
         logger.info("Staring PGP decryption PGPLiteralData ....");
            PGPLiteralData ld = (PGPLiteralData) message;
            unc = ld.getDataStream();
            buffInStrm=new BufferedInputStream(unc);

//            int ch;
//            while ((ch = unc.read()) >= 0) {
//                out.write(ch);
//            }
        } else if (message instanceof  PGPOnePassSignatureList) {
            throw new PGPException("Encrypted message contains a signed message - not literal data.");
        } else {
            throw new PGPException("Message is not a simple encrypted file - type unknown.");
        }
        if (pbe.isIntegrityProtected()) {
            if (!pbe.verify()) {
             throw new PGPException("Message failed integrity check");
            }
        }
        return buffInStrm;
    }
-----------------------------------------------------------------

method 2:

@SuppressWarnings("unchecked")
 public static void decryptFile(InputStream in, OutputStream out, InputStream keyIn, char[] passwd) throws Exception {

     Security.addProvider(new BouncyCastleProvider());
        in = org.bouncycastle.openpgp.PGPUtil.getDecoderStream(in);
        PGPObjectFactory pgpF = new PGPObjectFactory(in, null);
        PGPEncryptedDataList enc;
        Object o = pgpF.nextObject();
        //
        // the first object might be a PGP marker packet.
        //
        if (o instanceof  PGPEncryptedDataList) {
            enc = (PGPEncryptedDataList) o;
        } else {
            enc = (PGPEncryptedDataList) pgpF.nextObject();
        }
        //
        // find the secret key
        //
        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) {
            throw new IllegalArgumentException("Secret key for message not found.");
        }
        InputStream clear = pbe.getDataStream(new BcPublicKeyDataDecryptorFactory(sKey));
        PGPObjectFactory plainFact = new PGPObjectFactory(clear, null);
        Object message = plainFact.nextObject();
        if (message instanceof  PGPCompressedData) {
            PGPCompressedData cData = (PGPCompressedData) message;
            PGPObjectFactory pgpFact = new PGPObjectFactory(cData.getDataStream(), null);
            message = pgpFact.nextObject();
        }
        if (message instanceof  PGPLiteralData) {
            PGPLiteralData ld = (PGPLiteralData) message;
            InputStream unc = ld.getInputStream();
            int ch;
            while ((ch = unc.read()) >= 0) {
                out.write(ch);
            }
        } else if (message instanceof  PGPOnePassSignatureList) {
            throw new PGPException("Encrypted message contains a signed message - not literal data.");
        } else {
            throw new PGPException("Message is not a simple encrypted file - type unknown.");
        }
        if (pbe.isIntegrityProtected()) {
            if (!pbe.verify()) {
             throw new PGPException("Message failed integrity check");
            }
        }
    }

Thanks and Regards
-Raj


On Friday, April 28, 2017 4:34 PM, Eckenfels. Bernd <[hidden email]> wrote:


Please provide a minimum viable test case including source code and test input.

From: Shanmugaraja Chellappen [mailto:[hidden email]]
Sent: Friday, April 28, 2017 7:40 PM
To: [hidden email]
Subject: [dev-crypto] Request for Help on the Issue with Java BC

I am getting the following exception, while decryption the data file, What should I do to over the issue. any help is appreciated.

--------------------------------------------------------------------------------------------------------------

Caused by: java.io.EOFException: Unexpected end of ZIP input stream
        at org.bouncycastle.openpgp.PGPCompressedData$2.fill(Unknown Source)
        at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
        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(StreamDecoder.java:284)
        at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
        at sun.nio.cs.StreamDecoder.read0(StreamDecoder.java:127)
        at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:112)
        at java.io.InputStreamReader.read(InputStreamReader.java:168)
        at org.beanio.stream.fixedlength.FixedLengthReader.read(FixedLengthReader.java:162)
        at org.beanio.stream.fixedlength.FixedLengthReader.read(FixedLengthReader.java:38)
        at org.beanio.internal.parser.UnmarshallingContext.nextRecord(UnmarshallingContext.java:423)
        ... 52 more








SEEBURGER AG            Vorstand/SEEBURGER Executive Board:
Sitz der Gesellschaft/Registered Office:                Axel Haas, Michael Kleeberg, Friedemann Heinz, Dr. Martin Kuntz, Matthias Feßenbecker
Edisonstr. 1
D-75015 Bretten         Vorsitzende des Aufsichtsrats/Chairperson of the SEEBURGER Supervisory Board:
Tel.: 07252 / 96 - 0            Prof. Dr. Simone Zeuchner
Fax: 07252 / 96 - 2222
Internet: http://www.seeburger.de               Registergericht/Commercial Register:
e-mail: [hidden email]               HRB 240708 Mannheim

Dieses E-Mail ist nur für den Empfänger bestimmt, an den es gerichtet ist und kann vertrauliches bzw. unter das Berufsgeheimnis fallendes Material enthalten. Jegliche darin enthaltene Ansicht oder Meinungsäußerung ist die des Autors und stellt nicht notwendigerweise die Ansicht oder Meinung der SEEBURGER AG dar. Sind Sie nicht der Empfänger, so haben Sie diese E-Mail irrtümlich erhalten und jegliche Verwendung, Veröffentlichung, Weiterleitung, Abschrift oder jeglicher Druck dieser E-Mail ist strengstens untersagt. Weder die SEEBURGER AG noch der Absender (Eckenfels. Bernd) übernehmen die Haftung für Viren; es obliegt Ihrer Verantwortung, die E-Mail und deren Anhänge auf Viren zu prüfen.

This email is intended only for the recipient(s) to whom it is addressed. This email may contain confidential material that may be protected by professional secrecy. Any fact or opinion contained, or expression of the material herein, does not necessarily reflect that of SEEBURGER AG. If you are not the addressee or if you have received this email in error, any use, publication or distribution including forwarding, copying or printing is strictly prohibited. Neither SEEBURGER AG, nor the sender (Eckenfels. Bernd) accept liability for viruses; it is your responsibility to check this email and its attachments for viruses.











SEEBURGER AG            Vorstand/SEEBURGER Executive Board:
Sitz der Gesellschaft/Registered Office:                Axel Haas, Michael Kleeberg, Friedemann Heinz, Dr. Martin Kuntz, Matthias Feßenbecker
Edisonstr. 1
D-75015 Bretten         Vorsitzende des Aufsichtsrats/Chairperson of the SEEBURGER Supervisory Board:
Tel.: 07252 / 96 - 0            Prof. Dr. Simone Zeuchner
Fax: 07252 / 96 - 2222
Internet: http://www.seeburger.de               Registergericht/Commercial Register:
e-mail: [hidden email]               HRB 240708 Mannheim


Dieses E-Mail ist nur für den Empfänger bestimmt, an den es gerichtet ist und kann vertrauliches bzw. unter das Berufsgeheimnis fallendes Material enthalten. Jegliche darin enthaltene Ansicht oder Meinungsäußerung ist die des Autors und stellt nicht notwendigerweise die Ansicht oder Meinung der SEEBURGER AG dar. Sind Sie nicht der Empfänger, so haben Sie diese E-Mail irrtümlich erhalten und jegliche Verwendung, Veröffentlichung, Weiterleitung, Abschrift oder jeglicher Druck dieser E-Mail ist strengstens untersagt. Weder die SEEBURGER AG noch der Absender (Eckenfels. Bernd) übernehmen die Haftung für Viren; es obliegt Ihrer Verantwortung, die E-Mail und deren Anhänge auf Viren zu prüfen.


This email is intended only for the recipient(s) to whom it is addressed. This email may contain confidential material that may be protected by professional secrecy. Any fact or opinion contained, or expression of the material herein, does not necessarily reflect that of SEEBURGER AG. If you are not the addressee or if you have received this email in error, any use, publication or distribution including forwarding, copying or printing is strictly prohibited. Neither SEEBURGER AG, nor the sender (Eckenfels. Bernd) accept liability for viruses; it is your responsibility to check this email and its attachments for viruses.