code: public static KeyPair generateKeys(int numBits) throws NoSuchAlgorithmException {
          int numBitsPow2 = 1;
          while (numBitsPow2 < numBits) {
              numBitsPow2 <<= 1;
          }
  
          KeyPairGenerator kpg = KeyPairGenerator.getInstance(CIPHER_ALGORITHM);
          kpg.initialize(numBitsPow2, SECURE_RNG);
          KeyPair keyPair = kpg.generateKeyPair();
          return keyPair;
      }
docs: Generate a random RSA keypair.
  
  @param numBits
  key's length in bits, should be power of 2)
  @return
  @throws NoSuchAlgorithmException