CGC_Codes

cryp data receiver

Jun 5th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.91 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using System.Runtime.Serialization;
  6. using System.Runtime.Serialization.Formatters.Soap;
  7. using System.Security;
  8. using System.Security.Cryptography;
  9. using System.Text;
  10.  
  11. public class CryptoDataRcvr
  12. {
  13.    private SerialEmployee RecvData(NetworkStream strm)
  14.    {
  15.       MemoryStream memstrm = new MemoryStream();
  16.  
  17.       byte[] Key = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
  18.                     0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
  19.       byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
  20.                    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
  21.  
  22.       TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
  23.       CryptoStream csw = new CryptoStream(memstrm, tdes.CreateDecryptor(Key, IV),
  24.                          CryptoStreamMode.Write);
  25.  
  26.       byte[] data = new byte[2048];
  27.       int recv = strm.Read(data, 0, 4);
  28.       int size = BitConverter.ToInt32(data, 0);
  29.       int offset = 0;
  30.       while(size > 0)
  31.       {
  32.          recv = strm.Read(data, 0, size);
  33.          csw.Write(data, offset, recv);
  34.          offset += recv;
  35.          size -= recv;
  36.       }
  37.       csw.FlushFinalBlock();
  38.       IFormatter formatter = new SoapFormatter();
  39.       memstrm.Position = 0;
  40.       SerialEmployee emp = (SerialEmployee)formatter.Deserialize(memstrm);
  41.       memstrm.Close();
  42.       return emp;
  43.    }      
  44.  
  45.    public CryptoDataRcvr()
  46.    {
  47.       TcpListener server = new TcpListener(9050);
  48.       server.Start();
  49.       Console.WriteLine("Waiting for a client...");
  50.       TcpClient client = server.AcceptTcpClient();
  51.       NetworkStream strm = client.GetStream();
  52.  
  53.       SerialEmployee emp1 = RecvData(strm);
  54.       Console.WriteLine("emp1.EmployeeID = {0}", emp1.EmployeeID);
  55.       Console.WriteLine("emp1.LastName = {0}", emp1.LastName);
  56.       Console.WriteLine("emp1.FirstName = {0}", emp1.FirstName);
  57.       Console.WriteLine("emp1.YearsService = {0}", emp1.YearsService);
  58.       Console.WriteLine("emp1.Salary = {0}\n", emp1.Salary);
  59.  
  60.       SerialEmployee emp2 = RecvData(strm);
  61.       Console.WriteLine("emp2.EmployeeID = {0}", emp2.EmployeeID);
  62.       Console.WriteLine("emp2.LastName = {0}", emp2.LastName);
  63.       Console.WriteLine("emp2.FirstName = {0}", emp2.FirstName);
  64.       Console.WriteLine("emp2.YearsService = {0}", emp2.YearsService);
  65.       Console.WriteLine("emp2.Salary = {0}", emp2.Salary);
  66.       strm.Close();
  67.       server.Stop();
  68.    }
  69.  
  70.    public static void Main()
  71.    {
  72.       CryptoDataRcvr cdr = new CryptoDataRcvr();
  73.    }
  74. }
  75.  
  76. [Serializable]
  77. public class SerialEmployee
  78. {
  79.    public int EmployeeID;
  80.    public string LastName;
  81.    public string FirstName;
  82.    public int YearsService;
  83.    public double Salary;
  84.  
  85.    public SerialEmployee()
  86.    {
  87.       EmployeeID = 0;
  88.       LastName = null;
  89.       FirstName = null;
  90.       YearsService = 0;
  91.       Salary = 0.0;
  92.    }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment