This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes PGP encrypting using .asc public key file - encrypt String Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "PGP encrypting using .asc public key file - encrypt String" Watch "PGP encrypting using .asc public key file - encrypt String" New topic
Author

PGP encrypting using .asc public key file - encrypt String

Sean Chang
Greenhorn

Joined: Feb 12, 2009
Posts: 12
Hi,

I am trying to PGP encrypt a String object, I base 64 encode it and send it to our client. The person that is trying to decrypt it keep getting a "Secret key for message not found” error. I copy pasted the code below as well as output: Any advice would be appreciated.



The public key looks something like this (I cut off a bunch of it):

-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.4.9 (MingW32)

mQGiBEl4qC8RBADL2l4bRHhIuZpnCwdro4K3cLGDaKYZ7/FTSvBWAweWu83wOGWE
f6BtOT4ZuO5Au6VSMjFcG6hakdKogYM74OKMxVDSxZ+dBcDXejy1s1x6z1M0RLDD
5lRw1orOQ56L/QkHIoPWXgecrLUo0TtwnzPH1oOlVR7/UZenU0EMZKieywCgoTjZ
1Y+ss9WshZhGUDk4ImkOE4MEAKDZUgtEEMOjf/i7rev1bHlF1iaqWpYDwYnnG6vU

-----END PGP PUBLIC KEY BLOCK-----

Sean Chang
Greenhorn

Joined: Feb 12, 2009
Posts: 12
Here is also a log of the output:

Creating a temp file...
Temp file created at
C:\Users\me\AppData\Local\Temp\pgp2862285773788966456.tmp
Reading the temp file to make sure that the bits were written
--------------
testthisnewstring

Key Strength = 2048
Algorithm = 16
Bit strength = 2048
Version = 4
Encryption key = true, Master key = false
Key Count = 0
encrypted text length before=0
creating comData...
comData created...
using PGPEncryptedDataGenerator...
used PGPEncryptedDataGenerator...
wrote bOut to byte array...
encrypted text length=596
Encrypted: [B@c1f10e
Temp:

hQIOAwCHtLoSzFboEAf/aEwFfAG/6rWtTzeY51jKKXYxAnhOmIMdQ4QnaINM3N7EyGAB8bKGV9SV
6jZ+RyGMRWVC18GfQAAQEfDHgqbLilnfotcn2nC0v32Scjd/zx6ethHqdNiN5qhVWV04O0KlH/fQ
9qb3XYzIUN2LzQySRUWXXbfti3/xKc/j2zCUb+tgA8oxWRF84q4DrNd19CRzvOhjUqs5vPwL3soc
9l4NJmMLUen+ikohhD9IqVp6vaixq2cqVRtjG1wAzdLwJ9Vj5GUju5P9i7Nf5rYzI5SEYbcLVzDJ
I+DgazWxwX0JQIy/ChMM8bRnChfwPfChHkwWzHbCa8K525scr17QuUAU+Qf/b7JAgFc2pAC/M/wM
tycjZ72usqARsBQ0vm4b7TEjIXvRf+9iuhy/yGPTWoynNZEmGcmK8OvM4XHPxqqez6jBV7U30C6r
hA2Upax7fUXsmkL1lCwsZzaNdH+4t+kVFoXDBCwhJqtcerZtw/buQQMUT1v63VJvIGxJ/8EmHw92
Adt907tTuvjRliNPuGOAVLQEm9lqVI161IUmmERZkZNTmKzzq0ExGWF/mCtBGBDwMjYm1dHmClIA
5ikox76xVuG/JwHpdjXbM5bGcnBDQAvGeFN7xRkHM1SnyUJcu6IQhQ3eTmfoXILAfrS4CLec+ZsT
qoQUmcQvwusIOdvCH+KhlMlBdPFl8F/fEieNFf0rRzrsBkNpEdcHZTseCJ5xSQGHggLEJFms+k85
pigdb9e0u+xcQAXHMKijx4CicY8ooZp9bwA=
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

In method _encrypt() you should close 'cOut' and not 'out' so the final out.close() should be changed to cOut.close(). I don't know if this is your only problem since I no longer have PGP available on Windows and use GPG on Linux.

By using PipedInputStream and PipedOutputStream you can avoid creating the intermediate file. I find the BouncyCastle PGP API very cumbersome to work with; I don't think it has been well thought out.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4646
    
    5

I haven't looked carefully at your code, but are you sure you are handling the data properly? Crypto code uses arrays of bytes, not String objects.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Pat Farrell wrote:Crypto code uses arrays of bytes, not String objects.


I cannot find that the OP has used String rather than bytes. In several places he is writing data as String but this just seems to be for diagnostic purposes. The obvious problem I see is that he is closing the wrong stream so part of the ciphertext is not getting written. I can see another potential problem where he does not close one diagnostic input stream and this may stop the associated file from being re-opened later.

The first thing for the OP to do is to close the correct stream. If he reports back that he has the same problem then I will look again.
Sean Chang
Greenhorn

Joined: Feb 12, 2009
Posts: 12
James Sabre wrote:In method _encrypt() you should close 'cOut' and not 'out' so the final out.close() should be changed to cOut.close(). I don't know if this is your only problem since I no longer have PGP available on Windows and use GPG on Linux.


I tried this but it didn't work but results are the same.

One thing I noticed is that when I pass it this key file, in the log I have it says "Key Count = 0", so it looks like it doesn't detect the key in that file properly. It does say encryptionkey=true, but I don't know why it is saying key count = 0.


Please advise, thanks.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Sean Chang wrote:
James Sabre wrote:In method _encrypt() you should close 'cOut' and not 'out' so the final out.close() should be changed to cOut.close(). I don't know if this is your only problem since I no longer have PGP available on Windows and use GPG on Linux.


I tried this but it didn't work but results are the same.


Using gpg on Ubuntu to decrypt, whether or not I make the change your code works for me BUT I still think you need to close 'cOut' and not 'out'. If you don't believe me then check the BouncyCastle examples.


One thing I noticed is that when I pass it this key file, in the log I have it says "Key Count = 0", so it looks like it doesn't detect the key in that file properly. It does say encryptionkey=true, but I don't know why it is saying key count = 0.

Using my key I get

Key Count = 1


Please advise, thanks.


Make sure you are using a valid public key. Make sure that your client is using the matching private key. Make sure it is an encryption key and not a signing key. Make sure you are not corrupting the ciphertext when shipping it to your client. If sending to your client using FTP then make sure you set to BINARY mode. Ship either the raw ciphertext or an armoured ciphertext; note that just Base64 encoding does not make it armoured.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: PGP encrypting using .asc public key file - encrypt String
 
Similar Threads
Encrypt a zip file using PGP in java
XML Encryption
WS Security Related
Is cast5 128 supported by default sun JCE provider?
how to get pubring.pkr in PGP encryption in java