如何将私钥作为 Java 中的字符串传递,以便使用 SSHJ 通过 SFTP 进行连接?
How to pass the private key as a String in Java in order to connect via SFTP using SSHJ?
以下代码可以通过 SFTP 连接,使用密钥的 目录路径 来加载它。
这是当前代码(我指定了库以防万一)
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.userauth.keyprovider.KeyProvider;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
SSHClient sftp = new SSHClient();
KeyProvider privateKey = sftp.loadKeys("/home/sample/.ssh/id_rsa");
sftp.addHostKeyVerifier(new PromiscuousVerifier());
sftp.connect("111.222.333.444");
sftp.authPublickey("sample", privateKey);
有没有办法将私钥的内容作为字符串而不是使用其目录路径传递?
是的,您可以调用库的以下方法来支持use-case。
/**
* Creates a {@link KeyProvider} instance from passed strings. Currently only PKCS8 format private key files are
* supported (OpenSSH uses this format).
* <p/>
*
* @param privateKey the private key as a string
* @param publicKey the public key as a string if it's not included with the private key
* @param passwordFinder the {@link PasswordFinder} that can supply the passphrase for decryption (may be {@code
* null} in case keyfile is not encrypted)
*
* @return the key provider ready for use in authentication
*
* @throws SSHException if there was no suitable key provider available for the file format; typically because
* BouncyCastle is not in the classpath
* @throws IOException if the key file format is not known, etc.
*/
public KeyProvider loadKeys(String privateKey, String publicKey, PasswordFinder passwordFinder) throws IOException
如果您正在使用的库版本不提供,请考虑升级它。
<dependency>
<groupId>net.schmizz</groupId>
<artifactId>sshj</artifactId>
<version>0.10.0</version>
</dependency>
以下代码可以通过 SFTP 连接,使用密钥的 目录路径 来加载它。
这是当前代码(我指定了库以防万一)
import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.userauth.keyprovider.KeyProvider;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
SSHClient sftp = new SSHClient();
KeyProvider privateKey = sftp.loadKeys("/home/sample/.ssh/id_rsa");
sftp.addHostKeyVerifier(new PromiscuousVerifier());
sftp.connect("111.222.333.444");
sftp.authPublickey("sample", privateKey);
有没有办法将私钥的内容作为字符串而不是使用其目录路径传递?
是的,您可以调用库的以下方法来支持use-case。
/**
* Creates a {@link KeyProvider} instance from passed strings. Currently only PKCS8 format private key files are
* supported (OpenSSH uses this format).
* <p/>
*
* @param privateKey the private key as a string
* @param publicKey the public key as a string if it's not included with the private key
* @param passwordFinder the {@link PasswordFinder} that can supply the passphrase for decryption (may be {@code
* null} in case keyfile is not encrypted)
*
* @return the key provider ready for use in authentication
*
* @throws SSHException if there was no suitable key provider available for the file format; typically because
* BouncyCastle is not in the classpath
* @throws IOException if the key file format is not known, etc.
*/
public KeyProvider loadKeys(String privateKey, String publicKey, PasswordFinder passwordFinder) throws IOException
如果您正在使用的库版本不提供,请考虑升级它。
<dependency>
<groupId>net.schmizz</groupId>
<artifactId>sshj</artifactId>
<version>0.10.0</version>
</dependency>