CGC_Codes

Cryp data sender

Jun 5th, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.32 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 CryptoDataSender
  12. {
  13.    private void SendData(NetworkStream strm, SerialEmployee emp)
  14.    {
  15.       IFormatter formatter = new SoapFormatter();
  16.       MemoryStream memstrm = new MemoryStream();
  17.  
  18.       byte[] Key = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
  19.       byte[] IV = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16};
  20.  
  21.       TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
  22.       CryptoStream csw = new CryptoStream(memstrm, tdes.CreateEncryptor(Key, IV), CryptoStreamMode.Write);
  23.  
  24.       formatter.Serialize(csw, emp);
  25.       csw.FlushFinalBlock();
  26.       byte[] data = memstrm.GetBuffer();
  27.       int memsize = (int)memstrm.Length;
  28.       byte[] size = BitConverter.GetBytes(memsize);
  29.       strm.Write(size, 0, 4);
  30.       strm.Write(data, 0, (int)memsize);
  31.       strm.Flush();
  32.       csw.Close();
  33.       memstrm.Close();
  34.    }
  35.  
  36.    public CryptoDataSender()
  37.    {
  38.       SerialEmployee emp1 = new SerialEmployee();
  39.       SerialEmployee emp2 = new SerialEmployee();
  40.  
  41.       emp1.EmployeeID = 1;
  42.       emp1.LastName = "Blum";
  43.       emp1.FirstName = "Katie Jane";
  44.       emp1.YearsService = 12;
  45.       emp1.Salary = 35000.50;
  46.  
  47.       emp2.EmployeeID = 2;
  48.       emp2.LastName = "Blum";
  49.       emp2.FirstName = "Jessica";
  50.       emp2.YearsService = 9;
  51.       emp2.Salary = 23700.30;
  52.  
  53.       TcpClient client = new TcpClient("127.0.0.1", 9050);
  54.       NetworkStream strm = client.GetStream();
  55.  
  56.       SendData(strm, emp1);
  57.       SendData(strm, emp2);
  58.       strm.Close();
  59.       client.Close();
  60.    }
  61.  
  62.    public static void Main()
  63.    {
  64.       CryptoDataSender cds = new CryptoDataSender();
  65.    }
  66. }
  67.  
  68.  
  69. [Serializable]
  70. public class SerialEmployee
  71. {
  72.    public int EmployeeID;
  73.    public string LastName;
  74.    public string FirstName;
  75.    public int YearsService;
  76.    public double Salary;
  77.  
  78.    public SerialEmployee()
  79.    {
  80.       EmployeeID = 0;
  81.       LastName = null;
  82.       FirstName = null;
  83.       YearsService = 0;
  84.       Salary = 0.0;
  85.    }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment