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>
/// SummarydescriptionforSFTPHelper.
/// </summary>
public class SFTPHelper
{
private SFTPHelper()
{
}
private SshTransferProtocolBasem_sshCp;
public bool Connected
{
get
{
return m_sshCp.Connected;
}
}
public SFTPHelper(SshConnectionInfoconnectionInfo)
{
//
// TODO:CheckconnectionInfo
//
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 ponentModel;
using System.Windows.Forms;
using System.Data;
using SFTP;
namespace TestSFTP
... {
/**////<summary>
///SummarydescriptionforForm1.
///</summary>
publicclassForm1:System.Windows.Forms.Form
...{
privateSystem.Windows.Forms.GroupBoxgroupBox1;
privateSystem.Windows.Forms.Labellabel1;
privateSystem.Windows.Forms.Labellabel2;
privateSystem.Windows.Forms.Labellabel3;
privateSystem.Windows.Forms.TextBoxtxtHostName;
privateSystem.Windows.Forms.TextBoxtxtUserName;
privateSystem.Windows.Forms.TextBoxtxtPassword;
privateSystem.Windows.Forms.GroupBoxgroupBox2;
privateSystem.Windows.Forms.OpenFileDialogopenFileDialog1;
privateSystem.Windows.Forms.Labellabel4;
privateSystem.Windows.Forms.Buttonbutton1;
privateSystem.Windows.Forms.Labellabel5;
privateSystem.Windows.Forms.Buttonbutton2;
privateSystem.Windows.Forms.Buttonbutton3;
privateSystem.Windows.Forms.TextBoxtxtLocalFile;
privateSystem.Windows.Forms.TextBoxtxtRemoteFile;
/**////<summary>
///Requireddesignervariable.
///</summary>
ponentModel.Containercomponents=null;
publicForm1()
...{
//
//RequiredforWindowsFormDesignersupport
//
InitializeComponent();
//
//TODO:AddanyconstructorcodeafterInitializeComponentcall
//
}
/**////<summary>
///Cleanupanyresourcesbeingused.
///</summary>
protectedoverridevoidDispose(booldisposing)
...{
if(disposing)
...{
if(components!=null)
...{
components.Dispose();
}
}
base.Dispose(disposing);
}
WindowsFormDesignergeneratedcode#regionWindowsFormDesignergeneratedcode
/**////<summary>
///RequiredmethodforDesignersupport-donotmodify
///thecontentsofthismethodwiththecodeeditor.
///</summary>
privatevoidInitializeComponent()
...{
this.groupBox1=newSystem.Windows.Forms.GroupBox();
this.label1=newSystem.Windows.Forms.Label();
this.label2=newSystem.Windows.Forms.Label();
this.label3=newSystem.Windows.Forms.Label();
this.txtHostName=newSystem.Windows.Forms.TextBox();
this.txtUserName=newSystem.Windows.Forms.TextBox();
this.txtPassword=newSystem.Windows.Forms.TextBox();
this.groupBox2=newSystem.Windows.Forms.GroupBox();
this.openFileDialog1=newSystem.Windows.Forms.OpenFileDialog();
this.txtLocalFile=newSystem.Windows.Forms.TextBox();
this.label4=newSystem.Windows.Forms.Label();
this.button1=newSystem.Windows.Forms.Button();
this.label5=newSystem.Windows.Forms.Label();
this.txtRemoteFile=newSystem.Windows.Forms.TextBox();
this.button2=newSystem.Windows.Forms.Button();
this.button3=newSystem.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=newSystem.Drawing.Point(12,12);
this.groupBox1.Name="groupBox1";
this.groupBox1.Size=newSystem.Drawing.Size(188,292);
this.groupBox1.TabIndex=0;
this.groupBox1.TabStop=false;
this.groupBox1.Text="ConnectionInfo";
//
//label1
//
this.label1.Location=newSystem.Drawing.Point(8,20);
this.label1.Name="label1";
this.label1.Size=newSystem.Drawing.Size(60,23);
this.label1.TabIndex=0;
this.label1.Text="HostName";
//
//label2
//
this.label2.Location=newSystem.Drawing.Point(8,72);
this.label2.Name="label2";
this.label2.Size=newSystem.Drawing.Size(64,23);
this.label2.TabIndex=1;
this.label2.Text="UserName";
//
//label3
//
this.label3.Location=newSystem.Drawing.Point(8,120);
this.label3.Name="label3";
this.label3.Size=newSystem.Drawing.Size(56,23);
this.label3.TabIndex=2;
this.label3.Text="Password";
//
//txtHostName
//
this.txtHostName.Location=newSystem.Drawing.Point(72,20);
this.txtHostName.Name="txtHostName";
this.txtHostName.TabIndex=3;
this.txtHostName.Text="10.16.76.222";
//
//txtUserName
//
this.txtUserName.Location=newSystem.Drawing.Point(72,72);
this.txtUserName.Name="txtUserName";
this.txtUserName.TabIndex=4;
this.txtUserName.Text="JeffZhong";
//
//txtPassword
//
this.txtPassword.Location=newSystem.Drawing.Point(72,120);
this.txtPassword.Name="txtPassword";
this.txtPassword.PasswordChar='*';
this.txtPassword.TabIndex=5;
this.txtPassword.Text="Ecommerce";
//
//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=newSystem.Drawing.Point(224,12);
this.groupBox2.Name="groupBox2";
this.groupBox2.Size=newSystem.Drawing.Size(492,152);
this.groupBox2.TabIndex=1;
this.groupBox2.TabStop=false;
this.groupBox2.Text="UploadandDownload";
//
//txtLocalFile
//
this.txtLocalFile.Location=newSystem.Drawing.Point(88,24);
this.txtLocalFile.Name="txtLocalFile";
this.txtLocalFile.Size=newSystem.Drawing.Size(312,20);
this.txtLocalFile.TabIndex=0;
this.txtLocalFile.Text="";
//
//label4
//
this.label4.Location=newSystem.Drawing.Point(16,24);
this.label4.Name="label4";
this.label4.Size=newSystem.Drawing.Size(60,23);
this.label4.TabIndex=1;
this.label4.Text="LocalFile";
//
//button1
//
this.button1.Location=newSystem.Drawing.Point(404,24);
this.button1.Name="button1";
this.button1.TabIndex=2;
this.button1.Text="Browse";
this.button1.Click+=newSystem.EventHandler(this.button1_Click);
//
//label5
//
this.label5.Location=newSystem.Drawing.Point(16,52);
this.label5.Name="label5";
this.label5.Size=newSystem.Drawing.Size(72,23);
this.label5.TabIndex=3;
this.label5.Text="RemoteFile";
//
//txtRemoteFile
//
this.txtRemoteFile.Location=newSystem.Drawing.Point(88,52);
this.txtRemoteFile.Name="txtRemoteFile";
this.txtRemoteFile.Size=newSystem.Drawing.Size(312,20);
this.txtRemoteFile.TabIndex=4;
this.txtRemoteFile.Text="";
//
//button2
//
this.button2.Location=newSystem.Drawing.Point(136,112);
this.button2.Name="button2";
this.button2.TabIndex=5;
this.button2.Text="Upload";
this.button2.Click+=newSystem.EventHandler(this.button2_Click);
//
//button3
//
this.button3.Location=newSystem.Drawing.Point(260,112);
this.button3.Name="button3";
this.button3.TabIndex=6;
this.button3.Text="Download";
this.button3.Click+=newSystem.EventHandler(this.button3_Click);
//
//Form1
//
this.AutoScaleBaseSize=newSystem.Drawing.Size(5,13);
this.ClientSize=newSystem.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>
///Themainentrypointfortheapplication.
///</summary>
[STAThread]
staticvoidMain()
...{
Application.Run(newForm1());
}
privatevoidbutton1_Click(objectsender,System.EventArgse)
...{
openFileDialog1=newOpenFileDialog();
openFileDialog1.RestoreDirectory=true;
openFileDialog1.Filter="Allfiles(*.*)|*.*";
if(openFileDialog1.ShowDialog()==DialogResult.OK)
...{
this.txtLocalFile.Text=openFileDialog1.FileName;
}
}
privatevoidbutton2_Click(objectsender,System.EventArgse)
...{
SshConnectionInfoobjInfo=newSshConnectionInfo();
objInfo.Host=this.txtHostName.Text;
objInfo.User=this.txtUserName.Text;
objInfo.Pass=this.txtPassword.Text;
SFTPHelperobjSFTPHelper=newSFTPHelper(objInfo);
objSFTPHelper.Connect();
if(objSFTPHelper.Connected)
...{
objSFTPHelper.Upload(this.txtLocalFile.Text,this.txtRemoteFile.Text);
objSFTPHelper.Close();
MessageBox.Show("UploadSuccessful");
}
this.txtLocalFile.Text="";
this.txtRemoteFile.Text="";
}
privatevoidbutton3_Click(objectsender,System.EventArgse)
...{
SshConnectionInfoobjInfo=newSshConnectionInfo();
objInfo.Host=this.txtHostName.Text;
objInfo.User=this.txtUserName.Text;
objInfo.Pass=this.txtPassword.Text;
SFTPHelperobjSFTPHelper=newSFTPHelper(objInfo);
objSFTPHelper.Connect();
if(objSFTPHelper.Connected)
...{
objSFTPHelper.Download(this.txtRemoteFile.Text,this.txtLocalFile.Text);
objSFTPHelper.Close();
MessageBox.Show("DownloadSuccessful!");
}
this.txtLocalFile.Text="";
this.txtRemoteFile.Text="";
}
}
}