C# 如何禁用SslStream类的加密?

C# 如何禁用SslStream类的加密?,c#,.net,encryption,ftp,sslstream,C#,.net,Encryption,Ftp,Sslstream,我正在通过连接到FTPS服务器。一切都很好。但是,我需要支持一个名为“”的命令,该命令基本上禁用加密。如果我只是这样做: SendCommand("CCC"); sslStream.Close(); netStream.Write("...<futher FTP commands>...") 如您所见,FTP服务器发回“200未加密的控制连接”,这意味着命令成功。还需要注意的是,响应是以加密格式发送的 所以我可能需要在禁用加密的同时继续使用SslStream。在禁用加密算法时,可能

我正在通过连接到FTPS服务器。一切都很好。但是,我需要支持一个名为“”的命令,该命令基本上禁用加密。如果我只是这样做:

SendCommand("CCC");
sslStream.Close();
netStream.Write("...<futher FTP commands>...")
如您所见,FTP服务器发回“200未加密的控制连接”,这意味着命令成功。还需要注意的是,响应是以加密格式发送的

所以我可能需要在禁用加密的同时继续使用SslStream。在禁用加密算法时,可能仍然需要“块模式”通信。有人知道我该怎么做吗


“关闭安全连接”和 丢了

“关闭安全连接”指在不关闭底层TCP连接的情况下发送TLS close_notify警报。类“”支持此功能。通常,当您调用
SslStream.Close()
时,对等方通过交换TLS Close\u notify消息安全地关闭TLS连接,然后基础TCP连接立即关闭。但是,如果使用的SslStream构造函数的
leaveInnerStreamOpen
参数设置为
true
,则TCP连接不会关闭,并且可能会在其上发送更多不安全的数据。比如说,

var tcpClient = new TcpClient("127.0.0.1", 9876);
var tcpStream = tcpClient.GetStream();
var sslStream = new SslStream(tcpStream, true);
sslStream.AuthenticateAsClient("127.0.0.1");
sslStream.Write(....);  // use the secure connection.
sslStream.Close();   // close the TLS connection: the tcp stream is still viable
tcpStream.Write(...) // use the unsecured TCP connection
tcpStream.Close();  // now the TCP connection is terminated.

现在,手动实现FTP over SSL客户端似乎相当棘手。就我个人而言,在尝试编写我自己的实现之前,我会寻找一个现有的成熟实现。

这个类适合我。当您对其调用Close()时,它会发送SSL Close\u notify警报!然后,您可以继续向基本流发送明文数据。

我认为服务器可能希望您实际关闭安全连接,而不仅仅是删除它。据我所知,
SslStream
不支持这一点,因此您可能需要研究如何手动执行此操作“关闭安全连接”和删除它之间的区别是什么?谢谢您,詹姆斯,但我已经意识到了这一点,正如我在Q中的代码片段所示。但是我不理解“手动通过SSL客户端进行FTP”。为什么我不能在关闭SSL流后使用tcpStream.Write()发送纯文本数据?是否需要其他格式?
var tcpClient = new TcpClient("127.0.0.1", 9876);
var tcpStream = tcpClient.GetStream();
var sslStream = new SslStream(tcpStream, true);
sslStream.AuthenticateAsClient("127.0.0.1");
sslStream.Write(....);  // use the secure connection.
sslStream.Close();   // close the TLS connection: the tcp stream is still viable
tcpStream.Write(...) // use the unsecured TCP connection
tcpStream.Close();  // now the TCP connection is terminated.