Advertisement
Guest User

Untitled

a guest
Jan 28th, 2016
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.90 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows.Forms;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using System.IO;
  9. using System.Security.Cryptography;
  10. using System.Threading;
  11. using System.Runtime.InteropServices;
  12. namespace Login
  13. {
  14.     public class handleClinet
  15.     {
  16.         TcpClient clientSocket;
  17.         string clNo;
  18.         public void startClient(TcpClient inClientSocket, string clineNo)
  19.         {
  20.             this.clientSocket = inClientSocket;
  21.             this.clNo = clineNo;
  22.             Thread ctThread = new Thread(doChat);
  23.             ctThread.Start();
  24.         }
  25.         private void doChat()
  26.         {
  27.             int requestCount = 0;
  28.             byte[] bytesFrom = new byte[10025];
  29.             string dataFromClient = null;
  30.             Byte[] sendBytes = null;
  31.             string serverResponse = null;
  32.             string rCount = null;
  33.             requestCount = 0;
  34.             while ((true))
  35.             {
  36.                 try
  37.                 {
  38.                     requestCount = requestCount + 1;
  39.                     NetworkStream networkStream = clientSocket.GetStream();
  40.                     networkStream.Read(bytesFrom, 0, (int)clientSocket.ReceiveBufferSize);
  41.                     dataFromClient = System.Text.Encoding.ASCII.GetString(bytesFrom);
  42.                     dataFromClient = dataFromClient.Substring(0, dataFromClient.IndexOf("$"));
  43.                     Console.WriteLine(" >> " + "From client-" + clNo + dataFromClient); //clNo wird mit Username des
  44.                     rCount = Convert.ToString(requestCount);                            // Clients ersetzt.
  45.                     serverResponse = "Server to clinet(" + clNo + ") " + rCount;
  46.                     sendBytes = Encoding.ASCII.GetBytes(serverResponse);
  47.                     networkStream.Write(sendBytes, 0, sendBytes.Length);
  48.                     networkStream.Flush();
  49.                     Console.WriteLine(" >> " + serverResponse);
  50.                 }
  51.                 catch (Exception ex)
  52.                 {
  53.                     Console.WriteLine(" >> " + ex.ToString());
  54.                 }
  55.             }
  56.         }
  57.     }
  58.     class Program
  59.     {
  60.         [STAThread]
  61.         static void Main(string[] args)
  62.         {
  63.             Console.Title = "Login";
  64.             Console.ForegroundColor = ConsoleColor.Green;
  65.             Console.Write("id: ");
  66.             string id = Console.ReadLine();
  67.             Console.Write("pw: ");
  68.             string pw = ReadPassword();
  69.             while (true)
  70.             {
  71.                 if (string.Equals(pw, Password(), StringComparison.OrdinalIgnoreCase) && id == "shadowdriz")
  72.                 {
  73.                     Console.WriteLine("Successfully logged in.");
  74.                     System.Threading.Thread.Sleep(1500);
  75.                     Console.Clear();
  76.                     IPAddress ipAddress = Dns.GetHostEntry("localhost").AddressList[0];     //get the local ip
  77.                     TcpListener serverSocket = new TcpListener(ipAddress, 8181);
  78.                     TcpClient clientSocket = default(TcpClient);
  79.                     int counter = 0;
  80.                     serverSocket.Start();
  81.                     Console.WriteLine(">>Server Started. Waiting for Clients to connect.");
  82.                     counter = 0;
  83.                     while (true)
  84.                     {
  85.                         counter += 1;
  86.                         clientSocket = serverSocket.AcceptTcpClient();
  87.                         Console.WriteLine(">> A client just joined the Server. Client Number: " + counter.ToString());
  88.                         handleClinet client = new handleClinet();
  89.                         client.startClient(clientSocket, Convert.ToString(counter));
  90.                     }
  91.                 }
  92.                 else
  93.                     Console.WriteLine("Fuck off!");
  94.                 Form frm = new FuckYou();
  95.                 frm.ShowDialog();
  96.                 break;
  97.             }
  98.         }
  99.         public static string Password()
  100.         {
  101.             using (MD5 md5 = MD5.Create())
  102.             {
  103.                 string password = "";
  104.                 string time = DateTime.Now.ToString(), date = DateTime.Now.ToString();
  105.                 time = time.Substring(time.LastIndexOf(' ') + 1);
  106.                 time = time.Replace(':'.ToString(), "");
  107.                 time = time.Remove(time.Length - 2);
  108.                 date = date.Remove(date.LastIndexOf(' '));
  109.                 date = date.Replace('.'.ToString(), "");
  110.                 string username = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
  111.                 password = GetMD5Hash(md5, username + " " + time + date);
  112.                 return password;
  113.             }
  114.         }
  115.         public static string GetMD5Hash(MD5 md5, string password)
  116.         {
  117.             byte[] data = md5.ComputeHash(Encoding.UTF8.GetBytes(password));
  118.             StringBuilder sb = new StringBuilder();
  119.             for (int i = 0; i < data.Length; i++)
  120.             {
  121.                 sb.Append(data[i].ToString("x2")); //hexdecimal string
  122.             }
  123.             return sb.ToString();
  124.         }
  125.         public static string ReadPassword()
  126.         {
  127.             StringBuilder sb = new StringBuilder();
  128.             while (true)
  129.             {
  130.                 ConsoleKeyInfo cki = Console.ReadKey(true);
  131.                 if (cki.Key == ConsoleKey.Enter)
  132.                 {
  133.                     Console.WriteLine();
  134.                     break;
  135.                 }
  136.                 if (cki.Key == ConsoleKey.Backspace)
  137.                 {
  138.                     if (sb.Length > 0)
  139.                     {
  140.                         Console.Write("\b\0\b");
  141.                         sb.Length--;
  142.                     }
  143.                     continue;
  144.                 }
  145.                 Console.Write('*');
  146.                 sb.Append(cki.KeyChar);
  147.             }
  148.             return sb.ToString();
  149.         }
  150.     }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement