diff --git a/PolyChat/Controller.cs b/PolyChat/Controller.cs index 975c7cc..f27f2ff 100644 --- a/PolyChat/Controller.cs +++ b/PolyChat/Controller.cs @@ -8,9 +8,11 @@ using PolyChat.Models; using System.IO; using System.Threading; 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,8 +39,8 @@ namespace PolyChat { UIController = uiController; OwnIP = getIP(); - //loadChats(); - //SaveChats("10", "{das ist ein test}"); + loadChats(); + encode("test"); Serve(); // test @@ -60,7 +62,7 @@ namespace PolyChat { Connections.Add(ip, new Connection(ip, PORT, Data => OnMessage(ip, Data), CloseChat)); } - + } private void Serve() @@ -79,6 +81,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)) { @@ -174,10 +177,13 @@ namespace PolyChat /// /// sends chatlogs as json array to uiController wit corrosponding ip + /// + /// in ui when chat is clicked connection gets established /// /// 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")) { @@ -206,7 +212,6 @@ namespace PolyChat UIController.OnIncomingMessages(ip, jsonArr); } } - } /// @@ -221,53 +226,55 @@ 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]; - using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider()) - { - rng.GetBytes(entropy); - } - - /*byte[] ciphertext = ProtectedData.Protect(plaintext, entropy, - DataProtectionScope.CurrentUser);*/ + String ecryptetText = FileManager.encrypt(json); + Debug.WriteLine(ecryptetText); + //Debug.WriteLine(FileManager.decrypt(ecryptetText)); } } } diff --git a/PolyChat/MainPage.xaml.cs b/PolyChat/MainPage.xaml.cs index df1d05b..5179fbb 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 @@ -90,7 +90,7 @@ namespace PolyChat DateTime.Now ); NewChatDialog dialog = new NewChatDialog(); - var result = await dialog.ShowAsync(); + var result = await SafelyOpenDialog(dialog); if (result == ContentDialogResult.Primary) { string ip = IP.GetIPFromCode(dialog.getValue()); @@ -106,7 +106,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(); @@ -216,6 +216,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/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) /// 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..df5e9f4 100644 --- a/PolyChat/PolyChat.csproj +++ b/PolyChat/PolyChat.csproj @@ -137,6 +137,7 @@ + @@ -203,9 +204,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)