Solved

PGP Decryption code using Bouncy Castle jars

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

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
bitbucket vs gitbucket 3 55
AWS and Java and Rest - newbie question (Is this an authentication issue) 4 65
recursion example 16 111
how to add new optional parameter to JSP 1 26
You cannot be 100% sure that you can protect your organization against crypto ransomware but you can lower down the risk and impact of the infection.
Article by: btan
Provide an easy one stop to quickly get the relevant information on common asked question on Ransomware in Expert Exchange.
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 covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

930 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

9 Experts available now in Live!

Get 1:1 Help Now