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