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;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
using
SFTP;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
namespace
TestSFTP
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
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;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public Form1()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//
// Required for Windows Form Designer support
//
InitializeComponent();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//
// TODO: Add any constructor code after InitializeComponent call
//
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if( disposing )
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (components != null)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
components.Dispose();
}
}
base.Dispose( disposing );
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
Windows Form Designer generated code#region Windows Form Designer generated code
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
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);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Application.Run(new Form1());
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private void button1_Click(object sender, System.EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
openFileDialog1 = new OpenFileDialog();
openFileDialog1.RestoreDirectory = true ;
openFileDialog1.Filter = "All files (*.*)|*.*";
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
if(openFileDialog1.ShowDialog() == DialogResult.OK)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
this.txtLocalFile.Text = openFileDialog1.FileName;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private void button2_Click(object sender, System.EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
SshConnectionInfo objInfo = new SshConnectionInfo();
objInfo.Host = this.txtHostName.Text;
objInfo.User = this.txtUserName.Text;
objInfo.Pass = this.txtPassword.Text;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
SFTPHelper objSFTPHelper = new SFTPHelper(objInfo);
objSFTPHelper.Connect();
if(objSFTPHelper.Connected)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
objSFTPHelper.Upload(this.txtLocalFile.Text,this.txtRemoteFile.Text);
objSFTPHelper.Close();
MessageBox.Show("Upload Successful");
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
this.txtLocalFile.Text = "";
this.txtRemoteFile.Text = "";
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private void button3_Click(object sender, System.EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
SshConnectionInfo objInfo = new SshConnectionInfo();
objInfo.Host = this.txtHostName.Text;
objInfo.User = this.txtUserName.Text;
objInfo.Pass = this.txtPassword.Text;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
SFTPHelper objSFTPHelper = new SFTPHelper(objInfo);
objSFTPHelper.Connect();
if(objSFTPHelper.Connected)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
objSFTPHelper.Download(this.txtRemoteFile.Text,this.txtLocalFile.Text);
objSFTPHelper.Close();
MessageBox.Show("Download Successful!");
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
this.txtLocalFile.Text = "";
this.txtRemoteFile.Text = "";
}
}
}