Код: Выделить всё
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.addPasswordIdentity(this.password);
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
Код: Выделить всё
Exception: org.apache.sshd.common.SshException: No more authentication methods available
Изменить: я пытался предоставить KeyPairProvider, но безуспешно..
Код: Выделить всё
try (SshClient client = SshClient.setUpDefaultClient()) {
try {
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();
final KeyPair keyPair = new KeyPair(publicKey, privateKey);
KeyPairProvider keyPairProvider = new KeyPairProvider() {
@Override
public Iterable loadKeys(SessionContext session) {
return Collections.singletonList(keyPair);
}
};
client.setKeyIdentityProvider(keyPairProvider);
Код: Выделить всё
KeyProperties.PURPOSE_SIGN | KeyProperties.PURPOSE_VERIFY | KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT
Код: Выделить всё
sk-ecdsa-sha2-nistp256@openssh.com
ecdsa-sha2-nistp256
ecdsa-sha2-nistp384
ecdsa-sha2-nistp521
sk-ssh-ed25519@openssh.com
ssh-ed25519
ssh-dss
ssh-rsa
Подробнее здесь: https://stackoverflow.com/questions/784 ... cate-my-ss