Android KeyPairGenerator always generates the same key pair

I am creating an application that generates a key pair for a user. But in each 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 display the generated key code:

 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) ); 

Key generation is called only once for each Android device, and for this reason, the keys on each device must be different. Can someone tell me that I'm not here?

+6
source share
1 answer

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

+10
source

Source: https://habr.com/ru/post/971179/


All Articles