Skip to content

Commit 5b95c49

Browse files
committed
i dont know why its so laggy on the second part, thinking now... it might be it decrypting soo much it lags
1 parent 3a673c9 commit 5b95c49

3 files changed

Lines changed: 46 additions & 47 deletions

File tree

Additions.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,7 @@ public class ChatListener {
327327
private readonly object lockObject = new object();
328328

329329
public string[] GetChatContent() {
330+
Thread.Sleep(50);
330331
lock (lockObject) {
331332
return ChatContent;
332333
}
@@ -335,10 +336,12 @@ public string[] GetChatContent() {
335336
public void StartListening(string ChatRoom) {
336337

337338
while (true) {
338-
339-
ChatContent = File.ReadLines(@"chats\"+ChatRoom+".txt").ToArray();
340-
341-
System.Threading.Thread.Sleep(100);
339+
Thread.Sleep(50);
340+
try {
341+
ChatContent = File.ReadLines(@"chats\"+ChatRoom+".txt").ToArray();
342+
} catch {
343+
Console.Write("");
344+
}
342345
}
343346
}
344347
}

User.cs

Lines changed: 35 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static bool Exists_PublicKey(string Value) {
6969

7070

7171

72-
public void Generate() {
72+
public void Generate(int Width = 25, int Height = 25) {
7373
if (User.Exists(this.Username)) {
7474
return;
7575
}
@@ -79,7 +79,7 @@ public void Generate() {
7979
SQLiteConnection connection = new SQLiteConnection(connectionString);
8080
connection.Open();
8181

82-
string sqlCommand = "INSERT INTO Main (Username, PublicKey) VALUES ('"+this.Username+"', '"+this.Key.Public+"');";
82+
string sqlCommand = "INSERT INTO Main (Username, PublicKey, Width, Height) VALUES ('"+this.Username+"', '"+this.Key.Public+"', '"+Width+"', '"+Height+"');";
8383
SQLiteCommand command = new SQLiteCommand(sqlCommand, connection);
8484

8585
command = new SQLiteCommand(sqlCommand, connection);
@@ -88,6 +88,7 @@ public void Generate() {
8888
connection.Close();
8989
}
9090
public void Generate_Chat(string Name) {
91+
9192
string Name1Hex = Encode.ByteArrayToHexString(Encoding.ASCII.GetBytes(this.Username));
9293
string Name2Hex = Encode.ByteArrayToHexString(Encoding.ASCII.GetBytes(Name));
9394

@@ -106,38 +107,30 @@ public void Generate_Chat(string Name) {
106107
{
107108
writer.WriteLine(
108109
Name+
109-
":"+// SymmetricKey.Item1+
110-
Encode.ByteArrayToHexString(Encoding.ASCII.GetBytes(
111-
this.Key.Encrypt(SymmetricKey.Item1, User.Read_User_PublicKey(Name))
112-
))+
113-
":"+// SymmetricKey.Item2
114-
Encode.ByteArrayToHexString(Encoding.ASCII.GetBytes(
115-
this.Key.Encrypt(SymmetricKey.Item2, User.Read_User_PublicKey(Name))
116-
))
110+
":"+
111+
this.Key.Encrypt(SymmetricKey.Item1, User.Read_User_PublicKey(Name))+
112+
":"+
113+
this.Key.Encrypt(SymmetricKey.Item2, User.Read_User_PublicKey(Name))
117114
);
118115

119116
// User.Read_User_PublicKey(this.Username);
120117

121118
writer.WriteLine(
122119
this.Username+
123-
":"+// SymmetricKey.Item1+
124-
Encode.ByteArrayToHexString(Encoding.ASCII.GetBytes(
125-
this.Key.Encrypt(SymmetricKey.Item1, User.Read_User_PublicKey(this.Username))
126-
))+
127-
":"+// SymmetricKey.Item2
128-
Encode.ByteArrayToHexString(Encoding.ASCII.GetBytes(
129-
this.Key.Encrypt(SymmetricKey.Item2, User.Read_User_PublicKey(this.Username))
130-
))
120+
":"+
121+
this.Key.Encrypt(SymmetricKey.Item1, User.Read_User_PublicKey(Name))+
122+
":"+
123+
this.Key.Encrypt(SymmetricKey.Item2, User.Read_User_PublicKey(Name))
131124
);
132125

133126
// User.Read_User_PublicKey(Name);
134127
}
135128

136-
using (StreamWriter writer = new StreamWriter(@"chats\Write.txt"))
137-
{
138-
writer.WriteLine(SymmetricKey.Item1);
139-
writer.WriteLine(SymmetricKey.Item2);
140-
}
129+
// using (StreamWriter writer = new StreamWriter(@"chats\Write.txt"))
130+
// {
131+
// writer.WriteLine(SymmetricKey.Item1);
132+
// writer.WriteLine(SymmetricKey.Item2);
133+
// }
141134
}
142135
public static void Generate_Database() {
143136
while (!System.IO.File.Exists("UserList.db"))
@@ -146,7 +139,7 @@ public static void Generate_Database() {
146139
SQLiteConnection connection = new SQLiteConnection(connectionString);
147140
connection.Open();
148141

149-
string query = "CREATE TABLE Main (Username TEXT, PublicKey TEXT)";
142+
string query = "CREATE TABLE Main (Username TEXT, PublicKey TEXT, Width INTEGER, Height INTEGER)";
150143
SQLiteCommand command = new SQLiteCommand(query, connection);
151144
command.ExecuteNonQuery();
152145
connection.Close();
@@ -243,24 +236,26 @@ public Tuple<string, string> Read_Chat_SymmetricKey(string SelectedChat) {
243236

244237
iv = EncryptedMessages[0].Split(':')[2];
245238

239+
// Console.WriteLine(EncryptedMessages[0]);
240+
246241
} else {
247242

248243
SymmetricKey = EncryptedMessages[1].Split(':')[1];
249244

250245
iv = EncryptedMessages[1].Split(':')[2];
251246

247+
// Console.WriteLine(EncryptedMessages[1]);
248+
252249
}
253250

254251

255252

256-
byte[] encryptedData = Encode.HexStringToByteArray(SymmetricKey);
257-
string encryptedDataString = Convert.ToBase64String(encryptedData);
258-
string decryptedData = this.Key.Decrypt(encryptedDataString, this.Key.Private);
253+
string encryptedData = SymmetricKey;
254+
string decryptedData = this.Key.Decrypt(encryptedData, this.Key.Private);
259255
SymmetricKey = decryptedData;
260256

261-
encryptedData = Encode.HexStringToByteArray(iv);
262-
encryptedDataString = Convert.ToBase64String(encryptedData);
263-
decryptedData = this.Key.Decrypt(encryptedDataString, this.Key.Private);
257+
encryptedData = iv;
258+
decryptedData = this.Key.Decrypt(encryptedData, this.Key.Private);
264259
iv = decryptedData;
265260

266261

@@ -278,11 +273,11 @@ public Tuple<string, string> Read_Chat_SymmetricKey(string SelectedChat) {
278273
// Console.WriteLine(SymmetricKey);
279274
// Console.WriteLine(iv);
280275

281-
using (StreamWriter writer = new StreamWriter(@"chats\Read.txt"))
282-
{
283-
writer.WriteLine(SymmetricKey);
284-
writer.WriteLine(iv);
285-
}
276+
// using (StreamWriter writer = new StreamWriter(@"chats\Read.txt"))
277+
// {
278+
// writer.WriteLine(SymmetricKey);
279+
// writer.WriteLine(iv);
280+
// }
286281

287282
return new Tuple<string,string>(SymmetricKey,iv);
288283
}
@@ -312,7 +307,7 @@ public static string Read_User_PublicKey(string User) {
312307
//System.Threading.Thread.Sleep(5000);
313308
string TempString = rdr.GetString(0);
314309
rdr.Close();
315-
Console.WriteLine(TempString);
310+
// Console.WriteLine(TempString);
316311
// System.Threading.Thread.Sleep(5000);
317312
// System.Threading.Thread.Sleep(5000);
318313
// System.Threading.Thread.Sleep(5000);
@@ -387,6 +382,10 @@ public class Chats
387382
{
388383

389384
public static string[][] All(string folder) {
385+
if (!Directory.Exists(folder)) {
386+
Directory.CreateDirectory(folder);
387+
System.Threading.Thread.Sleep(100);
388+
}
390389
FileInfo[] files = new DirectoryInfo(folder).GetFiles("*.txt");
391390

392391
string[] fileNames = new string[files.Length];

program.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ public static void PrivateChatLoop(string SelectedChat, Tuple<int, int> BoardDim
269269
while (true)
270270
{
271271

272-
Thread.Sleep(100);
272+
Thread.Sleep(50);
273273

274274
if (TextListener.GetEndProgram()) {
275275
if (!string.IsNullOrEmpty(TypedText)) {
@@ -288,16 +288,13 @@ public static void PrivateChatLoop(string SelectedChat, Tuple<int, int> BoardDim
288288
}
289289
}
290290

291-
if ((TypedText != TextListener.GetTypedText()) || !ChatMessages.SequenceEqual(ChatListener.GetChatContent())) {
291+
//if (TypedText != TextListener.GetTypedText() || ChatMessages.SequenceEqual(ChatListener.GetChatContent())) {
292292

293293
TypedText = TextListener.GetTypedText();
294294
ChatMessages = ChatListener.GetChatContent();
295295

296-
User.User.Display.Content(CurrentUser.Read_Messages_Chat(SelectedChat),BoardDimensions,TypedText, false);
297-
}
298-
299-
300-
// Write_Messages_Chat(Tuple<string, string> SymmetricKey, string ChatName, string Content)
296+
User.User.Display.Content(CurrentUser.Read_Messages_Chat(SelectedChat),BoardDimensions,TypedText, true);
297+
//}
301298
}
302299
}
303300
}

0 commit comments

Comments
 (0)