NetPro'09 - SecureSend

0 views
Skip to first unread message

Nati

unread,
Sep 21, 2009, 10:43:03 AM9/21/09
to net...@googlegroups.com
using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Security;
using System.Security.Cryptography;
using System.Text;

namespace SecureSend
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpClient client = new TcpClient("127.0.0.1", 9050);
            NetworkStream strm = client.GetStream();
            SendData(strm, "Hello");

     

            strm.Close();
            client.Close();
        }
        private static void SendData(NetworkStream strm, string phrase)
        {
            MemoryStream memstrm = new MemoryStream();
            byte[] Key = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
            byte[] IV = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 };
            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            CryptoStream csw = new CryptoStream(memstrm, tdes.CreateEncryptor(Key, IV), CryptoStreamMode.Write);
            csw.Write(Encoding.ASCII.GetBytes(phrase), 0, phrase.Length);
            csw.FlushFinalBlock();
            byte[] data = memstrm.GetBuffer();
            int memsize = (int)memstrm.Length;
            //byte[] size = BitConverter.GetBytes(memsize);
            //strm.Write(size, 0, 4);
            strm.Write(data, 0, (int)memsize);
            strm.Flush();
            csw.Close();
            memstrm.Close();
        }
    }
}

Reply all
Reply to author
Forward
0 new messages