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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With