From d6b938668f18171953fd954d17051812422fc1df Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 30 Sep 2020 14:33:15 +0200 Subject: [PATCH 1/5] added reconnecting to the vr server when no tunnel id is found --- Client/Client.cs | 12 ++++++++++-- Client/EngineConnection.cs | 12 ++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Client/Client.cs b/Client/Client.cs index e65204f..5b4c873 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -27,16 +27,24 @@ namespace Client this.client = new TcpClient(); this.connected = false; client.BeginConnect(adress, port, new AsyncCallback(OnConnect), null); - - initEngine(); } private void initEngine() { engineConnection = EngineConnection.INSTANCE; + engineConnection.OnNoTunnelId = retryEngineConnection; if (!engineConnection.Connected) engineConnection.Connect(); } + 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.ReadKey(); + + engineConnection.CreateConnection(); + } + private void OnConnect(IAsyncResult ar) { this.client.EndConnect(ar); diff --git a/Client/EngineConnection.cs b/Client/EngineConnection.cs index 965673b..8b62c10 100644 --- a/Client/EngineConnection.cs +++ b/Client/EngineConnection.cs @@ -7,20 +7,22 @@ using System.Net.Sockets; namespace Client { public delegate void HandleSerial(string message); + public delegate void HandleNoTunnelId(); public sealed class EngineConnection { private static EngineConnection instance = null; private static readonly object padlock = new object(); + public HandleNoTunnelId OnNoTunnelId; private static PC[] PCs = { //new PC("DESKTOP-M2CIH87", "Fabian"), //new PC("T470S", "Shinichi"), //new PC("DESKTOP-DHS478C", "semme"), - new PC("HP-ZBOOK-SEM", "Sem"), + new PC("HP-ZBOOK-SEM", "Sem") //new PC("DESKTOP-TV73FKO", "Wouter"), - new PC("DESKTOP-SINMKT1", "Ralf van Aert"), + //new PC("DESKTOP-SINMKT1", "Ralf van Aert"), //new PC("NA", "Bart") }; @@ -62,6 +64,7 @@ namespace Client { TcpClient client = new TcpClient("145.48.6.10", 6666); stream = client.GetStream(); + initReader(); CreateConnection(); } @@ -69,9 +72,8 @@ namespace Client /// connects to the server and creates the tunnel /// /// the network stream to use - private void CreateConnection() + public void CreateConnection() { - initReader(); WriteTextMessage( "{\r\n\"id\" : \"session/list\",\r\n\"serial\" : \"list\"\r\n}"); @@ -119,6 +121,8 @@ namespace Client if (tunnelId == null) { Write("could not find a valid tunnel id!"); + OnNoTunnelId?.Invoke(); + Connected = false; return; } } From 97f9d863aa07b7af85b11097a361d83469c76d67 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 30 Sep 2020 15:10:47 +0200 Subject: [PATCH 2/5] fix --- Client/EngineConnection.cs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Client/EngineConnection.cs b/Client/EngineConnection.cs index 8b62c10..655e073 100644 --- a/Client/EngineConnection.cs +++ b/Client/EngineConnection.cs @@ -86,7 +86,10 @@ namespace Client // wait until we have a tunnel id while (tunnelId == string.Empty) { } - Write("got tunnel id! " + tunnelId); + if (tunnelId != null) + { + Write("got tunnel id! " + tunnelId); + } mainCommand = new Command(tunnelId); } From 3e5f6e46c4076bad28c797d79fbc23859eb3e742 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Wed, 30 Sep 2020 16:22:57 +0200 Subject: [PATCH 3/5] progress on validation --- Client/Client.cs | 6 ++++- Client/Client.csproj | 2 ++ Client/Program.cs | 3 ++- Hashing/Hasher.cs | 51 +++++++++++++++++++++++++++++++++++++++ Hashing/Hashing.projitems | 14 +++++++++++ Hashing/Hashing.shproj | 13 ++++++++++ ProftaakRH/ProftaakRH.sln | 7 ++++++ Server/Client.cs | 44 ++++++++++++++++++++++++++++++++- Server/Server.csproj | 2 ++ 9 files changed, 139 insertions(+), 3 deletions(-) create mode 100644 Hashing/Hasher.cs create mode 100644 Hashing/Hashing.projitems create mode 100644 Hashing/Hashing.shproj diff --git a/Client/Client.cs b/Client/Client.cs index 5b4c873..d718b1f 100644 --- a/Client/Client.cs +++ b/Client/Client.cs @@ -158,7 +158,11 @@ namespace Client Console.WriteLine("enter password"); string password = Console.ReadLine(); - byte[] message = DataParser.getJsonMessage(DataParser.GetLoginJson(username, password)); + string hashUser = Hashing.Hasher.Encrypt(username); + string hashPassword = Hashing.Hasher.Encrypt(password); + Console.WriteLine("hashed to " + hashUser + " " + hashPassword); + + byte[] message = DataParser.getJsonMessage(DataParser.GetLoginJson(hashUser, hashPassword)); initEngine(); this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); diff --git a/Client/Client.csproj b/Client/Client.csproj index 4ade6d5..6aa0290 100644 --- a/Client/Client.csproj +++ b/Client/Client.csproj @@ -15,4 +15,6 @@ + + diff --git a/Client/Program.cs b/Client/Program.cs index 9853e51..331d8a6 100644 --- a/Client/Program.cs +++ b/Client/Program.cs @@ -2,6 +2,8 @@ using Hardware; using Hardware.Simulators; using RH_Engine; +using System.Security.Cryptography; +using System.Text; namespace Client { @@ -12,7 +14,6 @@ namespace Client Console.WriteLine("Hello World!"); //connect fiets? - Client client = new Client(); diff --git a/Hashing/Hasher.cs b/Hashing/Hasher.cs new file mode 100644 index 0000000..aaea498 --- /dev/null +++ b/Hashing/Hasher.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Security.Cryptography; +using System.Text; + +namespace Hashing +{ + class Hasher + { + static string key = "ProftaakRH-B4"; + public static string Encrypt(string text) + { + 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.CreateEncryptor()) + { + byte[] textBytes = UTF8Encoding.UTF8.GetBytes(text); + byte[] bytes = transform.TransformFinalBlock(textBytes, 0, textBytes.Length); + return Convert.ToBase64String(bytes, 0, bytes.Length); + } + } + } + } + + public static string Decrypt(string cipher) + { + 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); + } + } + } + } + } +} diff --git a/Hashing/Hashing.projitems b/Hashing/Hashing.projitems new file mode 100644 index 0000000..127b36a --- /dev/null +++ b/Hashing/Hashing.projitems @@ -0,0 +1,14 @@ + + + + $(MSBuildAllProjects);$(MSBuildThisFileFullPath) + true + 70277749-d423-4871-b692-2efc5a6ed932 + + + Hashing + + + + + \ No newline at end of file diff --git a/Hashing/Hashing.shproj b/Hashing/Hashing.shproj new file mode 100644 index 0000000..e427bd6 --- /dev/null +++ b/Hashing/Hashing.shproj @@ -0,0 +1,13 @@ + + + + 70277749-d423-4871-b692-2efc5a6ed932 + 14.0 + + + + + + + + diff --git a/ProftaakRH/ProftaakRH.sln b/ProftaakRH/ProftaakRH.sln index f2df07f..42a23ed 100644 --- a/ProftaakRH/ProftaakRH.sln +++ b/ProftaakRH/ProftaakRH.sln @@ -15,7 +15,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Message", "..\Message\Messa EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DokterApp", "..\DokterApp\DokterApp.csproj", "{B150F08B-13DA-4D17-BD96-7E89F52727C6}" EndProject +Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Hashing", "..\Hashing\Hashing.shproj", "{70277749-D423-4871-B692-2EFC5A6ED932}" +EndProject Global + GlobalSection(SharedMSBuildProjectFiles) = preSolution + ..\Hashing\Hashing.projitems*{5759dd20-7a4f-4d8d-b986-a70a7818c112}*SharedItemsImports = 5 + ..\Hashing\Hashing.projitems*{70277749-d423-4871-b692-2efc5a6ed932}*SharedItemsImports = 13 + ..\Hashing\Hashing.projitems*{b1ab6f51-a20d-4162-9a7f-b3350b7510fd}*SharedItemsImports = 5 + EndGlobalSection GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU diff --git a/Server/Client.cs b/Server/Client.cs index 5e385be..46b2025 100644 --- a/Server/Client.cs +++ b/Server/Client.cs @@ -5,6 +5,7 @@ using System.Net.Sockets; using System.Text; using Client; using Newtonsoft.Json; +using System.Security.Cryptography; namespace Server { @@ -19,6 +20,7 @@ namespace Server private SaveData saveData; private string username = null; private DateTime sessionStart; + private const string fileName = "userInfo.dat"; @@ -125,6 +127,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)); } @@ -139,9 +142,48 @@ namespace Server private bool verifyLogin(string username, string password) { - return username == password; + Console.WriteLine("got hashes " + username + password); + Console.WriteLine(Hashing.Hasher.Decrypt(username) + " " + Hashing.Hasher.Decrypt(password)); + + if (!File.Exists(fileName)) + { + Console.WriteLine("file doesnt exist"); + + Console.WriteLine("true"); + return true; + } else + { + string[] usernamesPasswords = File.ReadAllLines(fileName); + + foreach (string s in usernamesPasswords) + { + string[] combo = s.Split(";"); + if (combo[0] == username) + { + Console.WriteLine("true"); + return combo[1] == password; + } + } + + } + Console.WriteLine("false"); + return false; + } + private void newUsers(string username, string password) + { + File.Create(fileName); + using (StreamWriter sw = File.AppendText(fileName)) + { + sw.WriteLine(username + ";" + password); + } + } + + + + + public static string ByteArrayToString(byte[] ba) { StringBuilder hex = new StringBuilder(ba.Length * 2); diff --git a/Server/Server.csproj b/Server/Server.csproj index a91c176..cd7c7da 100644 --- a/Server/Server.csproj +++ b/Server/Server.csproj @@ -13,4 +13,6 @@ + + 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 4/5] 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); } } From cb2435bf53cd88f819e24f8e2f9f773b267c92e5 Mon Sep 17 00:00:00 2001 From: Sem van der Hoeven Date: Fri, 2 Oct 2020 11:48:03 +0200 Subject: [PATCH 5/5] weird fix --- ProftaakRH/ProftaakRH.sln | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ProftaakRH/ProftaakRH.sln b/ProftaakRH/ProftaakRH.sln index 42a23ed..f05bff3 100644 --- a/ProftaakRH/ProftaakRH.sln +++ b/ProftaakRH/ProftaakRH.sln @@ -13,7 +13,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Client", "..\Client\Client. EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Message", "..\Message\Message.csproj", "{9ED6832D-B0FB-4460-9BCD-FAA58863B0CE}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DokterApp", "..\DokterApp\DokterApp.csproj", "{B150F08B-13DA-4D17-BD96-7E89F52727C6}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DokterApp", "..\DokterApp\DokterApp.csproj", "{B150F08B-13DA-4D17-BD96-7E89F52727C6}" EndProject Project("{D954291E-2A0B-460D-934E-DC6B0785DB48}") = "Hashing", "..\Hashing\Hashing.shproj", "{70277749-D423-4871-B692-2EFC5A6ED932}" EndProject