Advertisement
MarcelloGrechi

Crednet Access

Apr 9th, 2012
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.74 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Security.Cryptography.X509Certificates;
  4. using System.Net.Security;
  5. using System.Net.Sockets;
  6. using System.Security.Authentication;
  7. using BDC.BDCCommons;
  8.  
  9. namespace QueryCrednet
  10. {
  11.     public class SSLClient
  12.     {
  13.         private string m_server;
  14.         private string m_host;
  15.         private int    m_port;
  16.  
  17.         private SslStream m_sslStream;
  18.  
  19.         private const byte EOF = 0x0003;
  20.  
  21.         public SSLClient (string dnsServer, string host, int port)
  22.         {
  23.             m_server = dnsServer;
  24.             m_host   = host;
  25.             m_port   = port;
  26.         }
  27.  
  28.         public bool ValidateCertificate (Object sender, X509Certificate certificate, X509Chain chain,SslPolicyErrors errors)
  29.         {
  30.             if (errors == SslPolicyErrors.None)
  31.             {
  32.                 return true;
  33.             }
  34.  
  35.             return false;
  36.         }
  37.  
  38.         public void WriteMessage (string message)
  39.         {
  40.             // Creates Host
  41.             using (TcpClient client = new TcpClient (m_host, m_port))
  42.             {
  43.                 // Creating SSL Stream
  44.                 m_sslStream = new SslStream(client.GetStream (),false, new RemoteCertificateValidationCallback(ValidateCertificate),null);
  45.  
  46.                 // Creating Connection
  47.                 try
  48.                 {
  49.                    // Authentication will be executed as a client
  50.                     m_sslStream.AuthenticateAsClient (m_server);
  51.                 }
  52.                 catch (AuthenticationException ex)
  53.                 {
  54.                    LogWriter.Error (ex);
  55.                 }
  56.  
  57.                // Encoding message
  58.                byte[] encodedMessage = Encoding.UTF8.GetBytes (message);
  59.                m_sslStream.Write (encodedMessage);
  60.                m_sslStream.WriteByte (EOF);
  61.                m_sslStream.Flush ();
  62.             }
  63.         }
  64.  
  65.         public string ReadMessage()
  66.         {
  67.             // Creating buffer with stream size
  68.             byte[]        buffer      = new byte[2048];
  69.  
  70.             // Message Container
  71.             StringBuilder messageData = new StringBuilder();
  72.  
  73.             int bytes = - 1;
  74.             do
  75.             {
  76.                 // Reading Stream
  77.                 bytes = m_sslStream.Read (buffer, 0, buffer.Length);
  78.  
  79.                 // Decoding Message
  80.                 Decoder decoder = Encoding.UTF8.GetDecoder ();
  81.                 char[] chars = new char[decoder.GetCharCount (buffer,0,bytes)];
  82.                 decoder.GetChars (buffer, 0, bytes, chars, 0);
  83.  
  84.                 // Reading message
  85.                 messageData.Append (chars);
  86.                
  87.             } while (bytes != 0 && bytes != EOF);
  88.  
  89.             return messageData.ToString();
  90.         }
  91.     }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement