C#中使用SslStream类来创建SSL服务器222829

上传人:pu****.1 文档编号:568517566 上传时间:2024-07-25 格式:PDF 页数:10 大小:125.23KB
返回 下载 相关 举报
C#中使用SslStream类来创建SSL服务器222829_第1页
第1页 / 共10页
C#中使用SslStream类来创建SSL服务器222829_第2页
第2页 / 共10页
C#中使用SslStream类来创建SSL服务器222829_第3页
第3页 / 共10页
C#中使用SslStream类来创建SSL服务器222829_第4页
第4页 / 共10页
C#中使用SslStream类来创建SSL服务器222829_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《C#中使用SslStream类来创建SSL服务器222829》由会员分享,可在线阅读,更多相关《C#中使用SslStream类来创建SSL服务器222829(10页珍藏版)》请在金锄头文库上搜索。

1、欢迎您阅读并下载本文档,本文档来源于互联网整理,如有侵权请联系删除!我们将竭诚为您提供优质的文档!C#中使用 SslStream 类来创建 SSL 服务器 / Visual C# using System; using System.Net; using System.Net.Sockets; using System.Net.Security; using System.Text; using System.Security.Authentication; using System.Security.Cryptography.X509Certificates; namespace Examp

2、les.System.Net public sealed class SslTcpServer static X509Certificate serverCertificate = null; public static void RunServer(string certificate) serverCertificate = X509Certificate.CreateFromCertFile(certificate); TcpListener listener = new TcpListener(IPAddress.Any, 8080); listener.Start(); while

3、(true) Console.WriteLine(Waiting for a client to connect.); TcpClient client = listener.AcceptTcpClient(); ProcessClient(client); static void ProcessClient(TcpClient client) 欢迎您阅读并下载本文档,本文档来源于互联网整理,如有侵权请联系删除!我们将竭诚为您提供优质的文档! SslStream sslStream = new SslStream (client.GetStream(), false); try sslStre

4、am.AuthenticateAsServer(serverCertificate, false, SslProtocols.Tls, true); DisplaySecurityLevel(sslStream); DisplaySecurityServices(sslStream); DisplayCertificateInformation(sslStream); DisplayStreamProperties(sslStream); sslStream.ReadTimeout = 5000; sslStream.WriteTimeout = 5000; Console.WriteLine

5、(Waiting for client message.); string messageData = ReadMessage(sslStream); Console.WriteLine(Received: 0, messageData); byte message = Encoding.UTF8.GetBytes(Hello from the server.); Console.WriteLine(Sending hello message.); sslStream.Write(message); catch (AuthenticationException e) Console.Write

6、Line(Exception: 0, e.Message); if (e.InnerException != null) Console.WriteLine(Inner exception: 0, e.InnerException.Message); Console.WriteLine(Authentication failed - closing the connection.); sslStream.Close(); 欢迎您阅读并下载本文档,本文档来源于互联网整理,如有侵权请联系删除!我们将竭诚为您提供优质的文档! client.Close(); return; finally sslSt

7、ream.Close(); client.Close(); static string ReadMessage(SslStream sslStream) byte buffer = new byte2048; StringBuilder messageData = new StringBuilder(); int bytes = -1; do bytes = sslStream.Read(buffer, 0, buffer.Length); Decoder decoder = Encoding.UTF8.GetDecoder(); char chars = new chardecoder.Ge

8、tCharCount(buffer, 0, bytes); decoder.GetChars(buffer, 0, bytes, chars, 0); messageData.Append(chars); if (messageData.ToString().IndexOf() != -1) break; while (bytes != 0); return messageData.ToString(); 欢迎您阅读并下载本文档,本文档来源于互联网整理,如有侵权请联系删除!我们将竭诚为您提供优质的文档! static void DisplaySecurityLevel(SslStream st

9、ream) Console.WriteLine(Cipher: 0 strength 1, stream.CipherAlgorithm, stream.CipherStrength); Console.WriteLine(Hash: 0 strength 1, stream.HashAlgorithm, stream.HashStrength); Console.WriteLine(Key exchange: 0 strength 1, stream.KeyExchangeAlgorithm, stream.KeyExchangeStrength); Console.WriteLine(Pr

10、otocol: 0, stream.SslProtocol); static void DisplaySecurityServices(SslStream stream) Console.WriteLine(Is authenticated: 0 as server? 1, stream.IsAuthenticated, stream.IsServer); Console.WriteLine(IsSigned: 0, stream.IsSigned); Console.WriteLine(Is Encrypted: 0, stream.IsEncrypted); static void Dis

