Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

java gpg encryption

843811Mar 28 2006 — edited Mar 12 2009
Hi,
I want to encrypt and decrypt data using gpg in Java.
I don't want to call the gpg executable and run it.[ Using System.getRuntim
e()..)

When I run gpg tool directly and encrypt data kept in a file, the ascii outp
ut reads as follows:

-----BEGIN PGP MESSAGE-----
Version: GnuPG v1.2.5 (MingW32)

hQEOAxnWq33Bpts7EAQAgEWV8dp/HQfx7EPBtK9c8mkUGO8KWYnbv5ZYOLK7+eto
TgULaYbboJd26IBwcoLl2mi9R76TWl78g2rlKkkY
jK12NhiRQh5YVxfAmlCOGlGp
aYNeCw4sp7Aj/LZ9eWN1L2/6P+pXKk0yPNje4OyEg/ZxXd2bGlM1fxWCSjp51wkD
/ 1ORs84erTS5dQhmE4AvYggN303Za2tDVOZe0zQ66
K1F9GO8ver5/hKwkUqHQvGq
wtp0QI32LHlyzYlP58WiGqD7Iq8WJKNiR+/NwJOcD0+T7aQWEqv32zuaik/1S3ZQ
plarVyY3+2L9zl8GR4L9K5wGtZQh/t9l8qeVZJbQi8qO0lEBQ3Jx3fiZQxWMxrCu
E4DSG07h0wn9c5B7QW55+j0emgt4yCyZ5AZ7BImR
FeaGksXQqJKgk/RuDr0iH1IU
gBpADqtBL/lzNt3ZKldJs1ZAejQ=
=JujT
-----END PGP MESSAGE-----


But I want to get the same output when I encrypt through Java.I don't want t
o encrypt a text file. I need to encrypt data passed by arguments.

I have attached the code I am using[ only encrypt function mentioned here]
I am base64 encoding the encrypted byte array. But I am getting a different
output to what I get if I run the gpg tool directly.

How do I get the same result?



import java.io.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.rmi.RemoteException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Set;

import java.util.Map;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPrivateKey;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;

public class GTest
{
public static void main(String[] args) throws Exception{

GTest gTest = new GTest();
gTest.setUpCipher();
}

public void setUpCipher() throws NoSuchAlgorithmException,
InvalidKeyException, IllegalBlockSizeException, NoSuchProviderException,
BadPaddingException, NoSuchPaddingException {

System.out.println("####1.inside setUpCipher###########");


// The keyring that holds the public key
String publicKeyFilePath = "d://codearea//gTest//pubring.gpg";

// The keyring that holds the secret key
String secretKeyFilePath = "d://codearea//gTest//secring.gpg";

// init the security provider
Security.addProvider(new BouncyCastleProvider());

try {

// Read the public key
FileInputStream in = new FileInputStream(publicKeyFilePath);
PGPPublicKey key = readPublicKey(in);
PublicKey pubKey = key.getKey("BC");

Cipher ciper = Cipher.getInstance("RSA");


// Encrypt data
byte[] encData = encryptData(ciper, pubKey, new String("371218702868729").ge
tBytes() );
String cryptString = new sun.misc.BASE64Encoder().encode(enc);

System.out.println("\n\n\nEncrypted data="+ cryptString);


} catch (Exception e) {
System.out.println("Exception in UtilService::setUpCipher");
System.out.println(e.toString());
}
}


public byte[] encryptData(Cipher cipher, PublicKey pubKey, byte[] clearText)

{
byte[] encryptedBytes = null;
try {
cipher.init(Cipher.ENCRYPT_MODE,pubKey);
encryptedBytes = cipher.doFinal(clearText);


} catch (InvalidKeyException e) {
e.printStackTrace();
}catch (IllegalBlockSizeException e) {
e.printStackTrace();
}catch (BadPaddingException e) {
e.printStackTrace();
}
return encryptedBytes;
}

public byte[] decryptData(Cipher cipher, PrivateKey privKey, byte[] encryp
tedText)
{
byte[] decryptedBytes = null;
try {
cipher.init(Cipher.DECRYPT_MODE,privKey);
decryptedBytes = cipher.doFinal(encryptedText);


} catch (InvalidKeyException e) {
e.printStackTrace();
}catch (IllegalBlockSizeException e) {
e.printStackTrace();
}catch (BadPaddingException e) {
e.printStackTrace();
}
return decryptedBytes;
}


private static PGPPublicKey readPublicKey(InputStream in)
throws IOException {
try {
PGPPublicKeyRing pgpPub = new PGPPublicKeyRing(in);
return pgpPub.getPublicKey();
} catch (IOException io) {
System.out.println("readPublicKey() threw an IOException");
System.out.println(io.toString());
throw io;
}

}

private static PGPSecretKey readSecretKey(InputStream in)
throws IOException, PGPException {
try {

PGPSecretKeyRing pgpSec = new PGPSecretKeyRing(in);
return pgpSec.getSecretKey();
} catch (IOException io) {
System.out.println("readPublicKey() threw an IOException");
System.out.println(io.toString());
throw io;
}

}

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 9 2009
Added on Mar 28 2006
3 comments
4,919 views