A new version of the code (and a new post) has been posted at :
http://sloanseaman.com/wordpress/2012/05/13/revisited-pgp-encryptiondecryption-in-java/
Please note that if you are using Bouncy Castles libraries < 1.47 you should use the code in this post
http://sloanseaman.com/wordpress/2012/05/13/revisited-pgp-encryptiondecryption-in-java/
Please note that if you are using Bouncy Castles libraries < 1.47 you should use the code in this post
I have a requirement where I need to decrypt a file that is PGP encrypted for a project I am working on. At the same time I need a unit test for my code so I need to encrypt a file as well.
I started digging around and found that there is really no documentation I could find that covered the whole process. Some postings only covered encryption, some decryption, but none covered both as well as how to make the public/private keys.
How to Securely encrypt and decrypt files using 7zip.In this video I illustrate step-by-step how to securely create encrypted and compressed file archives us. PES CPK Crypter V1.0 By Devil Cold52 Production cpk file's Decryption / Encryption Decryption/Encryption of CPK's Files. Drag and Drop Function. PES 2016 Save Files Encrypter and Decrypter v1 by Mustafa U. This program decrypt or encrypt the PES 2016 Crypted file. Decrypt PES 2016 save file.
So, here you go ?
The Downloads/Installs
Before you even start coding we need to discuss Java Security. The JRE by default ships with security for cryptography called Java Cryptography Extension (JCE). The JCE will support PGP but because import/export of cryptography can be sketchy, it only supports weak keys by default (think keys that wouldn’t be too hard to crack). PGP won’t work under this environment so you need to first address this.
JCE Update
Credit where due: This part is taken mainly from this article on jce policy files
Go to the Java download page and go all the way to the bottom and download the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files. The number at the end (currently 6 or 7) is just the JRE version.
Go to the Java download page and go all the way to the bottom and download the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files. The number at the end (currently 6 or 7) is just the JRE version.
Extract the files and copy the two jars to your JRE’s /lib/security directory. This will override two existing jars and will allow you to use strong keys in security.
PGP Tools
Next you need to create you public and private key files. Since Symantec bought PGP in 2010 most of the freeware stuff has disappeared and most links lead to Symantec’s website. I found an old freeware version of PGP (Windows only) at http://www.pgpi.org/products/pgp/versions/freeware/win32/6.5.8/. Please note that newer version don’t work on Windows 7 or are owned by Symantac.
Install the program and then launch it by running PGPKeys.exe. To use it:
- Select Keys -> New Key
- Click Next
- Enter your information
- Click Next
- Use the Diffie-Hellman/DSS key Pair Type
- Click Next
- Select your Key Pair Size (I just used 1024 since I’m not that worried about it for now)
- Click Next
- Leave your Key Expiration as never expires
- Click Next
- Enter the passphrase (password) you want to use
- Click Next
- If you entered less than 8 characters you will be warned. Click Next
- Key will be generated
- Click Next
- Click Finish
The files that were generated and that you will need to use in your code can be found by going to Edit -> Options -> Files.
Find these files and copy them to someplace where your code can reach them.
Find these files and copy them to someplace where your code can reach them.
Side note: If you just need a basic public/private key you can generate ones at https://www.igolder.com/pgp/generate-key/
BouncyCastle’s PGP Libraries
I’m using the PGP library from Bouncy Castle. Honestly it’s poorly documented and rather complex but it’s the best and most common one that I could find.
Either download the latest from http://www.bouncycastle.org/latest_releases.html or use maven like so:
Either download the latest from http://www.bouncycastle.org/latest_releases.html or use maven like so:
You are now (finally) ready to actually code something!
The Code
First lets write a general util that will handle any InputStreams (shouldn’t we all be using Readers by now?) and then lets write one that wraps the first one and will handle files for us.
Here is the first util which will handle any type of InputStream:
I’m not going to go over it line-by-line because, honestly, I haven’t looked at it that hard so I’m not sure what it’s all doing ? Sorry.
Next let’s write a util that allows us to easily work with files (and Spring, if you are into that sort of thing :))
Remember that if you use the PGPFileProcessor in Spring and intend on changing things programmatically via the get/sets you need to make the scope of the bean “prototype” otherwise you will have issues in a multi-threaded environment.
That should do it! You should be able to encrypt and decrypt files (streams really) using PGP!
Support for signed files
If you wish to support signed files, Mateusz Klos posted an excellent comment below that modifies the above code to support signed files.
I didn’t incorporate it into my code because I think he should get full credit for the modifications ?
Common Errors
Illegal key size or default parameters
It’s yelling at you because the default JRE security can’t handle PGP.
You didn’t follow the instructions (above) on updating the JCE jars in your JRE’s lib/security directory.
It’s yelling at you because the default JRE security can’t handle PGP.
You didn’t follow the instructions (above) on updating the JCE jars in your JRE’s lib/security directory.
Secret key for message not found
Regenerate you pubring and secring using the PGP tool. Odds are something is wrong with them
Regenerate you pubring and secring using the PGP tool. Odds are something is wrong with them
The Comments
(Note: Added 4/2/2010)
There had been a lot of great activity in the comments as well as some very kind readers who have posted code snippets to do things that my initial library did not do. I highly suggest you dig through the comments if what you are looking for is not in my original post.
There had been a lot of great activity in the comments as well as some very kind readers who have posted code snippets to do things that my initial library did not do. I highly suggest you dig through the comments if what you are looking for is not in my original post.
v6.7
-added context menu option : Copy Absolute Paththis will copy the actual path to that file/folder in the cpk, relative to your CPK extract directory. so you can paste the path into your windows explorer window and instantly jump to it.
-when doubling clicking a search result (in Full Search mode) the program will jump to the folder AND jump to the location of the selected item
-when doing a normal search (without Full Search mode) a 'Revert' button will appear which will revert the directory to its original state before searching
-ability to extract folders from the tree view (left panel)as well as the same extra context menu options from right panel
-added 'Recent opened files' menu
v6.6
-add extra context menu options : Copy File Path and Copy File Name
-several bug fixes
-Added support for Super Dragon Ball Heroes World Mission CPK repacking
v6.5
-Fixed various bugs caused by the last update
-CPK Browser now requires .NET Framework 4.5 or above
-Added a new feature : M2C files (Mod2CPK)
so what are M2C files? they are a way to loosely simulate how X2Ms work.
for more info on these files check out The Citadel Server
v6.0
![Decrypt cpk file folder Decrypt cpk file folder](/uploads/1/1/9/0/119032320/278041913.jpg)
-Some adjustments (Back button, Progress Bar..)
-Added Dark Theme [Settings->Dark Theme]
-Ability to Drag and Drop CPK into tool
--Ability to extract (and replace) Search Results
-Added 'CTRL + A' functionality
-Fixed bug with extracting large files (movie.cpk), now the program is 64 bit only
-Added CPK Compare option (compare 2 CPK for differences without extracting) [Tools -> Compare CPK]
-Added 'Set as Default Program' for CPK extensions (requires administrator privileges) [File -> Set Default Program]
-Added repack functionality* [File -> Save CPK]
*This is a bit different from usual repacking, instead ofExtract All -> Replace Files -> Repack CPKnow you canReplace Files (inside the tool) -> Repack CPKto replace files, right click on a file and click 'Mark for replace'then choose the new file to replace with.
Cpk File Pes
v5.0.2 :-
Now the tool can detect if a file is encrypted or not (when turning on Heroes Decrypt) to check if it needs decrypting.
v5.0.1 :-
a tiny update to fix some files not getting decrypted properly
v5.0 :-
added support for decrypting 'Super Dragon Ball Heroes : World Mission' Assets.
credits to ThievingSix for breaking the encryption
v4.1:-
after adding the 'ttl' folder to data1.cpk some files get extracted incorrectly, this update fixes that
v4.0:-
add an extensive search option : Search Options-> Full Search
will search all the cpk for desired file
V3.2:-
Extract Cpk File
fixed more bugs..
V3.1:-
fixed an issue with Dokkan assests
v3 :-
fixed some bugs
Decrypt Cpk File Folders
added a 'back' button