如何在sftp生产者中读取加密的私钥



我正试图将一个文件上传到持有公钥的sftp。我在我的项目中有一个私钥,我通过ssh-keygen生成了一个密码短语集。我试过这个:

public void configure() throws Exception {
var connection =
sftpProperties
.getSftpForEntity("target")
.orElseThrow(SftpProperties.noSuchEntity("target"));

var privateKey = ArrayUtils.toObject(Files.readAllBytes(Path.of("src/main/resources/id_rsa")));

from(
file("test")
)
.to(
sftp(connection.getConnectionString("/hello"))
.username(connection.getUser())
.privateKeyPassphrase("pass")
.privateKey(privateKey)
);
}
}
org.apache.camel.component.file.GenericFileOperationFailedException: Cannot connect to sftp://arcapptest@us2.hostedftp.com:22
...
Caused by: com.jcraft.jsch.JSchException: invalid privatekey: [B@71068da

似乎没有应用密码短语。我也试过这个:

.to(
sftp(floridaConnection.getConnectionString("/hello"))
.username(floridaConnection.getUser())
.privateKeyPassphrase("pass")
.privateKeyFile("src/main/resources/id_rsa") 
);

结果相同。

我试过使用未加密的私钥,这很有效。

问题在于私钥的加密。Camel使用JSch来处理密钥,并且它需要是PEM。我用这个转换了密钥:ssh-keygen -p -f /path/to/key -m pem。之后,可以使用密码短语来读取密钥。

最新更新