Information Transmission Based on the sockert Certificate
/Files/chinasoft/ssl.zip
View code
1 using system;
2 using system. net;
3 using system. net. Sockets;
4 using system. net. Security;
5 using system. text;
6 using system. Security. authentication;
7 using system. Security. cryptography. x509certificates;
8
9 namespace examples. system. net
10 {
11 Public sealed class ssltcpserver
12 {
13 static x509certificate servercertificate = NULL;
14
15 public static int main (string [] ARGs)
16 {
17 string certificate = "localhost. pfx"; // mxd. Cer
18 string Password = "";
19 ssltcpserver. runserver (certificate, password );
20 return 0;
21}
22
23 public static void runserver (string certificate, string password)
24 {
25 servercertificate = new x509certificate2 (certificate, password );
26 tcplistener listener = new tcplistener (IPaddress. Any, 8080 );
27 listener. Start ();
28 while (true)
29 {
30 console. writeline ("waiting for a client to connect ...");
31 tcpclient client = listener. accepttcpclient ();
32 processclient (client );
33}
34}
35 static void processclient (tcpclient client)
36 {
37 sslstream = new sslstream (client. getstream (), false );
38 try
39 {
40 sslstream. authenticateasserver (servercertificate, false, sslprotocols. TLS, false );
41 displaysecuritylevel (sslstream );
42 displaysecurityservices (sslstream );
43 displaycertificateinformation (sslstream );
44 displaystreamproperties (sslstream );
45
46 sslstream. readtimeout = 5000;
47 sslstream. writetimeout = 5000;
48 console. writeline ("waiting for client message ...");
49 string messagedata = readmessage (sslstream );
50 console. writeline ("Received: {0}", messagedata );
51 byte [] Message = encoding. utf8.getbytes ("Hello from the server. ^-^ mxd ...");
52 console. writeline ("sending HELLO message .");
53 sslstream. Write (Message );
54}
55 catch (authenticationexception E)
56 {
57 console. writeline ("exception: {0}", E. Message );
58 If (E. innerexception! = NULL)
59 {
60 console. writeline ("inner exception: {0}", E. innerexception. Message );
61}
62 console. writeline ("authentication failed-closing the connection .");
63 sslstream. Close ();
64 client. Close ();
65 return;
66}
67 finally
68 {
69 sslstream. Close ();
70 client. Close ();
71}
72}
73 static string readmessage (sslstream)
74 {
75 byte [] buffer = new byte [1, 2048];
76 stringbuilder messagedata = new stringbuilder ();
77 int bytes =-1;
78 do
79 {
80 bytes = sslstream. Read (buffer, 0, buffer. Length );
81 decoder = encoding. utf8.getdecoder ();
82 char [] chars = new char [decoder. getcharcount (buffer, 0, bytes)];
83 decoder. getchars (buffer, 0, bytes, chars, 0 );
84 messagedata. append (chars );
85 if (messagedata. tostring (). indexof ("")! =-1)
86 {
87 break;
88}
89}
90 while (Bytes! = 0 );
91
92 return messagedata. tostring ();
93}
94 static void displaysecuritylevel (sslstream Stream)
95 {
96 console. writeline ("cipher: {0} strength {1}", stream. cipheralgorithm, stream. cipherstrength );
97 console. writeline ("hash: {0} strength {1}", stream. hashalgorithm, stream. hashstrength );
98 console. writeline ("Key Exchange: {0} strength {1}", stream. keyexchangealgorithm, stream. keyexchangestrength );
99 console. writeline ("Protocol: {0}", stream. sslprotocol );
100}
101 static void displaysecurityservices (sslstream Stream)
102 {
103 console. writeline ("is authenticated: {0} as server? {1} ", stream. isauthenticated, stream. isserver );
104 console. writeline ("issigned: {0}", stream. issigned );
105 console. writeline ("is encrypted: {0}", stream. isencrypted );
106}
107 static void displaystreamproperties (sslstream Stream)
108 {
109 console. writeline ("can read: {0}, write {1}", stream. Canread, stream. canwrite );
110 console. writeline ("Can Timeout: {0}", stream. cantimeout );
111}
112 static void displaycertificateinformation (sslstream Stream)
113 {
114 console. writeline ("Certificate Revocation List checked: {0}", stream. checkcertrevocationstatus );
115
116 x509certificate localcertificate = stream. localcertificate;
117 If (stream. localcertificate! = NULL)
118 {
119 console. writeline ("local CERT was issued to {0} and is valid from {1} until {2 }.",
120 localcertificate. subject,
121 localcertificate. getinclutivedatestring (),
122 localcertificate. getexpirationdatestring ());
123}
124 else
125 {
126 console. writeline ("Local Certificate is null .");
127}
128 x509certificate remotecertificate = stream. remotecertificate;
129 If (stream. remotecertificate! = NULL)
130 {
131 console. writeline ("remote Cert was issued to {0} and is valid from {1} until {2 }.",
132 remotecertificate. subject,
133 remotecertificate. getinclutivedatestring (),
134 remotecertificate. getexpirationdatestring ());
135}
136 else
137 {
138 console. writeline ("remote certificate is null .");
139}
140}
141 Private Static void displayusage ()
142 {
143 console. writeline ("to start the server specify :");
144 console. writeline ("serversync certificatefile. Cer ");
145 console. Readline ();
146 environment. Exit (1 );
147}
148}
149}