Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSch to add private key from a string

I have the contents of the key pair file for SFTP as a string. I need to use JSch to add the contents, but addIdentity only accepts a file path. Is there anyway I can do this?

I see that the KeyPair class has a method -

KeyPair load(JSch jsch, byte[] bytes, byte[] bytes1)

I'm not sure what this does.

like image 670
eechpeech Avatar asked Nov 10 '15 18:11

eechpeech


People also ask

Can we use JSch for SSH key based communication?

JSch is the Java implementation of SSH2 that allows us to connect to an SSH server and use port forwarding, X11 forwarding, and file transfer. Also, it is licensed under the BSD style license and provides us with an easy way to establish an SSH connection with Java.

What is JSch addIdentity?

addIdentity(String prvkey, String pubkey, byte[] passphrase) Adds an identity to be used for public-key authentication. protected void. addSession(Session session) Adds a session to our session pool.

What is setKnownHosts in JSch?

public class JSch extends Object. This class serves as a central configuration point, and as a factory for Session objects configured with these settings. Use getSession to start a new Session. Use one of the addIdentity methods for public-key authentication. Use setKnownHosts to enable checking of host keys.

What is JSch API?

JSch is a pure Java implementation of SSH2. JSch allows you to connect to an sshd server and use port forwarding, X11 forwarding, file transfer, etc., and you can integrate its functionality into your own Java programs.


1 Answers

There is an addIdentity overload that takes the key from a buffer:

public class JSch {
    ...
    public void addIdentity(String name, byte[]prvkey, byte[]pubkey, byte[] passphrase)

See also Java SFTP client that takes private key as a string.

For an example of implementation, see JSch: addIdentity from private key stored on hdfs.

See also Loading private key from string or resource in Java JSch in Android app for a format of the key in the buffers.

like image 165
Martin Prikryl Avatar answered Oct 09 '22 16:10

Martin Prikryl