我正在尝试建立到Unix服务器的连接,并使用SFTP放置/获取文件。我使用的是JCraft的Jsch 0.1.53和基于RSA令牌的身份验证。没有密码。
我使用IPSwitch软件生成了私钥和公钥,并为Unix服务器中的用户配置了公钥。
我在我的Java程序中使用了IPSwitch生成的私钥,而后者又使用了JSch。
IPSwitch的Store.ini文件看起来像
[____Item____0]
____ItemName____=auto
PublicKey=C:\ClientKeyStore\Pub203E.tmp
Pass=_y**************************NyYbpT
PrivateKey=C:\ClientKeyStore\Pri203F.tmp
[__CONFIG__]
Count=1私钥看上去像,
-----BEGIN ENCRYPTED PRIVATE KEY-----
MIIFHzBJB*******************************
B**************************************B
************1on4MNDZfTgQ==
-----END ENCRYPTED PRIVATE KEY-----在堆栈溢出和其他地方有很多例子。我试过了,但我正在点击com.jcraft.jsch.JSchException: 8月取消。
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.UserInfo;
public class UnixUtilities {
public void unixConnectionUsingKey(String username, String hostIPAddress, int portNumber) throws JSchException {
JSch jsch = new JSch();
jsch.addIdentity("src/main/resources/keys/id_rsa");
Session session = jsch.getSession(username, hostIPAddress, portNumber);
session.setConfig("StrictHostKeyChecking", "no");
session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");
UserInfo userinfo = new MyUserInfo();
session.setUserInfo(userinfo);
session.connect();
Channel channel = session.openChannel("sftp");
}
}
class MyUserInfo implements UserInfo {
String password = null;
@Override
public String getPassphrase() {
return null;
}
@Override
public String getPassword() {
return password;
}
public void setPassword(String passwd) {
password = passwd;
}
@Override
public boolean promptPassphrase(String message) {
return true;
}
@Override
public boolean promptPassword(String message) {
return true;
}
@Override
public boolean promptYesNo(String message) {
return true;
}
@Override
public void showMessage(String message) {
// TODO Auto-generated method stub
}
}我在打
com.jcraft.jsch.JSchException: Auth cancel
at com.jcraft.jsch.Session.connect(Session.java:511)
at com.jcraft.jsch.Session.connect(Session.java:183)当我只使用公钥而不是公开键,键盘交互,密码作为PreferredAuthentications时,我是在敲击,
com.jcraft.jsch.JSchException: Auth fail
at com.jcraft.jsch.Session.connect(Session.java:512)
at com.jcraft.jsch.Session.connect(Session.java:183)另外,如果我忽略了下面的2行,也就是说,如果我评论了下面的2行
UserInfo userinfo = new MyUserInfo();
session.setUserInfo(userinfo);我在打,
com.jcraft.jsch.JSchException: USERAUTH fail
at com.jcraft.jsch.UserAuthPublicKey.start(UserAuthPublicKey.java:119)
at com.jcraft.jsch.Session.connect(Session.java:463)
at com.jcraft.jsch.Session.connect(Session.java:183)Jsch日志如下,
Connecting to ***.***.**.*** port ****
Connection established
Remote version string: SSH-2.0-6.4.0.370 SSH Tectia Server
Local version string: SSH-2.0-JSCH-0.1.53
CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256
aes256-ctr is not available.
aes192-ctr is not available.
aes256-cbc is not available.
aes192-cbc is not available.
CheckKexes: diffie-hellman-group14-sha1,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521
CheckSignatures: ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
SSH_MSG_KEXINIT sent
SSH_MSG_KEXINIT received
kex: server: diffie-hellman-group1-sha1,diffie-hellman-group14-sha1,diffie-hellman-group14-sha256@ssh.com,diffie-hellman-group-exchange-sha1,diffie-hellman-group-exchange-sha256
kex: server: ssh-rsa,ssh-rsa-sha256@ssh.com
kex: server: aes128-cbc
kex: server: aes128-cbc
kex: server: hmac-sha1,hmac-sha1-96,hmac-sha256-2@ssh.com,hmac-sha224@ssh.com,hmac-sha256@ssh.com,hmac-sha384@ssh.com,hmac-sha512@ssh.com
kex: server: hmac-sha1,hmac-sha1-96,hmac-sha256-2@ssh.com,hmac-sha224@ssh.com,hmac-sha256@ssh.com,hmac-sha384@ssh.com,hmac-sha512@ssh.com
kex: server: none,zlib
kex: server: none,zlib
kex: server:
kex: server:
kex: client: ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1
kex: client: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc
kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc
kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
kex: client: none
kex: client: none
kex: client:
kex: client:
kex: server->client aes128-cbc hmac-sha1 none
kex: client->server aes128-cbc hmac-sha1 none
SSH_MSG_KEXDH_INIT sent
expecting SSH_MSG_KEXDH_REPLY
ssh_rsa_verify: signature true
Permanently added '***.***.**.***' (RSA) to the list of known hosts.
SSH_MSG_NEWKEYS sent
SSH_MSG_NEWKEYS received
SSH_MSG_SERVICE_REQUEST sent
SSH_MSG_SERVICE_ACCEPT received
Authentications that can continue: publickey
Next authentication method: publickey
Disconnecting from ***.***.**.*** port ****我使用的密钥与IPSwitch生成的密钥相同,并在eclipse中将其命名为id_rsa。从IPSwitch生成的私钥是否也适用于JSch?
当我通过IPSwitch连接时,一切正常。你能帮我一下吗?谢谢。
发布于 2019-06-12 20:20:09
要为加密的私钥提供密码,您需要实现UserInfo.promptPassphrase和UserInfo.getPassphrase,而不是UserInfo.getPassword和UserInfo.promptPassword。
@Override
public boolean promptPassphrase(String message) {
return true;
}
@Override
public String getPassphrase() {
return password;
}(为了保持一致性,应该将password字段重命名为passphrase__)。
发布于 2019-06-17 19:33:51
感谢Martin和社区在我解决这个问题的时候参与了这个问题。
很简单。正如Martin提到的他的一条评论,我只需要使用另一种工具(比如SSH)再生成一组密钥,然后使用它。我使用了一个名为VANDYKE SecureCRT的软件来生成多一组密钥,它在JSch中运行得很好。看起来,IPSwitch在生成键时做了一些事情,而这些键似乎不适用于JSch。它可能是在IPSwitch的配置文件中可用的'Pass‘,它是特定于IPSwitch的。
代码如下,
JSch jsch = new JSch();
jsch.addIdentity(privateKeyPath);
JSch.setLogger(new com.jcraft.jsch.Logger() {
Path path = Paths.get("target/jsch.log");
@Override
public boolean isEnabled(int level){
return true;
}
public void log(int level, String message){
try {
StandardOpenOption option =
!Files.exists(path) ? StandardOpenOption.CREATE : StandardOpenOption.APPEND;
Files.write(path, java.util.Arrays.asList(message), option);
} catch (IOException e) {
System.err.println(message);
}
}
});
Session session = jsch.getSession(username, host, port);
session.setConfig("StrictHostKeyChecking", "no");
session.setConfig("PreferredAuthentications", "publickey");
session.connect();在这里,setlogger方法可以帮助您获得良好的日志。如果你不想要,你可以忽略这一步。
https://stackoverflow.com/questions/56567599
复制相似问题