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();
        }
        private static void viewAllAccount(AdvanceStream stream)
        {
            AES aes = AES.getInstance();

            var    allAccountQuery = from t in DBContext.getInstace().Clients select t;
            string allAccounts     = null;

            foreach (Server.Models.Client clinet in allAccountQuery)
            {
                allAccounts += clinet.Username + "\t" + clinet.Balance + "\n";
            }

            string EncreptedAllAccounts = aes.Encrypt(allAccounts, KeysManager.AESkey);

            stream.Write(EncreptedAllAccounts);

            MainWindow.instance.Log("Accounts Data", allAccounts);
            MainWindow.instance.Log("Encrypted Accounts Data", EncreptedAllAccounts);
            MainWindow.instance.Log();
        }
        private static void signUp(AdvanceStream stream, MainWindow mainWindow)
        {
            string             digCer = stream.ReadString();
            DigitalCertificate dc     = DigitalCertificate.newClientObject(digCer);

            MainWindow.instance.Log("Get client certificate.....");
            string publicKey = "";

            MainWindow.instance.Log("connect to CA.....");


            MainWindow.clientForCertificate.connect((e) =>
            {
                e.Write("2");
            });

            publicKey = getCApublicKey(MainWindow.clientForCertificate.stream);



            KeysManager.RSAPcublicKeyOfCA = publicKey;
            MainWindow.instance.Log("CA public key", publicKey);

            if (dc.verviy())
            {
                stream.Write("1");
                byte[] encrSingUpdata   = stream.ReadBytes();
                RSA    rsa              = new RSA("Server");
                byte[] decrypSingUpData = rsa.decrypt(encrSingUpdata, KeysManager.RSAPrivateKey);

                MainWindow.instance.Log("Encrypted SignUp Data", Encoding.UTF8.GetString(encrSingUpdata));
                MainWindow.instance.Log("Decrypted SignUp Data", Encoding.UTF8.GetString(decrypSingUpData));

                SignUpObject signUpObject = SignUpObject.newLoginObject(Encoding.UTF8.GetString(decrypSingUpData));
                var          user         = DBContext.getInstace().Clients.SingleOrDefault(item => item.Username == signUpObject.username);
                if (user != null)
                {
                    if (user.Username.Equals(signUpObject.username))
                    {
                        stream.Write("0");
                        MainWindow.instance.Log("Error username already taken ", signUpObject.username);
                    }
                    else
                    {
                        stream.Write("1");
                        MainWindow.instance.Log("Error password already taken");
                    }
                }
                else
                {
                    DBContext.getInstace().Clients.Add(new Models.Client(signUpObject.name, signUpObject.username, signUpObject.password));
                    DBContext.getInstace().SaveChanges();
                    stream.Write("2");
                    user = DBContext.getInstace().Clients.SingleOrDefault(item => item.Username == signUpObject.username);
                    stream.Write(user.toJsonObject() + '\t' + "");
                    byte[] encryptKey = rsa.encrypte(KeysManager.AESkey, dc.SubjectPublicKey);
                    stream.Write(encryptKey);
                    MainWindow.instance.Log("Sign Up Successfully", user.Name);
                }
            }
            else
            {
                stream.Write("0");
            }

            MainWindow.instance.Log();
        }
 private static void sendKeys(AdvanceStream stream)
 {
     stream.Write(KeysManager.RSAPublicKey);
 }
 private static string getCApublicKey(AdvanceStream stream)
 {
     stream.Write("3");
     return(stream.ReadString());
 }