KeyPairGenerator in jdk1.5 differs from jdk1.4

I have been generating RSA keys in jdk1.4 for quite some time. Instead of storing the keys I have used the fact that the same key is generated each time as long as the input is the same. However, when doing this with jdk1.5 the results differ from what was produced in jdk1.4.

The following test case illustrates my problem:

import java.math.BigInteger;

import java.security.KeyPair;

import java.security.KeyPairGenerator;

import java.security.SecureRandom;

import java.security.interfaces.RSAPrivateKey;

import java.security.interfaces.RSAPublicKey;

import junit.framework.TestCase;

publicclass TestKeyPairGeneratorextends TestCase{

publicvoid testKeyPairGenerator()throws Exception{

SecureRandom sr =new SecureRandom(newbyte[]{1,2,3,4,5});

KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA");

gen.initialize(512, sr);

KeyPair keyPair = gen.generateKeyPair();

BigInteger priv = ((RSAPrivateKey)keyPair.getPrivate()).getPrivateExponent();

BigInteger mod = ((RSAPrivateKey)keyPair.getPrivate()).getModulus();

BigInteger pub = ((RSAPublicKey) keyPair.getPublic()).getPublicExponent();

if ("1.4".equals(System.getProperty("java.specification.version"))){

assertEquals("Public exponent differs", pub.toString(),"65537");

assertEquals("Modulus differs", mod.toString(),"10168453279863611160684257564658874979068032194153364974263061470417211558176431777315758266813304475505635474224640023491056357535644661964115218380088441");

assertEquals("Private exponent differs", priv.toString(),"3404586269131135384575044078021723509859923866461804593287064678660373120848696022530176891976025148660340997240242411014550229151222400914349583686738481");

}else{

assertEquals("Public exponent differs", pub.toString(),"65537");

assertEquals("Modulus differs", mod.toString(),"7548301025769813426531900568079274012258970653013576047079677969183673491810801374387356423765688204945000059232699002575691694838210921897180095000416309");

assertEquals("Private exponent differs", priv.toString(),"3535793417307894813328391832697035764565636817017314968175214519675444291394089637955390126352392044807601556393533178419076862429856371343196070121492705");

}

}

}

Anyone knows why the result is not the same in jdk1.5 as in jdk1.4?

thanks,

Tomas

[3675 byte] By [tompalabompaa] at [2007-10-3 3:48:49]
# 1
> I have been generating RSA keys in jdk1.4 for quite> some time. Instead of storing the keys I have used> the fact that the same key is generated each time as> long as the input is the same. Is this a documented feature or just an observed feature?
sabre150a at 2007-7-14 21:45:52 > top of Java-index,Security,Cryptography...
# 2

> > I have been generating RSA keys in jdk1.4 for

> quite

> > some time. Instead of storing the keys I have used

> > the fact that the same key is generated each time

> as

> > long as the input is the same.

>

> Is this a documented feature or just an observed

> feature?

Well, it is rather an assumption. Since I have to supply the "randomness" in the form of a SecureRandom instance I assume that if I supply an identical randomness I get an identical KeyPair.

This assumption holds true for both jdk1.4 and jdk1.5. The problem is that I don't get identical results from jdk1.4 and jdk1.5 if I supply the same input.

Both 1.4 and 1.5 specify that they are using the key generation algorithm described in PKCS#1.

/tomas

tompalabompaa at 2007-7-14 21:45:52 > top of Java-index,Security,Cryptography...
# 3

> Well, it is rather an assumption. Since I have to

> supply the "randomness" in the form of a SecureRandom

> instance I assume that if I supply an identical

> randomness I get an identical KeyPair.

I would not make that assumption. Looking at the API for SecureRandom it does not even say that given a particular seed one gets the same sequence of 'random' numbers.

>

> This assumption holds true for both jdk1.4 and

> jdk1.5. The problem is that I don't get identical

> results from jdk1.4 and jdk1.5 if I supply the same

> input.

I see nothing in the API to say that the SecureRandom results should be the same for two different Java versions.

>

> Both 1.4 and 1.5 specify that they are using the key

> generation algorithm described in PKCS#1.

>

PKCS#1 says nothing about random sequence repeatability in it's use of random numbers.

I suspect that you will have to change your appproach but since I don't kow why you want to generate keys this way I cannot suggest how.

sabre150a at 2007-7-14 21:45:52 > top of Java-index,Security,Cryptography...
# 4

I think I know what the OP is trying to do; one technique for creating portable RSA keys is to simply regenerate them every time they are needed; no key files are stored anywhere. As an example, the user can enter a password which is then (perhaps after hashing it with SHA1) used to seed a psuedo-random generator. This PRNG is then supplied to the RSA key generation procedure which should generate the same RSA key.

As you have pointed out, this only works if the API will guarantee such behavior: in this case it is 2 APIs, the SecureRandom API and the KeyPairGenerator API. Neither does. Futhermore, the OP is not even specifying a PRNG algorithm; he is therefore getting the default PRNG algorithm. I would certainly not expect this to remain unchanged even across minor java versions, let alone 1.4 to 1.5

ghstarka at 2007-7-14 21:45:52 > top of Java-index,Security,Cryptography...
# 5
Thanks for your replies.Anyone knows if it is possible to get the same behavior from KeyPairGenerator in 1.5 as it was in 1.4 by doing any configuration changes? Or by specifying another algorithm or provider?
tompalabompaa at 2007-7-14 21:45:52 > top of Java-index,Security,Cryptography...