简体   繁体   English

Java - 使用公钥和密码进行身份验证 - j2ssh

[英]Java - Authentication with public key and passphrase - j2ssh

How do I connect to server with public key and passphrase (I have the public key and I know the passphrase. I'm using com.sshtools.j2ssh library but I can't see an option to set passphrase. The problem is, even though I have the public key, the server keep asking for passphrase.如何使用公钥和密码连接到服务器(我有公钥,我知道密码。我正在使用 com.sshtools.j2ssh 库,但我看不到设置密码的选项。问题是,即使虽然我有公钥,但服务器一直要求输入密码。

Thanks.谢谢。 Ron.罗恩。

When you instantiate the SshPrivateKeyFile you can then call toPrivateKey on that object to create a SshPrivateKey object.当您实例化 SshPrivateKeyFile 时,您可以在该 object 上调用 toPrivateKey 以创建 SshPrivateKey object。 That toPrivateKey method takes a string that is your passphrase.该 toPrivateKey 方法采用一个字符串,即您的密码。 If you do not have a passphrase, pass an empty string (or a null I believe works also).如果您没有密码,请传递一个空字符串(或 null 我相信也可以)。 Here is a sample.这是一个示例。

PublicKeyAuthenticationClient sshClient = new PublicKeyAuthenticationClient();

SshPrivateKeyFile sshPrivKeyFile = SshPrivateKeyFile.parse(new File(keyFile));
SshPrivateKey sshPrivKey = sshPrivKeyFile.toPrivateKey("passphrase");
sshClient.setKey(sshPrivKey);

Check the API of j2ssh for decrypt/encrypt methods.检查 j2ssh 的 API 以了解解密/加密方法。 Usually those are used to send for authentication.通常这些用于发送身份验证。

Are you sure, that server is properly configured?您确定该服务器配置正确吗? If it's set to key auth no password should be needed except keystore password.如果它设置为 key auth,则除了 keystore 密码外,不需要密码。 Try to connect using putty or something like that.尝试使用腻子或类似的东西进行连接。 I also recommend you to try Ganymed SSH I have used it in couple of projects and there was no problems with that even using quite advanced features such tunnels, scp, etc.我还建议您尝试Ganymed SSH我已经在几个项目中使用过它,即使使用隧道、scp 等非常高级的功能也没有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM