public static Server.Models.Client Login(AdvanceStream stream, string loginData) { // To generate private key for RSA if not exist RSA rsa = new RSA(LoginObject.newLoginObject(loginData).username); KeyManager.generateRSAPublicKey(rsa.rsaSP); KeyManager.generateRSAPrivateKey(rsa.rsaSP); stream.Write("1"); stream.Write(KeyManager.RSAPublicKey); AES aes = AES.getInstance(); byte[] msg = Encoding.UTF8.GetBytes(loginData); byte[] EncreptedLoginData = rsa.encrypte(msg, KeyManager.serverRSAPublicKey); MainWindow.instance.Log("Login Data", loginData); MainWindow.instance.Log("Encrypted Login Data", Encoding.UTF8.GetString(EncreptedLoginData)); stream.Write(EncreptedLoginData); string response = stream.ReadString(); if (response.Equals("0")) { //no user MainWindow.instance.Log("No such user"); MainWindow.instance.Log(); return(null); } else if (response.Equals("1")) { //wrong password MainWindow.instance.Log("Wrong Password"); MainWindow.instance.Log(); return(null); } else { //ok response = stream.ReadString(); Server.Models.Client loginClient = Server.Models.Client.newClientObject(response); MainWindow.instance.Log(response); byte[] inStream = stream.ReadBytes(); byte[] decrypKey = rsa.decrypt(inStream, KeyManager.RSAPrivateKey); MainWindow.instance.Log("Encrypted AES Key", Convert.ToBase64String(inStream, 0, inStream.Length)); KeyManager.serverAESPublicKey = Convert.ToBase64String(decrypKey, 0, decrypKey.Length); MainWindow.instance.Log("AES Key", KeyManager.serverAESPublicKey); return(loginClient); } }
private static void login(AdvanceStream stream) { AES aes = AES.getInstance(); string publicKey = stream.ReadString(); byte[] encryptByte = stream.ReadBytes(); RSA rsa = new RSA("Server"); byte[] realDatabyte = rsa.decrypt(encryptByte, KeysManager.RSAPrivateKey); string realData = Encoding.UTF8.GetString(realDatabyte); MainWindow.instance.Log("Encrypted Login Data", Encoding.UTF8.GetString(encryptByte)); MainWindow.instance.Log("Decrypted Login Data", realData); LoginObject loginObject = LoginObject.newLoginObject(realData); var user = DBContext.getInstace().Clients.SingleOrDefault(item => item.Username == loginObject.username); if (user == null) { stream.Write("0"); MainWindow.instance.Log("Error no such user", loginObject.username); } else { if (!user.Password.Equals(loginObject.password)) { stream.Write("1"); MainWindow.instance.Log("Error Wrong credentials", loginObject.username); } else { stream.Write("2"); stream.Write(user.toJsonObject()); byte[] encryptKey = rsa.encrypte(KeysManager.AESkey, publicKey); stream.Write(encryptKey); MainWindow.instance.Log("Logged in", user.Name); } } MainWindow.instance.Log(); }