aboutsummaryrefslogtreecommitdiff
blob: 9248f5325874360ffb574fe91bcfdf25034d9c8a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Sockets;
using System.Threading.Tasks;
using DistributedPasswordCracker.Client.Models;

namespace DistributedPasswordCracker.Client
{
    class Program
    {
        const int NumberOfTasks = 5;

        private static StreamReader sr;
        private static StreamWriter sw;

        static void Main(string[] args)
        {
            Console.Write("Connecting to server... ");
            TcpClient clientSocket = new TcpClient("127.0.0.1", 7777);
            NetworkStream ns = clientSocket.GetStream();
            Console.Write("OK\n");
            sr = new StreamReader(ns);
            sw = new StreamWriter(ns);
            sw.AutoFlush = true;

            Console.Write("Getting data from the server... ");
            string data = GetData();
            if (data != "") Console.Write("OK\n");

            Console.Write("Parsing data... ");
            string dictionary = ParseData(data);
            if (dictionary != "") Console.Write("OK\n");

            Console.Write("Splitting dictionary... ");
            string[,] splitDictionary = SplitDictionary(dictionary);
            if (splitDictionary[0,0] != "") Console.Write("OK\n");

            Console.Write("Cracking... \n\n");
            List<Task<string>> tasks = new List<Task<string>>();
            for(int i = 0; i < NumberOfTasks; i++)
            {
                int rowLength = splitDictionary.GetLength(1);
                string[] chunk = new string[rowLength];
                for (int j = 0; j < rowLength; j++)
                    chunk[j] = splitDictionary[i, j];
                tasks.Add(Task.Run(()=>DecryptPassword(chunk)));
            }
            
            var result = Task.WhenAll(tasks).Result;
            if (result.Length != 0)
            {
                string output = "";
                for (int i = 0; i < result.Length; i++)
                    output += result[i];
                
                Console.Write("\nSending results to server...");
                sw.WriteLine(output);
                Console.Write("OK\n");
            }
            else 
            {
                Console.Write("FAILED\n");
                Console.Write("Sending notification to server...");
                sw.WriteLine();
                Console.Write("OK\n");
            }

            Console.Write("\nPress any key to exit...");
            Console.ReadKey();
        }

        private static string GetData()
        {
            string data = "";

            while (true)
            {
                string message = sr.ReadLine();
                if (message != "") data += message + "\n";
                else break;
            }

            return data;
        }

        private static string ParseData(string data)
        {
            var splitData = data.Split('\n');

            if (splitData[0] == "DPCP 1.0")
            {
                splitData[1] = splitData[1].Replace('|', '\n');
                File.WriteAllText("passwords.txt", splitData[1]);
                File.WriteAllText("dictionary.txt", splitData[2]);
            }

            return splitData[2];
        }

        private static string[,] SplitDictionary(string dictionary)
        {
            string[] dicWords = dictionary.Split('|');
            int dicWordsLength = dicWords.Length;
            string[,] splitDictionary = new string[NumberOfTasks,dicWordsLength/NumberOfTasks];
            int offset = 0;
            int j = 0;

            for (int i = 0; i < NumberOfTasks; i++)
            {
                while (j < dicWordsLength/NumberOfTasks)
                {
                    splitDictionary[i, j] = dicWords[j + offset];
                    j++;
                }
                offset += dicWordsLength/NumberOfTasks;
                j = 0;
            }

            return splitDictionary;
        }

        private static string DecryptPassword(string[] dictionary)
        {
            Cracking cracker = new Cracking();
            var result = cracker.RunCracking(dictionary);

            return result;
        }
    }
}