Facebook
From Filip Wicha, 5 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 233
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading;
  5.  
  6. namespace zad3._1
  7. {
  8.     class Program
  9.     {
  10.         static public string password;
  11.         static public string password2;
  12.  
  13.         static List<String> buffor = new List<string>();
  14.  
  15.         static Thread producer;
  16.         static Thread consumer;
  17.         static Thread temp; //bruteforce
  18.  
  19.         static void generujHaslo(int length)
  20.         {
  21.             Random random = new Random();
  22.             char[] tab = new char[length];
  23.  
  24.             for (int i = 0; i < length; i++)
  25.             {
  26.                 tab[i] = (char)random.Next(97, 122);
  27.             }
  28.  
  29.             password = new String(tab);
  30.  
  31.             Console.WriteLine(password);
  32.         }
  33.  
  34.  
  35.         static public void tempfunction()
  36.         {
  37.             buffor.Add("a");
  38.  
  39.             producer = new Thread(new ThreadStart(producerfunction));
  40.             consumer = new Thread(new ThreadStart(consumerfunction));
  41.  
  42.             producer.Start();
  43.             consumer.Start();
  44.  
  45.             while (true)
  46.             {
  47.                 if (!producer.IsAlive)
  48.                 {
  49.                     consumer.Abort();
  50.                     producer.Abort();
  51.                     break;
  52.                 }
  53.             }
  54.         }
  55.  
  56.         static public void producerfunction()
  57.         {
  58.             char[] tab = new char[password.Length];
  59.             Random random = new Random();
  60.             while (true)
  61.             {
  62.                 for (int j = 0; j < password.Length; j++)
  63.                 {
  64.                     tab[j] = (char)random.Next(97, 122);
  65.                 }
  66.                 password2 = new String(tab);
  67.                 Console.WriteLine("Producer: generated password  " + password2);
  68.                 buffor.Add(password2);
  69.                 Console.WriteLine("Buffor: Password added.      Capacity: " + buffor.Capacity);
  70.  
  71.                 Thread.Sleep(1);
  72.  
  73.             }
  74.         }
  75.  
  76.         static public void consumerfunction()
  77.         {
  78.             while (true)
  79.             {
  80.  
  81.                 if (buffor.First() != password)
  82.                 {
  83.                     Console.WriteLine("Consumer: Wrong password " + buffor.First());
  84.                     buffor.Remove(buffor.First());
  85.                 }
  86.                 else
  87.                 {
  88.                     Console.WriteLine("Consumer: Password Guessed - " + buffor.First());
  89.                     producer.Abort();
  90.  
  91.                 }
  92.                 Thread.Sleep(2);
  93.  
  94.             }
  95.         }
  96.  
  97.         static void Main(string[] args)
  98.         {
  99.             generujHaslo(3);
  100.  
  101.             temp = new Thread(new ThreadStart(tempfunction));
  102.             temp.Start();
  103.  
  104.             Console.ReadLine();
  105.         }
  106.     }
  107. }