Java SFTP client that takes private key as a strin

2019-06-05 19:36发布

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?

1条回答
姐就是有狂的资本
2楼-- · 2019-06-05 20:03

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{

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()
查看更多
登录 后发表回答