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?
source share