Android KeyPairGenerator always generates the same key pair
Asked Answered
L

1

9

I am making an application which generates a key pair for a user. But in every device the keys are identical. Here is my code:

public KeyPair generateKeys() {
    KeyPair keyPair = null;
    try {
        // get instance of rsa cipher
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(1024);            // initialize key generator
        keyPair = keyGen.generateKeyPair(); // generate pair of keys
    } catch(GeneralSecurityException e) {
        System.out.println(e); 
    }
    return keyPair;
}

And to show the generated keys code is:

KeyPair keyPair = rsa.generateKeys();

byte[] publicKey = keyPair.getPublic().getEncoded();
byte[] privateKey = keyPair.getPrivate().getEncoded();
privateText.setText( Base64.encodeToString(privateKey, Base64.NO_WRAP) );
publicText.setText( Base64.encodeToString(publicKey, Base64.NO_WRAP) );

The key generation is called only one time for each android device, and for that reason the keys in each device should be different.. Can anyone tell me what i am missing here?

Lacilacie answered 22/6, 2014 at 15:49 Comment(1)
this is happening for me too! any solution?Rampage
R
21

I believe you are only looking at the first few or last few bits. I thought I had the same problem too but when I looked at the bits in the middle, they were indeed different!

Rampage answered 5/8, 2014 at 3:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.