brgin of encryption

This commit is contained in:
SCM6WE
2021-09-23 13:33:03 +02:00
parent 866fa2600b
commit 665d289d3a
3 changed files with 95 additions and 42 deletions

View File

@@ -11,6 +11,9 @@ using System;
using System.Text.Json; using System.Text.Json;
using System.Text; using System.Text;
using System.Security.Cryptography; using System.Security.Cryptography;
using Windows.Security.Cryptography.Core;
using Windows.Security.Cryptography;
using Windows.Storage.Streams;
namespace PolyChat namespace PolyChat
{ {
@@ -37,6 +40,8 @@ namespace PolyChat
{ {
UIController = uiController; UIController = uiController;
OwnIP = getIP(); OwnIP = getIP();
loadChats();
encode("test");
Serve(); Serve();
} }
@@ -168,6 +173,8 @@ namespace PolyChat
/// <summary> /// <summary>
/// sends chatlogs as json array to uiController wit corrosponding ip /// sends chatlogs as json array to uiController wit corrosponding ip
///
/// in ui when chat is clicked connection gets established
/// </summary> /// </summary>
/// <param name="ip"></param> /// <param name="ip"></param>
public void loadChats() 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);
}
}
}
*/
/// <summary> /// <summary>
/// Saves incoming chat message to /// Saves incoming chat message to
/// </summary> /// </summary>
@@ -296,13 +268,9 @@ namespace PolyChat
private void encode(string json) private void encode(string json)
{ {
byte[] plaintext = Encoding.UTF8.GetBytes(json); String ecryptetText = FileManager.encrypt(json);
byte[] entropy = new byte[20]; Debug.WriteLine(ecryptetText);
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider()) //Debug.WriteLine(FileManager.decrypt(ecryptetText));
{
rng.GetBytes(entropy);
}
} }
} }
} }

View 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);
}
}
}
}

View File

@@ -137,6 +137,7 @@
<Compile Include="Models\ChatPartner.cs" /> <Compile Include="Models\ChatPartner.cs" />
<Compile Include="Models\Exceptions\MessageTimedOutException.cs" /> <Compile Include="Models\Exceptions\MessageTimedOutException.cs" />
<Compile Include="Models\Exceptions\ConnectionFailedException.cs" /> <Compile Include="Models\Exceptions\ConnectionFailedException.cs" />
<Compile Include="Models\FileManager.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Util\IP.cs" /> <Compile Include="Util\IP.cs" />
<Compile Include="Views\Dialog.xaml.cs"> <Compile Include="Views\Dialog.xaml.cs">