【问题标题】:ssh example of private/public key authentication [duplicate]私钥/公钥身份验证的 ssh 示例 [重复]
【发布时间】:2011-04-10 20:17:09
【问题描述】:

谁能给我一个sshj中的私钥/公钥认证的例子吗?

在 sshj 中,命令行相当于什么,

ssh -i /path/to/mykey.private username@host

我试过了(省略了错误处理),

final SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.connect("host");
ssh.authPublickey("username", "/path/to/mykey.private");
final Session session = ssh.startSession();
...

但在我看到的日志语句中,

DEBUG net.schmizz.sshj.SSHClient - Attempting to load key from: /path/to/mykey.private
WARN  net.schmizz.sshj.SSHClient - Could not load keys due to: {}
net.schmizz.sshj.common.SSHException: No provider available forUnknown key file
    at net.schmizz.sshj.SSHClient.loadKeys(SSHClient.java:482) ~[sshj-0.3.0.jar:na]
...
Exception in thread "main" 10:49:55.943 [reader] DEBUG
net.schmizz.sshj.transport.Reader - Stopping
net.schmizz.sshj.userauth.UserAuthException: Exhausted available authentication methods

谢谢, 埃弗里特

【问题讨论】:

  • 我回答了一个类似的问题here,并举了一个使用他们提供的 .pem 文件连接到 AWS 实例的示例。这并不像我希望的那样直截了当。

标签: java ssh sshj


【解决方案1】:

尝试像这样使用KeyPairWrapper

KeyPair kp = ... // read keypair from file
ssh.authPublickey(user, new KeyPairWrapper(keypair));

使用 BouncyCastle 提供程序,您可以使用类似的方法从 PKCS8 PEM 中提取 KeyPair(为混乱的代码道歉)

/**
 * Takes a PEM-encoded PKCS8 key-containing InputStream and returns the KeyPair within. Only the first keypair is considered
 * 
 * @return
 * @throws IOException if the stream is not a valid PKCS8 wrapped keypair
 */
public static KeyPair readKeypair(final InputStream is, final char[] password) throws IOException {
    PasswordFinder passwordFinder = password != null ? new StaticPasswordFinder(password) : null;

    KeyPair kp = null;
    try {
        // read the stream as a PEM encoded
        try {

            final PEMReader pem = new PEMReader(new InputStreamReader(is), passwordFinder);
            try {
                // Skip over entries in the file which are not KeyPairs
                do {
                    final Object o = pem.readObject();

                    if (o == null)
                        break; // at end of file
                    else if (o instanceof KeyPair)
                        kp = (KeyPair) o;
                } while (kp == null);
            }
            finally {
                pem.close();
            }
        }
        catch (EncryptionException e) {
            throw new IOException("Error reading PEM stream: " + e.getMessage(), e);
        }
    }
    finally {
        is.close();
    }

    // Cast the return to a KeyPair (or, if there is no [valid] return, throw an exception)
    if (kp != null)
        return kp;
    else
        throw new IOException("Stream " + is + " did not contain a PKCS8 KeyPair");
}

【讨论】:

    【解决方案2】:

    您需要为大多数键类型包含 BouncyCastle 库。这是 Maven 依赖项: org.bouncycastle bcprov-jdk16 1.46

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-20
      • 2020-04-04
      • 2017-11-08
      • 2015-08-20
      • 1970-01-01
      • 2012-02-17
      • 2012-10-27
      相关资源
      最近更新 更多