Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Text;
- using System.Security.Cryptography.X509Certificates;
- using System.Net.Security;
- using System.Net.Sockets;
- using System.Security.Authentication;
- using BDC.BDCCommons;
- namespace QueryCrednet
- {
- public class SSLClient
- {
- private string m_server;
- private string m_host;
- private int m_port;
- private SslStream m_sslStream;
- private const byte EOF = 0x0003;
- public SSLClient (string dnsServer, string host, int port)
- {
- m_server = dnsServer;
- m_host = host;
- m_port = port;
- }
- public bool ValidateCertificate (Object sender, X509Certificate certificate, X509Chain chain,SslPolicyErrors errors)
- {
- if (errors == SslPolicyErrors.None)
- {
- return true;
- }
- return false;
- }
- public void WriteMessage (string message)
- {
- // Creates Host
- using (TcpClient client = new TcpClient (m_host, m_port))
- {
- // Creating SSL Stream
- m_sslStream = new SslStream(client.GetStream (),false, new RemoteCertificateValidationCallback(ValidateCertificate),null);
- // Creating Connection
- try
- {
- // Authentication will be executed as a client
- m_sslStream.AuthenticateAsClient (m_server);
- }
- catch (AuthenticationException ex)
- {
- LogWriter.Error (ex);
- }
- // Encoding message
- byte[] encodedMessage = Encoding.UTF8.GetBytes (message);
- m_sslStream.Write (encodedMessage);
- m_sslStream.WriteByte (EOF);
- m_sslStream.Flush ();
- }
- }
- public string ReadMessage()
- {
- // Creating buffer with stream size
- byte[] buffer = new byte[2048];
- // Message Container
- StringBuilder messageData = new StringBuilder();
- int bytes = - 1;
- do
- {
- // Reading Stream
- bytes = m_sslStream.Read (buffer, 0, buffer.Length);
- // Decoding Message
- Decoder decoder = Encoding.UTF8.GetDecoder ();
- char[] chars = new char[decoder.GetCharCount (buffer,0,bytes)];
- decoder.GetChars (buffer, 0, bytes, chars, 0);
- // Reading message
- messageData.Append (chars);
- } while (bytes != 0 && bytes != EOF);
- return messageData.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement