问题描述
- sftp用证书文件登录报algorithm negotiation fail
-
请教各位大神,我要使用sftp传输文件,可以再登录这块出现了问题几天都没找到解决方法
开发语言C# 用的是网上开源的Tamir.SharpSSH.dll
对方提供了host name和user name
用工具PuTTY在本机生成了2个文件 public key和private key
public key已传给对方加过白名单了
用工具WinSCP 输入host name 和user name再传入private key file可以登录连上对方但是用网上开源的Tamir.SharpSSH.dll和代码连击不上
这是登录的代码
private Session m_session; private Channel m_channel; private ChannelSftp m_sftp; //host:sftp地址 user:用户名 pwd:密码 public Helper(string host, string user, string pwd, string privateKey) { string[] arr = host.Split(':'); string ip = arr[0]; int port = 22; if (arr.Length > 1) port = Int32.Parse(arr[1]); JSch jsch = new JSch(); string passphrase = ""; if (privateKey != null && !"".Equals(privateKey)) { if (passphrase != null && !"".Equals(passphrase)) { //设置带口令的密钥 jsch.addIdentity(privateKey, passphrase); } else { //设置不带口令的密钥 jsch.addIdentity(privateKey); } } m_session = jsch.getSession(user, ip, port); MyUserInfo ui = new MyUserInfo(); ui.setPassword(pwd); m_session.setUserInfo(ui); } //登录验证信息 public class MyUserInfo : UserInfo { String passwd; public String getPassword() { return passwd; } public void setPassword(String passwd) { this.passwd = passwd; } public String getPassphrase() { return null; } public bool promptPassphrase(String message) { return true; } public bool promptPassword(String message) { return true; } public bool promptYesNo(String message) { return true; } public void showMessage(String message) { } } //SFTP连接状态 public bool Connected { get { return m_session.isConnected(); } } //连接SFTP public bool Connect() { try { if (!Connected) { m_session.connect(); m_channel = m_session.openChannel("sftp"); m_channel.connect(); m_sftp = (ChannelSftp)m_channel; } return true; } catch { return false; } }
用PuTTY工具 把.ppa文件转成OpenSSH key
再去执行登录报异常实在没方向了 不知道怎么去做登录了 求做过C#用文件 在程序中登录大神们指点下
解决方案
你的private_key.ppk文件不正确,验证没有通过
时间: 2024-09-08 11:33:13