Solved

PGP Decryption code using Bouncy Castle jars

Posted on 2016-08-11
11
182 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 62

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

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…
As a financial services provider, your business is impacted by two of the strictest federal regulations on record: the Sarbanes-Oxley Act and the Gramm-Leach-Bliley Act. Correctly implementing faxing into your organization to provide secure, real-ti…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

777 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