There are a lot of examples on this, but none of them show a working example on how use the .ppk public/private file components in a string form converted to a byte[] and used with the JSch.addIdentity(String,byte[],byte[],byte[]) method. Can someone look at the following method and let me know where I'm going wrong. It doesn't like the strings converted to a byte[]. The strings are not in full for security reasons, but I think you'll get the point of what I'm trying accomplish with this. Unfortunately, I'm stuck using strings for this since I cannot use the .ppk file directly.
public void InitChannelSftp() throws JSchException {
JSch ssh = new JSch();
session_ = ssh.getSession(userName_, host_, port_);
session_.setConfig("StrictHostKeyChecking", "no");
String passPhrase= "trustNoOne";
String privateKey= "-----BEGIN RSA PRIVATE KEY-----\n" +
"hvbytegNktMU05bc3QlCrSs+YHkMAvZRkzcjaXDdcKDfSZyDqcFHfkup1N6CMDg4\n" +
"yZh0ou+7G8jDQtn29YoAO0tpEW4EGQiI6918iMk22012ytcsCnM80/hj1JbVa4St\n" +
"1StZKJtBYSu1bRZCpFPygditUGc9pZhDWfgDxLTzQdDMcmdvMb9AgpNKZAz8n0OR\n" +
"2tuGPAogbt8e48ad9H7IYuikhIqQU42DkrAqRPJDUZqP3gGdzwstuDPl1dwrRaCs\n" +
"vTynsEMDCjVjNyxRK9sbBO/BwFTaFRM2oN4BvCbfbQKyhRrXZ1gjNMv1P5GxchKj\n"+
"-----END RSA PRIVATE KEY-----\n";
String publicKey= "-----BEGIN SH2 PUBLIC KEY-----\n" +
"AAAAB3NzaC1yc2EAAAABJQAAAQEA6wXxLoqF70gkd2hURVlmq+iPCBPmkZ6IwCST\n" +
"nlxHY/Jc0izbn+6hyFUZmS53LN9CJyjjVvU+UAWapU2tTJKwO6aI8xcKmtBjdDeS\n" +
"GJgh6C+srGFlbNTXzV8sMmGEQirLyTOMwGviRFOhKn5/bUbqpw==\n" +
"-----BEGIN SH2 PUBLIC KEY-----\n"
ssh.addIdentity(
"Test Conn", privateKey.getBytes(StandardCharsets.US_ASCII),
publicKey.getBytes(StandardCharsets.US_ASCII), passPhrase.getBytes());
session_.connect();
channel_ = session_.openChannel("sftp");
channel_.connect();
ChannelSftp sftp_ = (ChannelSftp) channel_;
}
Error:
com.jcraft.jsch.JSchException: invalid privatekey: [B@25be7b63
Charset charset = StandardCharsets.UTF_8;
ssh.addIdentity(
UUID.randomUUID().toString(), privateKey.getBytes(charset),
publicKey.getBytes(charset), passPhrase.getBytes(charset));
The following private key format works:
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: DES-EDE3-CBC,322A25B18A52EDD1
Pwb2h8N...x8ijUQ==
-----END RSA PRIVATE KEY-----
A blank line at the end is not needed.
In PuTTY, this is achieved via Conversions > Export OpenSSH key.
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