Код: Выделить всё
protected String sshConnect() {
String stdoutString = null;
String alias = "KEYPAIRTEST";
KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
keyStore.load(null);
KeyStore.Entry entry = keyStore.getEntry(alias, null);
PrivateKey privateKey = ((KeyStore.PrivateKeyEntry) entry).getPrivateKey();
PublicKey publicKey = keyStore.getCertificate(alias).getPublicKey();
KeyPair keyPair = new KeyPair(publicKey, privateKey);
// create a client instance
try (SshClient client = SshClient.setUpDefaultClient()) {
client.setServerKeyVerifier(AcceptAllServerKeyVerifier.INSTANCE); // should be default
client.addPublicKeyIdentity(keyPair);
client.start();
// connection and authentication
try (ClientSession session = client.connect(username, host, port).verify(TimeUnit.SECONDS.toMillis(defaultTimeoutSeconds)).getSession()) {
session.auth().verify(TimeUnit.SECONDS.toMillis(defaultTimeoutSeconds));
// create a channel to communicate
ClientChannel channel = session.createExecChannel(this.command);
ByteArrayOutputStream stdoutStream = new ByteArrayOutputStream();
channel.setOut(stdoutStream);
// open channel
channel.open().verify(5, TimeUnit.SECONDS);
// close channel
channel.waitFor(EnumSet.of(ClientChannelEvent.CLOSED),
TimeUnit.SECONDS.toMillis(5));
// output after converting to string type
stdoutString = stdoutStream.toString();
} catch (Exception e) {
Log.d(TAG, "sshConnect(): client.connect(): Exception: " + e);
}
} catch (Exception e) {
Log.d(TAG, "sshConnect(): client.start(): Exception: " + e);
}
return stdoutString;
}
Код: Выделить всё
keyStore.getCertificate(alias).getPublicKey();
byte[] encodedPublicKey = publicKey.getEncoded();
String b64PublicKey = Base64.getEncoder().encodeToString(encodedPublicKey);
Код: Выделить всё
ssh-rsa MlsdfijANBgkqhkiG9w0BAQE..
Код: Выделить всё
rsa-sha2-256 MlsdfijANBgkqhkiG9w0BAQE..
Код: Выделить всё
KeyPairGenerator kpg = KeyPairGenerator.getInstance(
KeyProperties.KEY_ALGORITHM_RSA, "AndroidKeyStore");
kpg.initialize(new KeyGenParameterSpec.Builder(
alias,
KeyProperties.PURPOSE_SIGN | KeyProperties.PURPOSE_VERIFY)
.setDigests(KeyProperties.DIGEST_SHA256, KeyProperties.DIGEST_SHA512)
.setKeySize(2048)
.build());
kpg.generateKeyPair();
Код: Выделить всё
W 60 [AsyncTask #1] INFO org.apache.sshd.common.io.DefaultIoServiceFactoryFactory - No detected/configured IoServiceFactoryFactory using Nio2ServiceFactoryFactory
WARN org.apache.sshd.client.keyverifier.AcceptAllServerKeyVerifier - Server at /192.168.13.4:22 presented unverified EC key: SHA256:nNMe+ZsasdILlkGIZfcLwl41ZvVTzTaEOeA
Подробнее здесь: https://stackoverflow.com/questions/784 ... cate-my-ss