Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PGP Decryption code using Bouncy Castle jars

Posted on 2016-08-11
11
Medium Priority
?
896 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 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
When ransomware hits your clients, what do you do?

MSPs: Endpoint security isn’t enough to prevent ransomware.
As the impact and severity of crypto ransomware attacks has grown, Webroot has fought back, not just by building a next-gen endpoint solution capable of preventing ransomware attacks but also by being a thought leader.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The recent Petya-like ransomware attack served a big blow to hundreds of banks, corporations and government offices The Acronis blog takes a closer look at this damaging worm to see what’s behind it – and offers up tips on how you can safeguard your…
Will you be ready when the clock on GDPR compliance runs out? Is GDPR even something you need to worry about? Find out more about the upcoming regulation changes and download our comprehensive GDPR checklist today !
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

772 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