NetPro'09 - SecureRecv

0 views
Skip to first unread message

Nati

unread,
Sep 21, 2009, 10:41:48 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 SecureRecv
{
    class Program
    {
        static void Main(string[] args)
        {
            TcpListener server = new TcpListener(IPAddress.Parse("127.0.0.1"),9050);
            server.Start();
            Console.WriteLine("Waiting for a client...");
            TcpClient client = server.AcceptTcpClient();
            NetworkStream strm = client.GetStream();

            string str = RecvData(strm);
            Console.WriteLine(str);

            strm.Close();
            server.Stop();
        }

        private static string RecvData(NetworkStream strm)
        {
            MemoryStream memstrm = new MemoryStream();
            byte[] Key = {0x02, 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.CreateDecryptor(Key, IV),
                      CryptoStreamMode.Write);
            byte[] data = new byte[2048];
            //int recv = strm.Read(data, 0, 4);
            //int size = BitConverter.ToInt32(data, 0);
            //int offset = 0;
            //while (size > 0)
            {
                int recv = strm.Read(data, 0, data.Length);
                csw.Write(data, 0, recv);
                //offset += recv;
                //size -= recv;
            }
            csw.FlushFinalBlock();
            memstrm.Position = 0;
            string emp = Encoding.ASCII.GetString(memstrm.GetBuffer(),0,(int)memstrm.Length);
            memstrm.Close();
            return emp;
        }  
    }
}

Reply all
Reply to author
Forward
0 new messages