C#中使用SslStream类来创建SSL服务器Word文档下载推荐.docx

上传人:b****1 文档编号:5761452 上传时间:2023-05-05 格式:DOCX 页数:8 大小:15.67KB
下载 相关 举报
C#中使用SslStream类来创建SSL服务器Word文档下载推荐.docx_第1页
第1页 / 共8页
C#中使用SslStream类来创建SSL服务器Word文档下载推荐.docx_第2页
第2页 / 共8页
C#中使用SslStream类来创建SSL服务器Word文档下载推荐.docx_第3页
第3页 / 共8页
C#中使用SslStream类来创建SSL服务器Word文档下载推荐.docx_第4页
第4页 / 共8页
C#中使用SslStream类来创建SSL服务器Word文档下载推荐.docx_第5页
第5页 / 共8页
C#中使用SslStream类来创建SSL服务器Word文档下载推荐.docx_第6页
第6页 / 共8页
C#中使用SslStream类来创建SSL服务器Word文档下载推荐.docx_第7页
第7页 / 共8页
C#中使用SslStream类来创建SSL服务器Word文档下载推荐.docx_第8页
第8页 / 共8页
亲,该文档总共8页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

C#中使用SslStream类来创建SSL服务器Word文档下载推荐.docx

《C#中使用SslStream类来创建SSL服务器Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《C#中使用SslStream类来创建SSL服务器Word文档下载推荐.docx(8页珍藏版)》请在冰点文库上搜索。

C#中使用SslStream类来创建SSL服务器Word文档下载推荐.docx

serverCertificate=X509Certificate.CreateFromCertFile(certificate);

TcpListenerlistener=newTcpListener(IPAddress.Any,8080);

listener.Start();

while(true)

Console.WriteLine("

Waitingforaclienttoconnect..."

);

TcpClientclient=listener.AcceptTcpClient();

ProcessClient(client);

}

staticvoidProcessClient(TcpClientclient)

SslStreamsslStream=newSslStream

(client.GetStream(),false);

try

sslStream.AuthenticateAsServer(serverCertificate,

false,SslProtocols.Tls,true);

DisplaySecurityLevel(sslStream);

DisplaySecurityServices(sslStream);

DisplayCertificateInformation(sslStream);

DisplayStreamProperties(sslStream);

sslStream.ReadTimeout=5000;

sslStream.WriteTimeout=5000;

Waitingforclientmessage..."

stringmessageData=ReadMessage(sslStream);

Received:

{0}"

messageData);

byte[]message=Encoding.UTF8.GetBytes("

Hellofromtheserver."

Sendinghellomessage."

sslStream.Write(message);

catch(AuthenticationExceptione)

Exception:

e.Message);

if(e.InnerException!

=null)

Innerexception:

e.InnerException.Message);

Authenticationfailed-closingtheconnection."

sslStream.Close();

client.Close();

return;

finally

staticstringReadMessage(SslStreamsslStream)

byte[]buffer=newbyte[2048];

StringBuildermessageData=newStringBuilder();

intbytes=-1;

do

bytes=sslStream.Read(buffer,0,buffer.Length);

Decoderdecoder=Encoding.UTF8.GetDecoder();

char[]chars=newchar[decoder.GetCharCount(buffer,0,bytes)];

decoder.GetChars(buffer,0,bytes,chars,0);

messageData.Append(chars);

if(messageData.ToString().IndexOf("

"

)!

=-1)

break;

while(bytes!

=0);

returnmessageData.ToString();

staticvoidDisplaySecurityLevel(SslStreamstream)

Cipher:

{0}strength{1}"

stream.CipherAlgorithm,stream.CipherStrength);

Hash:

stream.HashAlgorithm,stream.HashStrength);

Keyexchange:

stream.KeyExchangeAlgorithm,stream.KeyExchangeStrength);

Protocol:

stream.SslProtocol);

staticvoidDisplaySecurityServices(SslStreamstream)

Isauthenticated:

{0}asserver?

{1}"

stream.IsAuthenticated,stream.IsServer);

IsSigned:

stream.IsSigned);

IsEncrypted:

stream.IsEncrypted);

staticvoidDisplayStreamProperties(SslStreamstream)

Canread:

{0},write{1}"

stream.CanRead,stream.CanWrite);

Cantimeout:

stream.CanTimeout);

staticvoidDisplayCertificateInformation(SslStreamstream)

Certificaterevocationlistchecked:

stream.CheckCertRevocationStatus);

X509CertificatelocalCertificate=stream.LocalCertificate;

if(stream.LocalCertificate!

Localcertwasissuedto{0}andisvalidfrom{1}until{2}."

localCertificate.Subject,

localCertificate.GetEffectiveDateString(),

localCertificate.GetExpirationDateString());

else

Localcertificateisnull."

X509CertificateremoteCertificate=stream.RemoteCertificate;

if(stream.RemoteCertificate!

Remotecertwasissuedto{0}andisvalidfrom{1}until{2}."

remoteCertificate.Subject,

remoteCertificate.GetEffectiveDateString(),

remoteCertificate.GetExpirationDateString());

Remotecertificateisnull."

privatestaticvoidDisplayUsage()

Tostarttheserverspecify:

serverSynccertificateFile.cer"

Environment.Exit

(1);

publicstaticintMain(string[]args)

stringcertificate=null;

if(args==null||args.Length<

1)

DisplayUsage();

certificate=args[0];

SslTcpServer.RunServer(certificate);

return0;

}

C#中使用SslStream类来创建SSL客户端

usingSystem.Collections;

publicclassSslTcpClient

privatestaticHashtablecertificateErrors=newHashtable();

publicstaticboolValidateServerCertificate(

objectsender,

X509Certificatecertificate,

X509Chainchain,

SslPolicyErrorssslPolicyErrors)

if(sslPolicyErrors==SslPolicyErrors.None)

returntrue;

Certificateerror:

sslPolicyErrors);

returnfalse;

publicstaticvoidRunClient(stringmachineName,stringserverName)

TcpClientclient=newTcpClient(machineName,443);

Clientconnected."

SslStreamsslStream=newSslStream(

client.GetStream(),

false,

newRemoteCertificateValidationCallback(ValidateServerCertificate),

null

);

sslStream.AuthenticateAsClient(serverName);

byte[]messsage=Encoding.UTF8.GetBytes("

Hellofromtheclient."

sslStream.Write(messsage);

sslStream.Flush();

stringserverMessage=ReadMessage(sslStream);

Serversays:

serverMessage);

Clientclosed."

Tostarttheclientspecify:

clientSyncmachineName[serverName]"

stringserverCertificateName=null;

stringmachineName=null;

machineName=args[0];

if(args.Length<

2)

serverCertificateName=machineName;

serverCertificat

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 人文社科 > 文学研究

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2