11、playStreamProperties(SslStream stream) Console.WriteLine(Can read: 0, write 1, stream.CanRead, stream.CanWrite); Console.WriteLine(Can timeout: 0, stream.CanTimeout); static void DisplayCertificateInformation(SslStream stream) Console.WriteLine(Certificate revocation list checked: 0, stream.CheckCer

12、tRevocationStatus); X509Certificate localCertificate = stream.LocalCertificate; if (stream.LocalCertificate != null) 欢迎您阅读并下载本文档,本文档来源于互联网整理,如有侵权请联系删除!我们将竭诚为您提供优质的文档! Console.WriteLine(Local cert was issued to 0 and is valid from 1 until 2., localCertificate.Subject, localCertificate.GetEffectiveDat

13、eString(), localCertificate.GetExpirationDateString(); else Console.WriteLine(Local certificate is null.); X509Certificate remoteCertificate = stream.RemoteCertificate; if (stream.RemoteCertificate != null) Console.WriteLine(Remote cert was issued to 0 and is valid from 1 until 2., remoteCertificate

14、.Subject, remoteCertificate.GetEffectiveDateString(), remoteCertificate.GetExpirationDateString(); else Console.WriteLine(Remote certificate is null.); private static void DisplayUsage() Console.WriteLine(To start the server specify:); Console.WriteLine(serverSync certificateFile.cer); Environment.E

15、xit(1); 欢迎您阅读并下载本文档,本文档来源于互联网整理,如有侵权请联系删除!我们将竭诚为您提供优质的文档! public static int Main(string args) string certificate = null; if (args = null | args.Length 1) DisplayUsage(); certificate = args0; SslTcpServer.RunServer(certificate); return 0; C#中使用 SslStream 类来创建 SSL 客户端 / Visual C# using System; using S

16、ystem.Collections; using System.Net; using System.Net.Security; using System.Net.Sockets; using System.Security.Authentication; using System.Text; using System.Security.Cryptography.X509Certificates; namespace Examples.System.Net public class SslTcpClient 欢迎您阅读并下载本文档,本文档来源于互联网整理,如有侵权请联系删除!我们将竭诚为您提供优

17、质的文档! private static Hashtable certificateErrors = new Hashtable(); public static bool ValidateServerCertificate( object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) if (sslPolicyErrors = SslPolicyErrors.None) return true; Console.WriteLine(Certificate error

18、: 0, sslPolicyErrors); return false; public static void RunClient(string machineName, string serverName) TcpClient client = new TcpClient(machineName, 443); Console.WriteLine(Client connected.); SslStream sslStream = new SslStream( client.GetStream(), false, new RemoteCertificateValidationCallback(V

19、alidateServerCertificate), null ); try sslStream.AuthenticateAsClient(serverName); catch (AuthenticationException e) 欢迎您阅读并下载本文档,本文档来源于互联网整理,如有侵权请联系删除!我们将竭诚为您提供优质的文档!欢迎您阅读并下载本文档,本文档来源于互联网整理,如有侵权请联系删除!我们将竭诚为您提供优质的文档! break; while (bytes != 0); return messageData.ToString(); private static void Displa

20、yUsage() Console.WriteLine(To start the client specify:); Console.WriteLine(clientSync machineName serverName); Environment.Exit(1); public static int Main(string args) string serverCertificateName = null; string machineName = null; if (args = null | args.Length 1) DisplayUsage(); machineName = args0; if (args.Length 2) serverCertificateName = machineName; else serverCertificateName = args1; 欢迎您阅读并下载本文档,本文档来源于互联网整理,如有侵权请联系删除!我们将竭诚为您提供优质的文档! SslTcpClient.RunClient(machineName, serverCertificateName); return 0;

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 建筑/环境 > 施工组织

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号