我正在尝试了解 SSL 的工作原理。我希望制作一个支持 SSL 的小型 FTP 客户端,但遇到了一些问题:
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(text); 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。 由于意外的数据包格式(我试过谷歌搜索,但所有人都说这是因为作者连接到错误的端口),我一直死于握手失败,我将其视为:The connection is not ssl until AUTH SSL is send to它。所以我的问题是,我将如何着手使它成为“混合”连接,以便我可以与服务器建立 SSL 连接?
非常感谢任何帮助!
最佳答案
使用这样的库与我想要的相反。由于我在网上搜索时发现的结果很少,所以我将发布我的想法: 构建一个 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 的函数(您可以自行谷歌并轻松修改)。
有关详细信息,请参阅 RFC 4217 和 2228。
关于c# - 使用 C# 的原始 FTP SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2524093/