简体   繁体   中英

Java SFTP client that takes private key as a string

Apache Commons and JSch both require a private key file to set up an SFTP connection. The project I'm working on will be used to connect to multiple SFTP servers. Therefore, we do not hope to deploy multiple private key files, but rather keep these keys as strings in an encrypted config file. Is there an SFTP library that doesn't require a file object for private key?

The JSch has an addIdentity method overload that takes the key from a buffer:

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

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.


Alternatives:

There is also an addIdentity overload that takes an Identity interface :

public class JSch {
    ...
    public void addIdentity(Identity identity, byte[] passphrase)

Just implement the interface to get the private key from wherever you need.

See IdentityFile for an example implementation.


Alternatively, store all your keys to IdentityRepository .

public interface IdentityRepository {
    ...
    public boolean add(byte[] identity);
public class JSch {
    ...
    public synchronized IdentityRepository getIdentityRepository()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM