-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
246 lines (233 loc) · 9.25 KB
/
Program.cs
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace atividade_sha_256
{
class Program
{
static TcpListener server = null;
static string mode = "";
static void Main(string[] args)
{
var ip = GetLocalIPAddress();
IPAddress ipAddress = Dns.GetHostEntry(ip).AddressList[0];
mode = args[0];
if (mode == "master")
{
var hashToFind = args[1];
var ips = new List<string>();
if (args.Length > 2)
{
ips.Add(args[2]);
}
if (args.Length > 3)
{
ips.Add(args[3]);
}
if (args.Length > 4)
{
ips.Add(args[4]);
}
Int64 temp = 0;
Int64 max = 999999999;
Int64 maxNode = max / Convert.ToInt64(ips.Count);
Console.WriteLine("Nodos configurados: " + ips.Count);
foreach (var ipTemp in ips)
{
try
{
TcpClient s = new TcpClient(ip, 8081);
var send = "{\"min\":" + temp + ",\"max\":" + (temp + maxNode) + ",\"hash\":\"" + hashToFind + "\",\"master\":\"" + ip + "\"}";
s.Client.Send(System.Text.Encoding.ASCII.GetBytes(send));
Console.WriteLine(ipTemp + " conectou.");
temp += maxNode;
}
catch (System.Exception)
{
Console.WriteLine(ipTemp + " recusou.");
}
}
Server(ipAddress, 8082);
}
else
{
Console.WriteLine("IP: " + ip);
Server(ipAddress, 8081);
}
}
public static string GetLocalIPAddress()
{
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
return ip.ToString();
}
}
throw new Exception("No network adapters with an IPv4 address in the system!");
}
public static void Server(IPAddress ip, int port)
{
server = new TcpListener(ip, port);
server.Start();
StartListener();
}
public static void StartListener()
{
try
{
if (mode == "master")
{
Console.WriteLine("Esperando pelo resultado...");
TcpClient client = server.AcceptTcpClient();
Thread t = new Thread(new ParameterizedThreadStart(HandleDeivce));
t.Start(client);
}
else
{
Console.WriteLine("Esperando pela conexão...");
TcpClient client = server.AcceptTcpClient();
Console.WriteLine("Conectado!");
Thread t = new Thread(new ParameterizedThreadStart(HandleDeivce));
t.Start(client);
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException: {0}", e);
server.Stop();
}
}
public static void HandleDeivce(Object obj)
{
TcpClient client = (TcpClient)obj;
var stream = client.GetStream();
string imei = String.Empty;
string data = null;
Byte[] bytes = new Byte[256];
int i;
try
{
while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
{
data = Encoding.ASCII.GetString(bytes, 0, i);
if (!String.IsNullOrEmpty(data))
{
if (mode == "master")
{
var receivedData = JsonConvert.DeserializeObject<dynamic>(data);
Console.WriteLine("Resultado encontrado: " + receivedData);
Environment.Exit(0);
}
else
{
var receivedData = JsonConvert.DeserializeObject<dynamic>(data);
var min = Convert.ToInt64(receivedData.min);
var max = Convert.ToInt64(receivedData.max);
var hashToFind = Convert.ToString(receivedData.hash);
var masterIp = Convert.ToString(receivedData.master);
var result = decript(min, max, hashToFind);
TcpClient s = new TcpClient(masterIp, 8082);
s.Client.Send(System.Text.Encoding.ASCII.GetBytes(Convert.ToString(result)));
Environment.Exit(0);
}
}
}
}
catch (Exception e)
{
Console.WriteLine("Exception: {0}", e.ToString());
client.Close();
}
}
internal static Int64 decript(Int64 min, Int64 max, string hashToFind)
{
Console.WriteLine("HASH: " + hashToFind);
var dbPath = Path.Combine(Directory.GetCurrentDirectory(), "database.sqlite");
var connectionString = string.Format("Data Source={0};Version=3;", dbPath);
using (var con = new SQLiteConnection(connectionString))
{
con.Open();
using (var transaction = con.BeginTransaction())
{
using (var cmd = new SQLiteCommand(con))
{
cmd.CommandText = "CREATE TABLE IF NOT EXISTS decripted (id INTEGER PRIMARY KEY AUTOINCREMENT, value text, hash varchar(64));";
cmd.ExecuteNonQuery();
}
transaction.Commit();
}
}
using (var con = new SQLiteConnection(connectionString))
{
con.Open();
using (var cmd = new SQLiteCommand(con))
{
cmd.CommandText = "SELECT value FROM decripted WHERE hash = @hash";
cmd.Parameters.AddWithValue("@hash", hashToFind);
var r = cmd.ExecuteReader();
while (r.Read())
{
var cachedResult = Convert.ToString(r["value"]);
Console.WriteLine("Encontrado em cache: " + cachedResult);
return Convert.ToInt64(cachedResult);
}
}
}
using (var con = new SQLiteConnection(connectionString))
{
con.Open();
using (var cmd = new SQLiteCommand(con))
{
cmd.CommandText = "SELECT max(value) as value FROM decripted";
string maxCached = cmd.ExecuteScalar().ToString();
if (!String.IsNullOrEmpty(maxCached))
{
min = Convert.ToInt64(maxCached);
}
}
}
Int64 result = 0;
var superCon = new SQLiteConnection(connectionString);
superCon.Open();
var superTransaction = superCon.BeginTransaction();
Console.WriteLine("Iniciando força bruta em " + min + " até " + max);
for (Int64 i = min; i <= max; i++)
{
string hash = "";
var crypt = new SHA256Managed();
byte[] crypto = crypt.ComputeHash(Encoding.ASCII.GetBytes(i.ToString()));
foreach (byte theByte in crypto)
{
hash += theByte.ToString("x2");
}
using (var cmd = new SQLiteCommand(superCon))
{
cmd.CommandText = "INSERT INTO decripted (value,hash) VALUES (@value, @hash)";
cmd.Parameters.AddWithValue("@value", i);
cmd.Parameters.AddWithValue("@hash", hash);
cmd.ExecuteNonQuery();
}
if (hash == hashToFind)
{
result = i;
break;
}
}
Console.WriteLine("Encontrado: " + result.ToString());
Console.WriteLine("Persistindo cache...");
superTransaction.Commit();
Console.WriteLine("Concluido.");
return result;
}
}
}