Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android KeyPairGenerator always generates the same key pair

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?

like image 440
M.Veli Avatar asked Jun 22 '14 15:06

M.Veli


1 Answers

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!

like image 104
necromancer Avatar answered Oct 04 '22 18:10

necromancer