I'm writing an app which is supposed to access my private server via SSH using JSch. Since I have set up public key authentication I want this app to authenticate the same way. I will be the only one using this app so I want to store my key either directly inside the app (e.g. hard-coded) or separated somewhere inside the home directory of my phone. Which would be the best way to store it, maybe as a resource file inside the project? Since I'm pretty new to Android development I'm not sure about what's the best way for this.
What I've tried:
// [...]
String user = "my_user";
String ssh_pwd = "my_pwd";
String host = "my_host";
// stored as OpenSSH key - file not found error - where shoud I move this file?
String private_key = "./my_pk";
int port = 22;
// basic SSH connection stuff
JSch jsch = new JSch();
session = jsch.getSession(user, host, port);
jsch.addIdentity(private_key, ssh_pwd.getBytes());
Another way:
// [...]
// private key in OpenSSH format as a plain string
String private_key = "xyz123abc456def789ghi012...";
// public key in OpenSSH format as a plain string
String public_key = "a1b2c3d4e5...";
// [...]
jsch.addIdentity("id_rsa", private_key.getBytes(), public_key.getBytes(), ssh_pwd.getBytes());
The latter resulted in an "invalid privatekey" error. Besides that, I'm not sure which of both is the more secure way to work with.
Thanks in advance.