added client server login with hashed passwords and usernames

This commit is contained in:
Sem van der Hoeven
2020-10-02 11:43:07 +02:00
parent 3e5f6e46c4
commit fedf8c0e5b
4 changed files with 33 additions and 47 deletions

View File

@@ -38,8 +38,8 @@ namespace Client
private void retryEngineConnection() private void retryEngineConnection()
{ {
Console.WriteLine("Could not connect to the VR engine. Please make sure you are running the simulation!"); 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("-- Press any key to retry connecting to the VR engine.");
Console.ReadKey(); Console.ReadKey();
engineConnection.CreateConnection(); engineConnection.CreateConnection();
@@ -92,6 +92,7 @@ namespace Client
if (responseStatus == "OK") if (responseStatus == "OK")
{ {
this.connected = true; this.connected = true;
initEngine();
} }
else else
{ {
@@ -158,13 +159,12 @@ namespace Client
Console.WriteLine("enter password"); Console.WriteLine("enter password");
string password = Console.ReadLine(); string password = Console.ReadLine();
string hashUser = Hashing.Hasher.Encrypt(username); string hashUser = Hashing.Hasher.HashString(username);
string hashPassword = Hashing.Hasher.Encrypt(password); string hashPassword = Hashing.Hasher.HashString(password);
Console.WriteLine("hashed to " + hashUser + " " + hashPassword);
byte[] message = DataParser.getJsonMessage(DataParser.GetLoginJson(hashUser, hashPassword)); byte[] message = DataParser.getJsonMessage(DataParser.GetLoginJson(hashUser, hashPassword));
initEngine();
this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null); this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
} }
} }

View File

@@ -167,7 +167,7 @@ namespace Client
stream.Write(res); stream.Write(res);
Write("sent message " + message); //Write("sent message " + message);
} }
public void Write(string msg) public void Write(string msg)
{ {

View File

@@ -7,45 +7,21 @@ namespace Hashing
{ {
class Hasher class Hasher
{ {
static string key = "ProftaakRH-B4"; public static byte[] GetHash(string input)
public static string Encrypt(string text)
{ {
using (var md5 = new MD5CryptoServiceProvider()) using (HashAlgorithm algorithm = SHA256.Create())
{ {
using (var tdes = new TripleDESCryptoServiceProvider()) return algorithm.ComputeHash(Encoding.UTF8.GetBytes(input));
{
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) public static string HashString(string input)
{ {
using (var md5 = new MD5CryptoServiceProvider()) StringBuilder sb = new StringBuilder();
{ foreach (byte b in GetHash(input)) {
using (var tdes = new TripleDESCryptoServiceProvider()) sb.Append(b.ToString("X2"));
{ }
tdes.Key = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key)); return sb.ToString();
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);
}
}
}
} }
} }
} }

View File

@@ -128,7 +128,7 @@ namespace Server
Array.Copy(message, 5, payloadbytes, 0, message.Length - 5); Array.Copy(message, 5, payloadbytes, 0, message.Length - 5);
dynamic json = JsonConvert.DeserializeObject(Encoding.ASCII.GetString(payloadbytes)); 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)) else if (DataParser.isRawData(message))
@@ -142,28 +142,37 @@ namespace Server
private bool verifyLogin(string username, string password) private bool verifyLogin(string username, string password)
{ {
Console.WriteLine("got hashes " + username + password); Console.WriteLine("got hashes " + username + "\n" + password);
Console.WriteLine(Hashing.Hasher.Decrypt(username) + " " + Hashing.Hasher.Decrypt(password));
if (!File.Exists(fileName)) if (!File.Exists(fileName))
{ {
File.Create(fileName);
Console.WriteLine("file doesnt exist"); Console.WriteLine("file doesnt exist");
newUsers(username, password);
Console.WriteLine("true"); Console.WriteLine("true");
return true; return true;
} else } else
{ {
Console.WriteLine("file exists, located at " + Path.GetFullPath(fileName));
string[] usernamesPasswords = File.ReadAllLines(fileName); string[] usernamesPasswords = File.ReadAllLines(fileName);
if (usernamesPasswords.Length == 0)
{
newUsers(username, password);
return true;
}
foreach (string s in usernamesPasswords) foreach (string s in usernamesPasswords)
{ {
string[] combo = s.Split(";"); string[] combo = s.Split(" ");
if (combo[0] == username) if (combo[0] == username)
{ {
Console.WriteLine("true"); Console.WriteLine("correct info");
return combo[1] == password; return combo[1] == password;
} }
} }
Console.WriteLine("combo was not found in file");
} }
Console.WriteLine("false"); Console.WriteLine("false");
@@ -173,10 +182,11 @@ namespace Server
private void newUsers(string username, string password) private void newUsers(string username, string password)
{ {
File.Create(fileName);
Console.WriteLine("creating new entry in file");
using (StreamWriter sw = File.AppendText(fileName)) using (StreamWriter sw = File.AppendText(fileName))
{ {
sw.WriteLine(username + ";" + password); sw.WriteLine(username + " " + password);
} }
} }