merge reactive username with encode
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
|
||||
/// <summary>
|
||||
/// sends chatlogs as json array to uiController wit corrosponding ip
|
||||
///
|
||||
/// in ui when chat is clicked connection gets established
|
||||
/// </summary>
|
||||
/// <param name="ip"></param>
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -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)
|
||||
{
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<ContentDialogResult> SafelyOpenDialog(Dialog d)
|
||||
{
|
||||
if(VisualTreeHelper.GetOpenPopups(Window.Current).Count == 0)
|
||||
return d.ShowAsync();
|
||||
return null;
|
||||
}
|
||||
|
||||
public static IAsyncOperation<ContentDialogResult> SafelyOpenDialog(NewChatDialog d)
|
||||
{
|
||||
if (VisualTreeHelper.GetOpenPopups(Window.Current).Count == 0)
|
||||
return d.ShowAsync();
|
||||
return null;
|
||||
}
|
||||
|
||||
public static IAsyncOperation<ContentDialogResult> SafelyOpenDialog(EditUsernameDialog d)
|
||||
{
|
||||
if (VisualTreeHelper.GetOpenPopups(Window.Current).Count == 0)
|
||||
return d.ShowAsync();
|
||||
return null;
|
||||
}
|
||||
|
||||
// UPDATE FUNCTIONS FOR UI PLACEHOLDERS
|
||||
|
||||
private void updateNoChatsPlaceholder()
|
||||
|
||||
@@ -12,11 +12,6 @@ namespace PolyChat.Models
|
||||
public DateTime TimeStamp;
|
||||
public readonly bool Foreign;
|
||||
|
||||
|
||||
public ChatMessage()
|
||||
{
|
||||
|
||||
}
|
||||
/// <summary>
|
||||
/// Create own Message (directly sent)
|
||||
/// </summary>
|
||||
|
||||
84
PolyChat/Models/FileManager.cs
Normal file
84
PolyChat/Models/FileManager.cs
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -137,6 +137,7 @@
|
||||
<Compile Include="Models\ChatPartner.cs" />
|
||||
<Compile Include="Models\Exceptions\MessageTimedOutException.cs" />
|
||||
<Compile Include="Models\Exceptions\ConnectionFailedException.cs" />
|
||||
<Compile Include="Models\FileManager.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Util\IP.cs" />
|
||||
<Compile Include="Views\Dialog.xaml.cs">
|
||||
@@ -203,9 +204,6 @@
|
||||
<Version>5.0.2</Version>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="PolyChat_TemporaryKey.pfx" />
|
||||
</ItemGroup>
|
||||
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' < '14.0' ">
|
||||
<VisualStudioVersion>14.0</VisualStudioVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user