private void loginButton_Click(object sender, RoutedEventArgs e)
        {
            if (user == null)
            {
                // Login
                LoginObject          loginObeject = new LoginObject(loginUsernameTextBox.Text, loginPasswordTextBox.Password);
                Server.Models.Client loginClient  = RequestsManager.Login(clientObject.stream, loginObeject.toJsonObject());

                if (loginClient != null)
                {
                    user = loginClient;
                    TransactionsTab.Visibility = Visibility.Visible;
                    checkAll.Visibility        = Visibility.Visible;


                    loginButton.Content           = "SignOut";
                    loginUsernameTextBox.Text     = "";
                    loginPasswordTextBox.Password = "";
                }
                else
                {
                    checkAll.Visibility        = Visibility.Hidden;
                    TransactionsTab.Visibility = Visibility.Hidden;
                }
            }
            else
            {
                // Sign out
                TransactionsTab.Visibility    = Visibility.Hidden;
                checkAll.Visibility           = Visibility.Hidden;
                loginButton.Content           = "Login";
                loginUsernameTextBox.Text     = "";
                loginPasswordTextBox.Password = "";
            }
        }
        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);
            }
        }
Beispiel #3
0
        public static Boolean DoTransaction(string sender, string reciver, string amount)
        {
            int senderID  = int.Parse(sender);
            int reciverID = int.Parse(reciver);


            DBContext db = getInstace();

            int transmitted;

            try
            {
                transmitted = Int32.Parse(amount);
            }
            catch
            {
                transmitted = 0;
            }
            var FromQuery      = from t in db.Clients where t.Id == senderID select t;
            var ToQuery        = from t in db.Clients where t.Id == reciverID select t;
            int senderBalance  = 0;
            int ReciverBalance = 0;

            foreach (Server.Models.Client clinet in FromQuery)
            {
                senderBalance = clinet.Balance;
            }
            foreach (Server.Models.Client clinet in FromQuery)
            {
                ReciverBalance = clinet.Balance;
            }
            if (senderBalance < transmitted)
            {
                return(false);
            }
            else
            {
                Server.Models.Client sendeUser   = db.Clients.First(e => e.Id.Equals(senderID));
                Server.Models.Client reciverUser = db.Clients.First(e => e.Id.Equals(reciverID));
                reciverUser.Balance += transmitted;
                sendeUser.Balance   -= transmitted;
                db.SaveChanges();
                Transaction t = new Transaction();
                t.Amount    = transmitted;
                t.ReciverId = reciverUser.Id;
                t.SenderId  = sendeUser.Id;
                db.Transactions.Add(t);
                db.SaveChanges();
                return(true);
            }
        }