From be4eada18a6a03662df279d595330cbe0c463c52 Mon Sep 17 00:00:00 2001 From: SCM6WE Date: Thu, 23 Sep 2021 12:55:31 +0200 Subject: [PATCH 1/3] stuff --- PolyChat/Controller.cs | 104 +++++++++++++++++++++++---------- PolyChat/MainPage.xaml.cs | 4 +- PolyChat/Models/ChatMessage.cs | 5 -- 3 files changed, 74 insertions(+), 39 deletions(-) diff --git a/PolyChat/Controller.cs b/PolyChat/Controller.cs index ec9ee05..b7573d5 100644 --- a/PolyChat/Controller.cs +++ b/PolyChat/Controller.cs @@ -37,8 +37,6 @@ namespace PolyChat { UIController = uiController; OwnIP = getIP(); - loadChats(); - //SaveChats("10", "{das ist ein test}"); Serve(); } @@ -55,7 +53,7 @@ namespace PolyChat { Connections.Add(ip, new Connection(ip, PORT, Data => OnMessage(ip, Data), CloseChat)); } - + } private void Serve() @@ -74,6 +72,7 @@ namespace PolyChat { Debug.WriteLine("--- initial packet received ---"); string ForeignIp = data[0].ToString(); + Debug.WriteLine($"--- this ip was in the inital packet: {ForeignIp} ---"); if (isInConnections(ForeignIp)) { @@ -171,6 +170,7 @@ namespace PolyChat /// public void loadChats() { + //TODO: also load chatlogs when user tries to connect //load dir and create if non existant if (Directory.Exists("U:\\PolyChat\\Saves")) { @@ -200,9 +200,43 @@ namespace PolyChat UIController.OnIncomingMessages(ip, jsonArr); } } - } + /* + public void loadChat(String ip) + { + //TODO: also load chatlogs when user tries to connect + //load dir and create if non existant + if (Directory.Exists("U:\\PolyChat\\Saves")) + { + Debug.WriteLine("--Path exists.--"); + } + else + { + Directory.CreateDirectory("U:\\PolyChat\\Saves"); + Debug.WriteLine("--Path Created--."); + } + + //go through all files and send ip and json array to ui + String[] filepaths = Directory.GetFiles("U:\\PolyChat\\Saves"); + if (filepaths.Length > 0) + { + Debug.WriteLine("---Loading Saves"); + foreach (String path in filepaths) + { + Debug.WriteLine($"--{path}"); + String jsonArr = File.ReadAllText(path); + String ip = Path.GetFileName(path); + ip = ip.Substring(0, ip.Length - 4); + Debug.WriteLine($"-{ip}"); + Debug.WriteLine(jsonArr); + Connect(ip); + UIController.OnIncomingConnection(ip); + UIController.OnIncomingMessages(ip, jsonArr); + } + } + } + */ /// /// Saves incoming chat message to /// @@ -215,43 +249,51 @@ namespace PolyChat //writing flag setzen oder auch in der datei selbst ne flag setzen new Thread(() => { - if (File.Exists($"U:\\PolyChat\\Saves\\{ip}.txt")) + //breaking if namechange + JObject obj = JObject.Parse(json); + if (!obj["type"].ToString().Equals("username")) { - Debug.WriteLine("--File allready exists--"); - //check for integraty of file - string output = File.ReadAllText($"U:\\PolyChat\\Saves\\{ip}.txt"); - Debug.WriteLine($"---{output}---"); - if (output.Substring(0, 1).Equals("[") && output.Substring(output.Length - 1, 1).Equals("]")) + //adding timestamp + obj = JObject.Parse(json); + obj.Add(new JProperty("timestamp", timeStamp)); + json = obj.ToString(); + if (File.Exists($"U:\\PolyChat\\Saves\\{ip}.txt")) { - Debug.WriteLine("--adding new chatmessage--"); - //structure intact - JObject obj = JObject.Parse(json); - //save new chat - String saved = output.Substring(0, output.Length - 1); - output = saved + ", " + json + " ]"; - File.Delete($"U:\\PolyChat\\Saves\\{ip}.txt"); - File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", output); + Debug.WriteLine("--File allready exists--"); + //check for integraty of file + string output = File.ReadAllText($"U:\\PolyChat\\Saves\\{ip}.txt"); + Debug.WriteLine($"---{output}---"); + if (output.Substring(0, 1).Equals("[") && output.Substring(output.Length - 1, 1).Equals("]")) + { + Debug.WriteLine("--adding new chatmessage--"); + //structure intact + //save new chat + String saved = output.Substring(0, output.Length - 1); + output = saved + ", " + json + " ]"; + File.Delete($"U:\\PolyChat\\Saves\\{ip}.txt"); + File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", output); + } + else + { + Debug.WriteLine("--Structure not intact--"); + Debug.WriteLine("--redoing file--"); + //structure not intact + //redo file + File.Delete($"U:\\PolyChat\\Saves\\{ip}.txt"); + File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", $"[ {json} ]"); + } } else { - Debug.WriteLine("--Structure not intact--"); - Debug.WriteLine("--redoing file--"); - //structure not intact - //redo file - File.Delete($"U:\\PolyChat\\Saves\\{ip}.txt"); + Debug.WriteLine("--Creating new File--"); + //setup file File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", $"[ {json} ]"); } } - else - { - Debug.WriteLine("--Creating new File--"); - //setup file - File.WriteAllText($"U:\\PolyChat\\Saves\\{ip}.txt", $"[ {json} ]"); - } }).Start(); } - private void encode(string json) + private void encode(string json) { byte[] plaintext = Encoding.UTF8.GetBytes(json); byte[] entropy = new byte[20]; @@ -260,8 +302,6 @@ namespace PolyChat rng.GetBytes(entropy); } - /*byte[] ciphertext = ProtectedData.Protect(plaintext, entropy, - DataProtectionScope.CurrentUser);*/ } } } diff --git a/PolyChat/MainPage.xaml.cs b/PolyChat/MainPage.xaml.cs index c3c3b84..2f4788a 100644 --- a/PolyChat/MainPage.xaml.cs +++ b/PolyChat/MainPage.xaml.cs @@ -156,8 +156,8 @@ namespace PolyChat new ChatMessage( origin, item["type"].ToString(), - item["content"].ToString(), - DateTime.Parse(item["timestamp"].ToString()) + item["content"].ToString()//, + //DateTime.Parse(item["timestamp"].ToString()) ) ); } diff --git a/PolyChat/Models/ChatMessage.cs b/PolyChat/Models/ChatMessage.cs index 4056559..7428068 100644 --- a/PolyChat/Models/ChatMessage.cs +++ b/PolyChat/Models/ChatMessage.cs @@ -11,12 +11,7 @@ namespace PolyChat.Models public string Content; public DateTime TimeStamp; public readonly bool Foreign; - - public ChatMessage() - { - - } /// /// Create own Message (directly sent) /// From f07793bb93409030ea6576186f15b1869d39843f Mon Sep 17 00:00:00 2001 From: "Felix Hartmann (PEA3-Fe-FI)" Date: Thu, 23 Sep 2021 13:29:04 +0200 Subject: [PATCH 2/3] only open dialog if there are no popups already open --- PolyChat/Controller.cs | 1 - PolyChat/MainPage.xaml.cs | 29 +++++++++++++++++++++++++---- PolyChat/PolyChat.csproj | 3 --- PolyChat/Views/Dialog.xaml.cs | 3 ++- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/PolyChat/Controller.cs b/PolyChat/Controller.cs index 5f114e6..4676a5b 100644 --- a/PolyChat/Controller.cs +++ b/PolyChat/Controller.cs @@ -8,7 +8,6 @@ using PolyChat.Models; using System.IO; using System.Threading; using System; -using System.Text.Json; using System.Text; using System.Security.Cryptography; diff --git a/PolyChat/MainPage.xaml.cs b/PolyChat/MainPage.xaml.cs index 06a9db4..0c9442d 100644 --- a/PolyChat/MainPage.xaml.cs +++ b/PolyChat/MainPage.xaml.cs @@ -7,11 +7,11 @@ using System.Collections.ObjectModel; using System.Diagnostics; using System.Linq; using System.Text.Json; -using System.Threading.Tasks; +using Windows.Foundation; using Windows.UI.Core; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; -using Windows.UI.Xaml.Data; +using Windows.UI.Xaml.Media; // The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 @@ -81,7 +81,7 @@ namespace PolyChat public async void OnOpenNewChatDialog(object sender = null, RoutedEventArgs e = null) { NewChatDialog dialog = new NewChatDialog(); - var result = await dialog.ShowAsync(); + var result = await SafelyOpenDialog(dialog); if (result == ContentDialogResult.Primary) { string ip = IP.GetIPFromCode(dialog.getValue()); @@ -97,7 +97,7 @@ namespace PolyChat public async void OnOpenEditUsernameDialog(object sender = null, RoutedEventArgs e = null) { EditUsernameDialog dialog = new EditUsernameDialog(username); - var result = await dialog.ShowAsync(); + var result = await SafelyOpenDialog(dialog); if (result == ContentDialogResult.Primary) { username = dialog.getValue(); @@ -204,6 +204,27 @@ namespace PolyChat } } + public static IAsyncOperation SafelyOpenDialog(Dialog d) + { + if(VisualTreeHelper.GetOpenPopups(Window.Current).Count == 0) + return d.ShowAsync(); + return null; + } + + public static IAsyncOperation SafelyOpenDialog(NewChatDialog d) + { + if (VisualTreeHelper.GetOpenPopups(Window.Current).Count == 0) + return d.ShowAsync(); + return null; + } + + public static IAsyncOperation SafelyOpenDialog(EditUsernameDialog d) + { + if (VisualTreeHelper.GetOpenPopups(Window.Current).Count == 0) + return d.ShowAsync(); + return null; + } + // UPDATE FUNCTIONS FOR UI PLACEHOLDERS private void updateNoChatsPlaceholder() diff --git a/PolyChat/PolyChat.csproj b/PolyChat/PolyChat.csproj index 12041a4..42b0df9 100644 --- a/PolyChat/PolyChat.csproj +++ b/PolyChat/PolyChat.csproj @@ -203,9 +203,6 @@ 5.0.2 - - - 14.0 diff --git a/PolyChat/Views/Dialog.xaml.cs b/PolyChat/Views/Dialog.xaml.cs index f342693..331fb6f 100644 --- a/PolyChat/Views/Dialog.xaml.cs +++ b/PolyChat/Views/Dialog.xaml.cs @@ -3,6 +3,7 @@ using PolyChat.Util; using System; using System.Collections.ObjectModel; using System.Linq; +using System.Threading; using Windows.Foundation; using Windows.UI.Core; using Windows.UI.Popups; @@ -34,7 +35,7 @@ namespace PolyChat.Views Primary = primary.Action; Secondary = secondary.Action; // show - ShowDialogAsync(); + MainPage.SafelyOpenDialog(this); } private void setType(string type, string message) From 665d289d3a8fd86b8fd2cdaedd7956ecacfe6b1b Mon Sep 17 00:00:00 2001 From: SCM6WE Date: Thu, 23 Sep 2021 13:33:03 +0200 Subject: [PATCH 3/3] brgin of encryption --- PolyChat/Controller.cs | 52 ++++----------------- PolyChat/Models/FileManager.cs | 84 ++++++++++++++++++++++++++++++++++ PolyChat/PolyChat.csproj | 1 + 3 files changed, 95 insertions(+), 42 deletions(-) create mode 100644 PolyChat/Models/FileManager.cs diff --git a/PolyChat/Controller.cs b/PolyChat/Controller.cs index 5f114e6..1d0c322 100644 --- a/PolyChat/Controller.cs +++ b/PolyChat/Controller.cs @@ -11,6 +11,9 @@ using System; using System.Text.Json; using System.Text; using System.Security.Cryptography; +using Windows.Security.Cryptography.Core; +using Windows.Security.Cryptography; +using Windows.Storage.Streams; namespace PolyChat { @@ -37,6 +40,8 @@ namespace PolyChat { UIController = uiController; OwnIP = getIP(); + loadChats(); + encode("test"); Serve(); } @@ -168,6 +173,8 @@ namespace PolyChat /// /// sends chatlogs as json array to uiController wit corrosponding ip + /// + /// in ui when chat is clicked connection gets established /// /// public void loadChats() @@ -203,41 +210,6 @@ namespace PolyChat } } - /* - public void loadChat(String ip) - { - //TODO: also load chatlogs when user tries to connect - //load dir and create if non existant - if (Directory.Exists("U:\\PolyChat\\Saves")) - { - Debug.WriteLine("--Path exists.--"); - } - else - { - Directory.CreateDirectory("U:\\PolyChat\\Saves"); - Debug.WriteLine("--Path Created--."); - } - - //go through all files and send ip and json array to ui - String[] filepaths = Directory.GetFiles("U:\\PolyChat\\Saves"); - if (filepaths.Length > 0) - { - Debug.WriteLine("---Loading Saves"); - foreach (String path in filepaths) - { - Debug.WriteLine($"--{path}"); - String jsonArr = File.ReadAllText(path); - String ip = Path.GetFileName(path); - ip = ip.Substring(0, ip.Length - 4); - Debug.WriteLine($"-{ip}"); - Debug.WriteLine(jsonArr); - Connect(ip); - UIController.OnIncomingConnection(ip); - UIController.OnIncomingMessages(ip, jsonArr); - } - } - } - */ /// /// Saves incoming chat message to /// @@ -296,13 +268,9 @@ namespace PolyChat private void encode(string json) { - byte[] plaintext = Encoding.UTF8.GetBytes(json); - byte[] entropy = new byte[20]; - using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider()) - { - rng.GetBytes(entropy); - } - + String ecryptetText = FileManager.encrypt(json); + Debug.WriteLine(ecryptetText); + //Debug.WriteLine(FileManager.decrypt(ecryptetText)); } } } diff --git a/PolyChat/Models/FileManager.cs b/PolyChat/Models/FileManager.cs new file mode 100644 index 0000000..815873c --- /dev/null +++ b/PolyChat/Models/FileManager.cs @@ -0,0 +1,84 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.IO; +using System.Security.Cryptography; + + +namespace PolyChat.Models +{ + class FileManager + { + public static String encrypt(String toEncrypt) + { + try { + string textToEncrypt = toEncrypt; + string ToReturn = ""; + string publickey = "santhosh"; + string secretkey = "engineer"; + byte[] secretkeyByte = { }; + secretkeyByte = System.Text.Encoding.UTF8.GetBytes(secretkey); + byte[] publickeybyte = { }; + publickeybyte = System.Text.Encoding.UTF8.GetBytes(publickey); + MemoryStream ms = null; + CryptoStream cs = null; + byte[] inputbyteArray = System.Text.Encoding.UTF8.GetBytes(textToEncrypt); + using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) + { + ms = new MemoryStream(); + cs = new CryptoStream(ms, des.CreateEncryptor(publickeybyte, secretkeyByte), CryptoStreamMode.Write); + cs.Write(inputbyteArray, 0, inputbyteArray.Length); + cs.FlushFinalBlock(); + ToReturn = Convert.ToBase64String(ms.ToArray()); + } + return ToReturn; + } catch (Exception ex) + { + throw new Exception(ex.Message, ex.InnerException); + } + + } + + + public static String decrypt(String toDecrypt) + { + try + { + string textToDecrypt = toDecrypt; + string ToReturn = ""; + string publickey = "santhosh"; + string privatekey = "engineer"; + byte[] privatekeyByte = { }; + privatekeyByte = System.Text.Encoding.UTF8.GetBytes(privatekey); + byte[] publickeybyte = { }; + publickeybyte = System.Text.Encoding.UTF8.GetBytes(publickey); + MemoryStream ms = null; + CryptoStream cs = null; + byte[] inputbyteArray = new byte[textToDecrypt.Replace(" ", "+").Length]; + inputbyteArray = Convert.FromBase64String(textToDecrypt.Replace(" ", "+")); + using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) + { + ms = new MemoryStream(); + cs = new CryptoStream(ms, des.CreateDecryptor(publickeybyte, privatekeyByte), CryptoStreamMode.Write); + cs.Write(inputbyteArray, 0, inputbyteArray.Length); + cs.FlushFinalBlock(); + Encoding encoding = Encoding.UTF8; + ToReturn = encoding.GetString(ms.ToArray()); + } + return ToReturn; + } + catch (Exception ae) + { + throw new Exception(ae.Message, ae.InnerException); + } + } + } + + + + + + +} diff --git a/PolyChat/PolyChat.csproj b/PolyChat/PolyChat.csproj index 12041a4..c047509 100644 --- a/PolyChat/PolyChat.csproj +++ b/PolyChat/PolyChat.csproj @@ -137,6 +137,7 @@ +