Public key encryption help

Hi all,

I'll try to explain this as best as I can. I need to design an encryption application which deals with the following:

1. Encryption to be done using a public key at the front end of the application.

2. Decryption to be done using a private key at the back end of the application, but:

3. The private key needs to be secured in some way and not available in a file or similar; probably by requiring the admin user to submit a password in order for the key to be generated.

I have generated a public and private key pair using the following code:

KeyPairGenerator kpg =null;

kpg = KeyPairGenerator.getInstance("RSA","BC");

SecureRandom srandom =new SecureRandom();

kpg.initialize(1024, srandom);

KeyPair kp = kpg.generateKeyPair();

PrivateKey priKey = kp.getPrivate();

PublicKey pubKey = kp.getPublic();

But now that I've done that I have no idea how to achieve the important bits:

1. Encryption with my predefined public key.

2. Decryption with my predefined private key, in conjunction with a user-submitted password.

Help? :)

[1258 byte] By [jbbnza] at [2007-11-27 10:46:26]
# 1

javax.crypto.Cipher

For a keystore with a password see java.security.KeyStore.

ejpa at 2007-7-28 20:18:54 > top of Java-index,Security,Cryptography...
# 2

I should probably add that this is my first Java application, so anything in-depth would be hugely appreciated. :D

jbbnza at 2007-7-28 20:18:55 > top of Java-index,Security,Cryptography...
# 3

Blimey you picked a tough one. If you are dealing in streams the easiest thing is to use javax.crypto.CipherInputStream and CipherOutputStream. In any case you just initialize the Cipher object with the appropriate keys and modes and away you go ... ;-)

ejpa at 2007-7-28 20:18:55 > top of Java-index,Security,Cryptography...
# 4

It may or may not be worth mentioning that any functions are being called and having their returned values parsed by PHP, by the way.

I envision it like this:

/*** PHP ***/

// load Java class

$crypt = new Java('Crypt');

// encrypt a string

$crypt->Encrypt($someString);

// decrypt a string

$crypt->Decrypt($someString, $password);

/*** JAVA ***/

// encryption method declaration

public String Encrypt(String plaintext);

// decryption method declaration

public String Decrypt(String ciphertext, String password);

(Whether the methods would actually return Strings I have no idea.)

jbbnza at 2007-7-28 20:18:55 > top of Java-index,Security,Cryptography...
# 5

No they wouldn't. Strings are for characters. Ciphertext is binary. You can't put that into a String and get it back again intact. Use byte[].

ejpa at 2007-7-28 20:18:55 > top of Java-index,Security,Cryptography...
# 6

/me resumes work on this project

I've created a keystore with keytool, but when I try to initialise a KeyStore in my java application I get this:

Code:

KeyStore keyStore = KeyStore.getInstance("JCEKS");

Error:

java.lang.Exception: CreateInstance failed: new Xcryption. Cause: java.security.KeyStoreException: JCEKS

Any ideas? :)

Message was edited by:

jbbnz

jbbnza at 2007-7-28 20:18:55 > top of Java-index,Security,Cryptography...
# 7

JKS

ejpa at 2007-7-28 20:18:55 > top of Java-index,Security,Cryptography...
# 8

OK turns out this was a Tomcat problem; I needed to get sunjce_provider.jar into my Tomcat path.

I'm getting a different error now, however.

NoClassDefFoundError: com.sun.crypto.provider.SunJCE_aa. -- Unable to call constructor

This is occurring on the getKey() line of this code:

KeyStore keyStore = KeyStore.getInstance("JCEKS", new com.sun.crypto.provider.SunJCE());

FileInputStream fis = new FileInputStream("/path/to/file.jceks");

keyStore.load(fis, "mypassword".toCharArray());

Key public_key = keyStore.getKey("mykeystorealias", "mypassword".toCharArray());

This seems odd because the SunJCE_aa class appears to be part of that .jar file...

jbbnza at 2007-7-28 20:18:55 > top of Java-index,Security,Cryptography...
# 9

Same thing if I copy the SunJCE_aa.class file to my Tomcat path; so I can only assume that it cannot *create* the object rather than it cannot *find* it.

I have no idea why, however. :(

jbbnza at 2007-7-28 20:18:55 > top of Java-index,Security,Cryptography...
# 10

OK, abandoning this. I've figured something out with OpenSSL and PHP instead.

Far easier. :)

jbbnza at 2007-7-28 20:18:55 > top of Java-index,Security,Cryptography...