From fedf8c0e5b7d2c2b50d68fdc8add311f6eba90fa Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Fri, 2 Oct 2020 11:43:07 +0200 Subject: [PATCH] added client server login with hashed passwords and usernames --- Client/Client.cs | 12 ++++++------ Client/EngineConnection.cs | 2 +- Hashing/Hasher.cs | 40 ++++++++------------------------------ Server/Client.cs | 26 +++++++++++++++++-------- 4 files changed, 33 insertions(+), 47 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index d718b1f..16d24ad 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -38,8 +38,8 @@ namespace Client private void retryEngineConnection() { - Console.WriteLine("Could not connect to the VR engine. Please make sure you are running the simulation!"); - Console.WriteLine("Press any key to retry connection"); + Console.WriteLine("-- Could not connect to the VR engine. Please make sure you are running the simulation!"); + Console.WriteLine("-- Press any key to retry connecting to the VR engine."); Console.ReadKey(); engineConnection.CreateConnection(); @@ -92,6 +92,7 @@ namespace Client if (responseStatus == "OK") { this.connected = true; + initEngine(); } else { @@ -158,13 +159,12 @@ namespace Client Console.WriteLine("enter password"); string password = Console.ReadLine(); - string hashUser = Hashing.Hasher.Encrypt(username); - string hashPassword = Hashing.Hasher.Encrypt(password); - Console.WriteLine("hashed to " + hashUser + " " + hashPassword); + string hashUser = Hashing.Hasher.HashString(username); + string hashPassword = Hashing.Hasher.HashString(password); byte[] message = DataParser.getJsonMessage(DataParser.GetLoginJson(hashUser, hashPassword)); - initEngine(); + this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); } } diff --git a/Client/EngineConnection.cs b/Client/EngineConnection.cs index 655e073..4905911 100644 --- a/Client/EngineConnection.cs +++ b/Client/EngineConnection.cs @@ -167,7 +167,7 @@ namespace Client stream.Write(res); - Write("sent message " + message); + //Write("sent message " + message); } public void Write(string msg) { diff --git a/Hashing/Hasher.cs b/Hashing/Hasher.cs index aaea498..270faa3 100644 --- a/Hashing/Hasher.cs +++ b/Hashing/Hasher.cs @@ -7,45 +7,21 @@ namespace Hashing { class Hasher { - static string key = "ProftaakRH-B4"; - public static string Encrypt(string text) + public static byte[] GetHash(string input) { - using (var md5 = new MD5CryptoServiceProvider()) + using (HashAlgorithm algorithm = SHA256.Create()) { - using (var tdes = new TripleDESCryptoServiceProvider()) - { - tdes.Key = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key)); - tdes.Mode = CipherMode.ECB; - tdes.Padding = PaddingMode.PKCS7; - - using (var transform = tdes.CreateEncryptor()) - { - byte[] textBytes = UTF8Encoding.UTF8.GetBytes(text); - byte[] bytes = transform.TransformFinalBlock(textBytes, 0, textBytes.Length); - return Convert.ToBase64String(bytes, 0, bytes.Length); - } - } + return algorithm.ComputeHash(Encoding.UTF8.GetBytes(input)); } } - public static string Decrypt(string cipher) + public static string HashString(string input) { - using (var md5 = new MD5CryptoServiceProvider()) - { - using (var tdes = new TripleDESCryptoServiceProvider()) - { - tdes.Key = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key)); - tdes.Mode = CipherMode.ECB; - tdes.Padding = PaddingMode.PKCS7; - - using (var transform = tdes.CreateDecryptor()) - { - byte[] cipherBytes = Convert.FromBase64String(cipher); - byte[] bytes = transform.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length); - return UTF8Encoding.UTF8.GetString(bytes); - } - } + StringBuilder sb = new StringBuilder(); + foreach (byte b in GetHash(input)) { + sb.Append(b.ToString("X2")); } + return sb.ToString(); } } } diff --git a/Server/Client.cs b/Server/Client.cs index 46b2025..79697b7 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -128,7 +128,7 @@ namespace Server Array.Copy(message, 5, payloadbytes, 0, message.Length - 5); dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payloadbytes)); - saveData.WriteDataJSON(Encoding.ASCII.GetString(payloadbytes)); + //saveData.WriteDataJSON(Encoding.ASCII.GetString(payloadbytes)); } else if (DataParser.isRawData(message)) @@ -142,28 +142,37 @@ namespace Server private bool verifyLogin(string username, string password) { - Console.WriteLine("got hashes " + username + password); - Console.WriteLine(Hashing.Hasher.Decrypt(username) + " " + Hashing.Hasher.Decrypt(password)); + Console.WriteLine("got hashes " + username + "\n" + password); + if (!File.Exists(fileName)) { + File.Create(fileName); Console.WriteLine("file doesnt exist"); - + newUsers(username, password); Console.WriteLine("true"); return true; } else { + Console.WriteLine("file exists, located at " + Path.GetFullPath(fileName)); string[] usernamesPasswords = File.ReadAllLines(fileName); + if (usernamesPasswords.Length == 0) + { + newUsers(username, password); + return true; + } foreach (string s in usernamesPasswords) { - string[] combo = s.Split(";"); + string[] combo = s.Split(" "); if (combo[0] == username) { - Console.WriteLine("true"); + Console.WriteLine("correct info"); return combo[1] == password; } + } + Console.WriteLine("combo was not found in file"); } Console.WriteLine("false"); @@ -173,10 +182,11 @@ namespace Server private void newUsers(string username, string password) { - File.Create(fileName); + + Console.WriteLine("creating new entry in file"); using (StreamWriter sw = File.AppendText(fileName)) { - sw.WriteLine(username + ";" + password); + sw.WriteLine(username + " " + password); } }