Generate a RSA Private/Public Key pair for SSH login on Android
<p>I am working on a project where I need my app to generate a public/private RSA key for SSH login. </p>

<p>I have the following code so far to get the keys:</p>

<pre><code>private void createKeyTest()
    {
        try
        {
            KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
            kpg.initialize(2048);
            KeyPair keyPair = kpg.genKeyPair();
            byte[] pri = keyPair.getPrivate().getEncoded();
            byte[] pub = keyPair.getPublic().getEncoded();

            String privateKey = new String(pri);
            String publicKey = new String(pub);

            Log.d("SSHKeyManager", "Private Key: " + privateKey);
            Log.d("SSHLeyManager", "Public Key: " + publicKey);
        }
        catch (NoSuchAlgorithmException ex)
        {
            Log.e("SSHKeyManager", ex.toString());
        }
</code></pre>

<p>When I print this out in Logcat I get random non textual characters when I am expecting the key to look something like:</p>

<pre><code>-----BEGIN RSA PRIVATE KEY-----
MIIEoQIBAAKCAQEAm8QDThbuEjAbQxbsdDltL2xdFkQOep3L0wseSJAxmDuvH6yB
9I2fEEmF+dcVoNo2DGCDZMw7EgdFsfQZNF+PzKdZwtvSUTDW/TmMHWux2wYimNU3
jhQ3kfxGmiLgMJHQHWLkESwd06rCr7s1yOnPObdPjTybt7Odbp9bu+E59U10Ri3W
JFxIhi9uYQvpRn4LT/VIfH/KBdglpbD9xBAneVbKFXW7....
-----END RSA PRIVATE KEY-----
</code></pre>