110,536
社区成员
发帖
与我相关
我的任务
分享
private static bool OnCertificateValidation(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate, System.Security.Cryptography.X509Certificates.X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
if (sslPolicyErrors == SslPolicyErrors.None)
{
return true;
}
return true;
}
2、服务器端
TcpListener lister = new TcpListener(IPAddress.Parse("127.0.0.1"), 442);
lister.Start();
while (true)
{
var newClient = await lister.AcceptTcpClientAsync();
SslStream sl = new SslStream(newClient.GetStream(), false, OnCertificateValidation);
await sl.AuthenticateAsServerAsync(cert);
var bytes = Encoding.ASCII.GetBytes(new string('c', 5));
await sl.WriteAsync(bytes, 0, bytes.Length);
await sl.WriteAsync(bytes, 0, bytes.Length);
await sl.WriteAsync(bytes, 0, bytes.Length);
await sl.WriteAsync(bytes, 0, bytes.Length);
}
3、客户端
TcpClient client = new TcpClient();
client.Connect(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 442));
SslStream sl = new SslStream(client.GetStream(), false, OnCertificateValidation);
sl.AuthenticateAsClient("localhost");
byte[] date = new byte[65535];
while (true)
{
int len = sl.Read(date, 0, date.Length);
string text = Encoding.ASCII.GetString(date, 0, len);
Console.WriteLine(text);
}
这样一看,确实发送的数据是加密的了,不知道这样是否可行