added client server login with hashed passwords and usernames
This commit is contained in:
@@ -38,8 +38,8 @@ namespace Client
|
||||
|
||||
private void retryEngineConnection()
|
||||
{
|
||||
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("-- Could not connect to the VR engine. Please make sure you are running the simulation!");
|
||||
Console.WriteLine("-- Press any key to retry connecting to the VR engine.");
|
||||
Console.ReadKey();
|
||||
|
||||
engineConnection.CreateConnection();
|
||||
@@ -92,6 +92,7 @@ namespace Client
|
||||
if (responseStatus == "OK")
|
||||
{
|
||||
this.connected = true;
|
||||
initEngine();
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -158,13 +159,12 @@ namespace Client
|
||||
Console.WriteLine("enter password");
|
||||
string password = Console.ReadLine();
|
||||
|
||||
string hashUser = Hashing.Hasher.Encrypt(username);
|
||||
string hashPassword = Hashing.Hasher.Encrypt(password);
|
||||
Console.WriteLine("hashed to " + hashUser + " " + hashPassword);
|
||||
string hashUser = Hashing.Hasher.HashString(username);
|
||||
string hashPassword = Hashing.Hasher.HashString(password);
|
||||
|
||||
byte[] message = DataParser.getJsonMessage(DataParser.GetLoginJson(hashUser, hashPassword));
|
||||
|
||||
initEngine();
|
||||
|
||||
this.stream.BeginWrite(message, 0, message.Length, new AsyncCallback(OnWrite), null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -167,7 +167,7 @@ namespace Client
|
||||
|
||||
stream.Write(res);
|
||||
|
||||
Write("sent message " + message);
|
||||
//Write("sent message " + message);
|
||||
}
|
||||
public void Write(string msg)
|
||||
{
|
||||
|
||||
@@ -7,45 +7,21 @@ namespace Hashing
|
||||
{
|
||||
class Hasher
|
||||
{
|
||||
static string key = "ProftaakRH-B4";
|
||||
public static string Encrypt(string text)
|
||||
public static byte[] GetHash(string input)
|
||||
{
|
||||
using (var md5 = new MD5CryptoServiceProvider())
|
||||
using (HashAlgorithm algorithm = SHA256.Create())
|
||||
{
|
||||
using (var tdes = new TripleDESCryptoServiceProvider())
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
return algorithm.ComputeHash(Encoding.UTF8.GetBytes(input));
|
||||
}
|
||||
}
|
||||
|
||||
public static string Decrypt(string cipher)
|
||||
public static string HashString(string input)
|
||||
{
|
||||
using (var md5 = new MD5CryptoServiceProvider())
|
||||
{
|
||||
using (var tdes = new TripleDESCryptoServiceProvider())
|
||||
{
|
||||
tdes.Key = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
|
||||
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);
|
||||
}
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
foreach (byte b in GetHash(input)) {
|
||||
sb.Append(b.ToString("X2"));
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,7 +128,7 @@ namespace Server
|
||||
Array.Copy(message, 5, payloadbytes, 0, message.Length - 5);
|
||||
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))
|
||||
@@ -142,28 +142,37 @@ namespace Server
|
||||
|
||||
private bool verifyLogin(string username, string password)
|
||||
{
|
||||
Console.WriteLine("got hashes " + username + password);
|
||||
Console.WriteLine(Hashing.Hasher.Decrypt(username) + " " + Hashing.Hasher.Decrypt(password));
|
||||
Console.WriteLine("got hashes " + username + "\n" + password);
|
||||
|
||||
|
||||
if (!File.Exists(fileName))
|
||||
{
|
||||
File.Create(fileName);
|
||||
Console.WriteLine("file doesnt exist");
|
||||
|
||||
newUsers(username, password);
|
||||
Console.WriteLine("true");
|
||||
return true;
|
||||
} else
|
||||
{
|
||||
Console.WriteLine("file exists, located at " + Path.GetFullPath(fileName));
|
||||
string[] usernamesPasswords = File.ReadAllLines(fileName);
|
||||
if (usernamesPasswords.Length == 0)
|
||||
{
|
||||
newUsers(username, password);
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach (string s in usernamesPasswords)
|
||||
{
|
||||
string[] combo = s.Split(";");
|
||||
string[] combo = s.Split(" ");
|
||||
if (combo[0] == username)
|
||||
{
|
||||
Console.WriteLine("true");
|
||||
Console.WriteLine("correct info");
|
||||
return combo[1] == password;
|
||||
}
|
||||
|
||||
}
|
||||
Console.WriteLine("combo was not found in file");
|
||||
|
||||
}
|
||||
Console.WriteLine("false");
|
||||
@@ -173,10 +182,11 @@ namespace Server
|
||||
|
||||
private void newUsers(string username, string password)
|
||||
{
|
||||
File.Create(fileName);
|
||||
|
||||
Console.WriteLine("creating new entry in file");
|
||||
using (StreamWriter sw = File.AppendText(fileName))
|
||||
{
|
||||
sw.WriteLine(username + ";" + password);
|
||||
sw.WriteLine(username + " " + password);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user