Example #1
0
        public static Config decodeConfig(string data)
        {
            Config c = new Config();

            data = data.Replace("\"", "");
            data = data.Replace("{", "");
            data = data.Replace("}", "");

            string[] fields = data.Split(',');
            foreach (string field in fields)
            {
                string[] info = field.Split(':');
                if (info[0].Equals("TSL"))
                {
                    c.threadSleep = Convert.ToInt32(info[1]);
                }
                else if (info[0].Equals("GTW"))
                {
                    c.consultGateway = info[1]+":"+info[2];
                    c.consultGateway = c.consultGateway.Replace("\\","");
                }
                else if (info[0].Equals("LMD"))
                {
                    c.lastModified = Convert.ToDouble(info[1]);
                }
            }

            return c;
        }
Example #2
0
        public string getConfigs(string token)
        {
            string status = "OK";
            string json = JSON.getConfigUpdate(token);
            string key = Cryptography.randomString(32);
            string iv = Cryptography.randomString(32);

            string encryptedContent = Cryptography.encryptRJ256(json, key, iv);
            string sendData = encryptedContent + key + iv;

            Dictionary<string, string> data = new Dictionary<string, string>();
            data.Add("CTT", sendData);

            string webResponse = HttpComm.httpPostData(data, this.consultGateway);

            if (webResponse.Length <= 6)
            {
                status = HttpComm.treatHttpError(webResponse, "CCF");
            }
            else
            {
                string encryptedData = "";
                string decodeKey = webResponse.Substring(0, 32);
                string decodeIv = webResponse.Substring(webResponse.Length - 32);

                int keyIndex = webResponse.IndexOf(decodeKey);
                encryptedData = webResponse.Remove(keyIndex, decodeKey.Length);

                int ivIndex = encryptedData.IndexOf(decodeIv);
                encryptedData = encryptedData.Remove(ivIndex, decodeIv.Length);

                string configData = Cryptography.decryptRJ256(encryptedData, decodeKey, decodeIv);
                Config c = new Config();
                c = JSON.decodeConfig(configData);

                if(c.lastModified > this.lastModified)
                {
                    this.consultGateway = c.consultGateway;
                    this.threadSleep = c.threadSleep;
                    this.lastModified = c.lastModified;

                    try
                    {
                        this.saveConfigs();
                    }
                    catch(Exception e)
                    {
                        status = "Impossível salvar as configurações";
                    }
                }
            }
            return status;
        }
Example #3
0
        private void proccessLogin()
        {
            this.configs = new Config();

            this.updateDialogLabel("Carregando configurações");

            string status = this.configs.loadConfigs();

            if (status.Equals("OK"))
            {
                this.httpFunc = "LGN";

                if (loginErrorLabel.InvokeRequired)
                {
                    loginErrorLabel.Invoke(new MethodInvoker(delegate { loginErrorLabel.Text = "Autenticando usuário"; }));
                }

                string user = username.Text;
                string pass = password.Text;

                byte[] keyBytes = Encoding.UTF8.GetBytes(this.hashKey);
                byte[] passBytes = Encoding.UTF8.GetBytes(pass);

                var md5 = new HMACMD5(keyBytes);
                byte[] hashedBytesPass = md5.ComputeHash(passBytes);
                string md5Pass = BitConverter.ToString(hashedBytesPass).Replace("-", "").ToLower();

                Dictionary<string, string> fields = new Dictionary<string, string>();
                fields.Add("FNC", this.httpFunc);
                fields.Add("USR", user);
                fields.Add("PWD", md5Pass);

                string json = this.generateJson(fields);

                string randomKey = Cryptography.randomString(32);
                string randomIV = Cryptography.randomString(32);

                string encryptedData = Cryptography.encryptRJ256(json, randomKey, randomIV);
                encryptedData += randomKey;
                encryptedData += randomIV;

                Dictionary<string, string> sendData = new Dictionary<string, string>();
                sendData.Add("CTT", encryptedData);

                string webResponse = HttpComm.httpPostData(sendData, this.configs.consultGateway);

                if (webResponse.Length <= 6)
                {
                    string message = HttpComm.treatHttpError(webResponse, this.httpFunc);
                    this.updateDialogLabel(message);
                }
                else
                {
                    this.performLogin(webResponse);
                }
            }
            else
            {
                this.updateDialogLabel(status);
            }
        }