Solved

PGP Decryption code using Bouncy Castle jars

Posted on 2016-08-11
11
106 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
  • 6
  • 4
11 Comments
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 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 61

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
 

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Explore the encryption capabilities built into Google Apps and how these features can help you meet privacy policy and regulatory compliance, but are not a full solution. Understand and compare the most popular email encryption services for Google A…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now