使用Tamir.sharpSsh.dll实现SFTP文件传输

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

        
/// <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 = "";
        }

    }

}
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值