import java.security.*;
public class MyKeyGenerator {
    private KeyPairGenerator keyGen;
    private KeyPair pair;
    private PrivateKey privateKey;
    private PublicKey publicKey;
    private Context context;
    public MyKeyGenerator(Context context, int length)throws Exception{
        this.context =context;
        this.keyGen = KeyPairGenerator.getInstance("RSA");
        this.keyGen.initialize(length);
    }
    public void createKeys(){
        this.pair = this.keyGen.generateKeyPair();
        this.privateKey = pair.getPrivate();
        this.publicKey = pair.getPublic();
    }
    public PrivateKey getPrivateKey(){
        return this.privateKey;
    }
    public PublicKey getPublicKey(){
        return this.publicKey;
    }
    public  String getPrivateKeyStr(){
        byte b [] = this.getPrivateKey().getEncoded();
          return new String(b));
    }
    public  String getPublicKeyStr(){
        byte b [] = this.getPublicKey().getEncoded();
        return new String(b));
    }
}
Hello, I have searched SO for how to convert or get the String representation of a public key or private key, most of the answers were very old and only for how to convert String pubKey ="...."; into a key. I tried to generate the keys and get the encoded bytes and I tried to convert the byte to String as my code shows above, but I am not sure if I am doing it in the right way by simply converting the encoded bytes into a String.
 
    