JSch to add private key from a string
Asked Answered
E

2

12

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.

Eiland answered 10/11, 2015 at 18:58 Comment(0)
J
14

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.

Jealous answered 11/11, 2015 at 7:12 Comment(0)
A
1

You can simply do the following

String privateKeyValue = "You private Key String";
jsch.addIdentity(null, privateKeyValue.getBytes(), null, null);
Avestan answered 18/1, 2024 at 4:43 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.