2010-03-26 39 views
2

我想了解SSL如何工作。在我的願望,使一個小的FTP客戶端支持SSL我碰到的一些問題:原始FTP SSL與C#

TcpClient FtpConnection = new TcpClient(FtpServer, FtpPort); 
NetworkStream FtpStream = FtpConnection.GetStream(); 
StreamReader FtpReader = new StreamReader(FtpStream); 
FtpWriter = new StreamWriter(IrcStream); 
send_cmd("AUTH SSL"); 

send_cmd只是一個FtpWriter.WriteLine(文本); FtpWriter.Flush();功能。我的「問題」是這樣的:首先,我需要與FTP建立一個(非SSL)連接,然後告訴它做一個SSL連接(AUTH SSL),然後我想我需要做一個新的連接 - 例如:

TcpClient client = new TcpClient(FtpServer, FtpPort); 
SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null); 
try 
{ 
    sslStream.AuthenticateAsClient("foobar"); 
} 
catch (AuthenticationException e) 
{ 
    MessageBox.Show("Authentication failed - closing the connection."); 
    client.Close(); 
    return; 
} 

取自MSDN。 由於意外的數據包格式(我嘗試了谷歌搜索,但都說這是因爲作者已連接到一個錯誤的端口),我一直在死於握手失敗,我認爲:連接不是ssl,直到AUTH SSL發送到它。所以我的問題是,我將如何去做這個「混合」連接,所以我可以做一個SSL連接到服務器?

任何幫助,非常感謝!

回答

1

使用這樣的庫與我想要的相反。因爲我發現了這樣幾命中在網上搜索的時候,我會寄我想通了: 建立一個C#的FTP客戶端基本上是像這樣:

TcpClient blabla = new TcpClient("some.host", 21); 
NetworkStream blabla_stream = blabla.GetStream(); 
StreamReader unsecure_reader = new StreamReader(blabla_stream); 
StreamWriter blabla_writer = new StreamWriter(blabla_stream); 
blabla_writer.WriteLine("AUTH SSL"); 
string response = ""; 
while ((response = unsecure_reader.ReadLine()) != null) 
{ 
    if (response.Substring(0,3) == "234") 
    { 
     SslStream ssl_connection = new SslStream(blabla.GetStream(), false, new RemoteCertificateValidationCallback(validate_certificate), null); 
     ssl_connection.AuthenticateAsClient(""); 
     StreamReader ssl_stream = new StreamReader(ssl_connection); 
     ftp_writer = new StreamWriter(ssl_connection); 
    } 
} 

其中validate_certificate是基於MSDN的功能(你可以在google它並自己修改它)。

欲瞭解更多信息,請參閱RFC 4217和2228.