gpt4 book ai didi

C# tls1.3 异常 : Cannot determine the frame size or a corrupted frame was received

转载 作者:行者123 更新时间:2023-12-04 13:54:40 25 4
gpt4 key购买 nike

我想测试tls1.3,所以我在VS 2019(版本16.7.7)中创建了一个控制台应用程序,目标框架是.NET Core 3.1。
enter image description here
我的程序.cs

using System;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Authentication;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace TestSsl {
class Program {
static void Main(string[] args) {
SslProtocols protocol = SslProtocols.Tls13;
Console.WriteLine($"testing SslProtocols.{protocol}");
int port = 1999;
RemoteCertificateValidationCallback certificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => {
return (true);
};
X509Certificate2 serverCert = new X509Certificate2("server.pfx", "testpass123");
X509Certificate2 clientCert = new X509Certificate2("client.pfx", "testpass123");
TcpListener server = TcpListener.Create(port);
server.Server.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
server.Server.NoDelay = true;
server.Server.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.IPv6Only, false);
server.Start();
Task taskServer = Task.Run(() => {
TcpClient romoteClient = server.AcceptTcpClient();
Task.Run(() => {
using(romoteClient) {
using(SslStream sslStreamRomoteClient = new SslStream(romoteClient.GetStream(), false, certificateValidationCallback)) {
try {
sslStreamRomoteClient.AuthenticateAsServer(serverCert, true, protocol, true);
byte[] buf = new byte[1000];
int len = sslStreamRomoteClient.Read(buf, 0, buf.Length);
string receive = Encoding.UTF8.GetString(buf, 0, len);
Console.WriteLine($"server receive:{receive}");
sslStreamRomoteClient.Write(Encoding.UTF8.GetBytes("Ok"));
Console.WriteLine($"server send:Ok");
} catch(Exception ex) {
Console.WriteLine(ex);
}
}
}
}).Wait();
});
Task taskClient = Task.Run(() => {
try {
using(TcpClient client = new TcpClient()) {
client.Connect("127.0.0.1", port);
using(SslStream sslStreamClient = new SslStream(client.GetStream(), false, certificateValidationCallback)) {
sslStreamClient.AuthenticateAsClient("127.0.0.1", new X509CertificateCollection() { clientCert }, protocol, true);
string send = "hi, i am testing tls";
sslStreamClient.Write(Encoding.UTF8.GetBytes(send));
Console.WriteLine($"client send:{send}");
byte[] buf = new byte[1000];
int len = sslStreamClient.Read(buf);
string receive = Encoding.UTF8.GetString(buf, 0, len);
Console.WriteLine($"client receive:{receive}");
}
}
} catch(Exception ex) {
Console.WriteLine(ex);
}
});
Task.WaitAll(taskClient, taskServer);
}
}
}
然后根据 how to enable TLS 1.3 in windows 10我在 regedit 中启用了 TLS 1.3。
TLS1.3 client
TLS1.3 server
我的电脑信息:
enter image description here
然后我调试我的项目并遇到异常
enter image description here
调试控制台:
enter image description here
这些 pfx 证书有什么要求吗?
如何解决这个异常?请帮忙。谢谢。

最佳答案

目前,Windows 10 的最高版本是 20H2(OS Build 19042.630)。 TLS1.3 服务器只有在 TLS1.3 server is enabled in regedit 时才能正常工作.但是 TLS1.3 客户端甚至无法工作 TLS1.3 client is enabled in regedit .目前 TLS1.3 客户端仅适用于 Windows 10 Insider Preview Build 20170 .

关于C# tls1.3 异常 : Cannot determine the frame size or a corrupted frame was received,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64591531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com