Daily using/supporting

Get Firefox browser!
Get Thunderbird!
Get Opera browser!
Get The Gimp!
Get Inkscape!
Get LibreOffice!
Get Videolan!
Get Linux!
Get Mandriva!
Get Joomla!
Hacker Emblem

Archives

Which topics would you like us to cover more?

Latest comments

Latest tweets

about 1 day ago Using REDIPS.drag to add drag and drop to your .Net webapplication #li #dib0 http://t.co/n8zY3s7d
about 7 days ago http://t.co/cknQcDbo #Kindle
about 15 days ago Freedom isn't the ability to choose what to do or say, but the ability to choose what not to do or say #freedom
about 29 days ago http://t.co/61KTQknI #Kindle
12 Apr 2012 Force the use of a networking adapter using C# #li #dib0 http://t.co/ZTJOPzOz
9 Apr 2012 Mandriva 2010.2 and USB devices in Virtualbox http://t.co/fwq9gbHB
9 Apr 2012 Execute a http request to you own site with PHP http://t.co/DIvWPrpd
Home Architecture, security and coding Using SSL over TCP as client and server with C#
Using SSL over TCP as client and server with C#
Written by Division by Zero   
Tuesday, 21 June 2011 08:52

In January I wrote a post on HL7 and security. One of the ways to secure the HL7 communication is using SSL. When using SSL all communication will be encrypted and it will be harder to sniff.

To show how to do this, here are some code snippets. This example is based on the example int the MSDN. I used an online X.509 certificate generator to generate the included certificate. This example will go well with the nHapi example solution.

Download the complete solution here.

Here's how the client works. The 'hard' part is to validate the certificate.

static void Main(string[] args)
{
    // Connect as client to port 1300
    string server = "127.0.0.1";
    TcpClient client = new TcpClient(server, 1300);
   
    // Create a secure stream
    using (SslStream sslStream = new SslStream(client.GetStream(), false,
        new RemoteCertificateValidationCallback(ValidateServerCertificate), null))
    {
        sslStream.AuthenticateAsClient(server);
        // ... Send and read data over the stream
    }
    // Disconnect and close the client
    client.Close();
}
// The following method is invoked by the RemoteCertificateValidationDelegate.
// This allows you to check the certificate and accept or reject it
// return true will accept the certificate
public static bool ValidateServerCertificate(object sender, X509Certificate certificate,
    X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    // Accept all certificates
    return true;
}

Even the server is not that hard. You just have to make sure that you install/import the certificate in Windows. If you don't an excpetion will be thrown. Here's a code snippet. Download the complete solution for the working source.

TcpListener listener = new TcpListener(IPAddress.Any, 1300);
listener.Start();
// Wait for a client to connect on TCP port 1300
TcpClient clientSocket = listener.AcceptTcpClient();
X509Certificate certificate = new X509Certificate("..\\path\\tp\\Certificate.pfx", "ThisPasswordIsTheSameForInstallingTheCertificate");
// Create a stream to decrypt the data
using (SslStream sslStream = new SslStream(clientSocket.GetStream()))
{
    sslStream.AuthenticateAsServer(certificate);
    // ... Send and read data over the stream
}
 

Add comment


Security code
Refresh

Professionals built the Titanic. Amateurs the ark. - Unknown


© 2009 - 2012, Division by Zero

Template based on the empire template by joomlashack 

Valid XHTML 1.0 Strict  Valid CSS!  Creative Commons License
This work by Division by Zero is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Netherlands License.