开发者

ssh example of private/public key authentication [duplicate]

This question already has answers here: SSHJ Example of Public Key Auth from File (2 answers) Closed 5 years ago.

Can anyone give me an example of private/public key authentication in sshj?

In sshj what's the command line equivalent of,

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

I tried (error handling omitted),

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

but in the log statements I see,

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: Exh开发者_StackOverflowausted available authentication methods

Thanks, Everett


Try using KeyPairWrapper like this:

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

With the BouncyCastle provider you can use something like this to extract a KeyPair from a PKCS8 PEM (apologies for the messy code)

/**
 * 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");
}


You need to include BouncyCastle lib for most key types. Here's the Maven dependency: org.bouncycastle bcprov-jdk16 1.46

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