SFTP即加密文件传输协议(Secure file transfer protocol) ,与FTP不同的是,指令与数据在传输过程中都是经过加密的,防止在网络传输过程中密码和敏感信息被盗取。为了使用.Net来实现这一过程:
1.安装SFTP Server测试环境:
下载setupssh.exe 在服务器上按照提示安装setupssh.exe 打开一个命令行,找到OpenSSH目录(默认:C:/Program Files/OpenSSH) 创建Group mkgroup -l >> . etc group 创建User mkpasswd -l >> . etc passwd注:创建group和User请参考安装目录的readme.txt
2.下载SampleCode
3.新建一个工程,引用SampleCode中的Tamir.sharpSsh.dll,Org.Mentalis.Security.dll,DiffieHellman.dll
4.测试代码:
using System; using Tamir.SharpSsh; namespace SFTP{ /// <summary> /// Summary description for SFTPHelper. /// </summary> public class SFTPHelper { private SFTPHelper() { } private SshTransferProtocolBase m_sshCp; public bool Connected { get { return m_sshCp.Connected; } } public SFTPHelper(SshConnectionInfo connectionInfo) { // // TODO: Check connectionInfo // m_sshCp = new Sftp(connectionInfo.Host,connectionInfo.User); if (connectionInfo.Pass != null ) { m_sshCp.Password = connectionInfo.Pass; } if (connectionInfo.IdentityFile != null ) { m_sshCp.AddIdentityFile(connectionInfo.IdentityFile ); } } public void Connect() { if ( ! m_sshCp.Connected) { m_sshCp.Connect(); } } public void Close() { if (m_sshCp.Connected) { m_sshCp.Close(); } } public bool Upload( string localPath, string remotePath) { try { if ( ! m_sshCp.Connected) { m_sshCp.Connect(); } m_sshCp.Put(localPath, remotePath); return true ; } catch { return false ; } } public bool Download( string remotePath, string localPath) { try { if ( ! m_sshCp.Connected) { m_sshCp.Connect(); } m_sshCp.Get(remotePath,localPath); return true ; } catch { return false ; } } }}
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using SFTP; namespace TestSFTP ... { /**//// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form ...{ private System.Windows.Forms.GroupBox groupBox1; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox txtHostName; private System.Windows.Forms.TextBox txtUserName; private System.Windows.Forms.TextBox txtPassword; private System.Windows.Forms.GroupBox groupBox2; private System.Windows.Forms.OpenFileDialog openFileDialog1; private System.Windows.Forms.Label label4; private System.Windows.Forms.Button button1; private System.Windows.Forms.Label label5; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button3; private System.Windows.Forms.TextBox txtLocalFile; private System.Windows.Forms.TextBox txtRemoteFile; /**//// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public Form1() ...{ // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /**//// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) ...{ if( disposing ) ...{ if (components != null) ...{ components.Dispose(); } } base.Dispose( disposing ); } Windows Form Designer generated code#region Windows Form Designer generated code /**//// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() ...{ this.groupBox1 = new System.Windows.Forms.GroupBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.txtHostName = new System.Windows.Forms.TextBox(); this.txtUserName = new System.Windows.Forms.TextBox(); this.txtPassword = new System.Windows.Forms.TextBox(); this.groupBox2 = new System.Windows.Forms.GroupBox(); this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog(); this.txtLocalFile = new System.Windows.Forms.TextBox(); this.label4 = new System.Windows.Forms.Label(); this.button1 = new System.Windows.Forms.Button(); this.label5 = new System.Windows.Forms.Label(); this.txtRemoteFile = new System.Windows.Forms.TextBox(); this.button2 = new System.Windows.Forms.Button(); this.button3 = new System.Windows.Forms.Button(); this.groupBox1.SuspendLayout(); this.groupBox2.SuspendLayout(); this.SuspendLayout(); // // groupBox1 // this.groupBox1.Controls.Add(this.txtPassword); this.groupBox1.Controls.Add(this.txtUserName); this.groupBox1.Controls.Add(this.txtHostName); this.groupBox1.Controls.Add(this.label3); this.groupBox1.Controls.Add(this.label2); this.groupBox1.Controls.Add(this.label1); this.groupBox1.Location = new System.Drawing.Point(12, 12); this.groupBox1.Name = "groupBox1"; this.groupBox1.Size = new System.Drawing.Size(188, 292); this.groupBox1.TabIndex = 0; this.groupBox1.TabStop = false; this.groupBox1.Text = "ConnectionInfo"; // // label1 // this.label1.Location = new System.Drawing.Point(8, 20); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(60, 23); this.label1.TabIndex = 0; this.label1.Text = "HostName"; // // label2 // this.label2.Location = new System.Drawing.Point(8, 72); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(64, 23); this.label2.TabIndex = 1; this.label2.Text = "UserName"; // // label3 // this.label3.Location = new System.Drawing.Point(8, 120); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(56, 23); this.label3.TabIndex = 2; this.label3.Text = "Password"; // // txtHostName // this.txtHostName.Location = new System.Drawing.Point(72, 20); this.txtHostName.Name = "txtHostName"; this.txtHostName.TabIndex = 3; this.txtHostName.Text = "10.16.76.222"; // // txtUserName // this.txtUserName.Location = new System.Drawing.Point(72, 72); this.txtUserName.Name = "txtUserName"; this.txtUserName.TabIndex = 4; this.txtUserName.Text = "JeffZhong"; // // txtPassword // this.txtPassword.Location = new System.Drawing.Point(72, 120); this.txtPassword.Name = "txtPassword"; this.txtPassword.PasswordChar = '*'; this.txtPassword.TabIndex = 5; this.txtPassword.Text = "Ecommerce2006"; // // groupBox2 // this.groupBox2.Controls.Add(this.button3); this.groupBox2.Controls.Add(this.button2); this.groupBox2.Controls.Add(this.txtRemoteFile); this.groupBox2.Controls.Add(this.label5); this.groupBox2.Controls.Add(this.button1); this.groupBox2.Controls.Add(this.label4); this.groupBox2.Controls.Add(this.txtLocalFile); this.groupBox2.Location = new System.Drawing.Point(224, 12); this.groupBox2.Name = "groupBox2"; this.groupBox2.Size = new System.Drawing.Size(492, 152); this.groupBox2.TabIndex = 1; this.groupBox2.TabStop = false; this.groupBox2.Text = "Upload and Download"; // // txtLocalFile // this.txtLocalFile.Location = new System.Drawing.Point(88, 24); this.txtLocalFile.Name = "txtLocalFile"; this.txtLocalFile.Size = new System.Drawing.Size(312, 20); this.txtLocalFile.TabIndex = 0; this.txtLocalFile.Text = ""; // // label4 // this.label4.Location = new System.Drawing.Point(16, 24); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(60, 23); this.label4.TabIndex = 1; this.label4.Text = "Local File"; // // button1 // this.button1.Location = new System.Drawing.Point(404, 24); this.button1.Name = "button1"; this.button1.TabIndex = 2; this.button1.Text = "Browse"; this.button1.Click += new System.EventHandler(this.button1_Click); // // label5 // this.label5.Location = new System.Drawing.Point(16, 52); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(72, 23); this.label5.TabIndex = 3; this.label5.Text = "Remote File"; // // txtRemoteFile // this.txtRemoteFile.Location = new System.Drawing.Point(88, 52); this.txtRemoteFile.Name = "txtRemoteFile"; this.txtRemoteFile.Size = new System.Drawing.Size(312, 20); this.txtRemoteFile.TabIndex = 4; this.txtRemoteFile.Text = ""; // // button2 // this.button2.Location = new System.Drawing.Point(136, 112); this.button2.Name = "button2"; this.button2.TabIndex = 5; this.button2.Text = "Upload"; this.button2.Click += new System.EventHandler(this.button2_Click); // // button3 // this.button3.Location = new System.Drawing.Point(260, 112); this.button3.Name = "button3"; this.button3.TabIndex = 6; this.button3.Text = "Download"; this.button3.Click += new System.EventHandler(this.button3_Click); // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(776, 334); this.Controls.Add(this.groupBox2); this.Controls.Add(this.groupBox1); this.Name = "Form1"; this.Text = "Form1"; this.groupBox1.ResumeLayout(false); this.groupBox2.ResumeLayout(false); this.ResumeLayout(false); } #endregion /**//// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() ...{ Application.Run(new Form1()); } private void button1_Click(object sender, System.EventArgs e) ...{ openFileDialog1 = new OpenFileDialog(); openFileDialog1.RestoreDirectory = true ; openFileDialog1.Filter = "All files (*.*)|*.*"; if(openFileDialog1.ShowDialog() == DialogResult.OK) ...{ this.txtLocalFile.Text = openFileDialog1.FileName; } } private void button2_Click(object sender, System.EventArgs e) ...{ SshConnectionInfo objInfo = new SshConnectionInfo(); objInfo.Host = this.txtHostName.Text; objInfo.User = this.txtUserName.Text; objInfo.Pass = this.txtPassword.Text; SFTPHelper objSFTPHelper = new SFTPHelper(objInfo); objSFTPHelper.Connect(); if(objSFTPHelper.Connected) ...{ objSFTPHelper.Upload(this.txtLocalFile.Text,this.txtRemoteFile.Text); objSFTPHelper.Close(); MessageBox.Show("Upload Successful"); } this.txtLocalFile.Text = ""; this.txtRemoteFile.Text = ""; } private void button3_Click(object sender, System.EventArgs e) ...{ SshConnectionInfo objInfo = new SshConnectionInfo(); objInfo.Host = this.txtHostName.Text; objInfo.User = this.txtUserName.Text; objInfo.Pass = this.txtPassword.Text; SFTPHelper objSFTPHelper = new SFTPHelper(objInfo); objSFTPHelper.Connect(); if(objSFTPHelper.Connected) ...{ objSFTPHelper.Download(this.txtRemoteFile.Text,this.txtLocalFile.Text); objSFTPHelper.Close(); MessageBox.Show("Download Successful!"); } this.txtLocalFile.Text = ""; this.txtRemoteFile.Text = ""; } }}