Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PGP Decryption code using Bouncy Castle jars

Posted on 2016-08-11
11
Medium Priority
?
726 Views
Last Modified: 2016-08-18
I have been trying to search code for PGP encrypted file to be decrypted using latest release of

Bouncy Castle

All the examples out there are on very old version of Jars and the latest API methods are not matching.  

Latest jars are bcpg-jdk15on-154.jar and bcprov-jdk15on-154.jar
0
Comment
Question by:MehtaJasmin
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
11 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 41752492
bc-java/pg/src/main/java/org/bouncycastle/openpgp/examples/KeyBasedFileProcessor.java

in the distribution is probably what you need
0
 
LVL 65

Expert Comment

by:btan
ID: 41752497
I dont think there is a full code sample to latest version but the porting guide will help
http://www.bouncycastle.org/wiki/display/JA1/Porting+from+earlier+BC+releases+to+1.47+and+later
that is manual mapping for object mapping based on the ASN changes but specifically we may want to explore based on BC wiki
To view some examples, look at the test programs in the packages:

org.bouncycastle.crypto.test
org.bouncycastle.jce.provider.test
org.bouncycastle.cms.test
org.bouncycastle.mail.smime.test
org.bouncycastle.openpgp.test

org.bouncycastle.tsp.test

There are also some specific example programs for dealing with Attribute Certificates, PKCS12, SMIME and OpenPGP. They can be found in:

org.bouncycastle.jce.examples
org.bouncycastle.mail.smime.examples
org.bouncycastle.openpgp.examples
org.bouncycastle.x509.examples

Finally there are also code examples from Beginning Cryptography with Java which demonstrate both the use of the JCE/JCA and also some of the Bouncy Castle APIs such as for certificate generation, CMS and S/MIME. Note: the book was written to cover J2SE 5.0, while many of the examples will work with earlier JDKs, some will not compile if you are not using J2SE 5.0 or later.
Specific t sample codes maybe can explore into the "org.bouncycastle.openpgp.examples"

(overall) https://www.bouncycastle.org/documentation.html
(sampe codes in zip) http://media.wiley.com/product_ancillary/30/07645963/DOWNLOAD/beg_crypto_examples.zip
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 41752518
Incidentally, the first method in that source is 'decryptFile'
0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 

Author Comment

by:MehtaJasmin
ID: 41752820
CEHJ,

Thanks for providing link, that is exactly what I was looking for.

But the puzzle is, all those classes are already part of latest bcpg-jdk15on-154.jar file. So for example if I want to use "decrypt" method in my own custom application, technically I can just import the KeyBasedFileProcessor class and call 'decryptFile' method.

But the way its coded, it has access 'private static void'. So I can't reuse the one from jar, I have to literally overwrite the same class in my application. Or am I missing something?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 41752957
Well don't forget it's just an example. You could of course call main with the correct parameters. Not sure why they made the methods private
0
 

Author Comment

by:MehtaJasmin
ID: 41752999
Yes, I pulled those source and created classes in my application. It works great for encryption with my public key. But for decryption, I am not sure what do I pass as 'password'. I do have private key.  By leaving password argument as empty value, I am getting below error

org.bouncycastle.openpgp.PGPException: checksum mismatch at 0 of 20

I did not set any password during encryption, and there is no parameter to do so. So not sure what to set it for decryption.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 41753193
Pretty sure that's the passphrase for the secret key
0
 

Author Comment

by:MehtaJasmin
ID: 41756735
What I am saying is, I do not provide passphrase while encrypting. I do NOT have passphrase. So what do I pass, or what is expected for decryption. Here is the code

Example

That I am invoking in my package.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 41757118
Well encryption is done with a public key so you wouln't need a passphrase
If your private key / keyring doesn't have a passphrase then try passing null or an empty array
0
 

Author Closing Comment

by:MehtaJasmin
ID: 41761110
The example worked out great for my need. After I got the passphrase from the application owner, I was able to do decryption using Bouncy Castle product for my PGP encrypted file. Thanks a lot.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 41761177
:)
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Ransomware is a growing menace to anyone using a computer or mobile device. Here are answers to some common questions about this vicious new form of malware.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.
Suggested Courses

664 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question