Beispiel #1
0
        public string Decrypt(string encrypted)
        {
            string res = "";

            try
            {
                if (encrypted == null)
                {
                    return("");
                }
                if (encrypted.Trim().Length == 0)
                {
                    return("");
                }
                // base 64 decode
                byte[] encryptedBytesWithSalt = Convert.FromBase64String(encrypted);
                // extract salt (first 8 bytes of encrypted)
                byte[] salt           = new byte[8];
                byte[] encryptedBytes = new byte[encryptedBytesWithSalt.Length - salt.Length - 8];
                Buffer.BlockCopy(encryptedBytesWithSalt, 8, salt, 0, salt.Length);
                Buffer.BlockCopy(encryptedBytesWithSalt, salt.Length + 8, encryptedBytes, 0, encryptedBytes.Length);
                // get key and iv
                byte[] key, iv;
                DeriveKeyAndIV(passphrase, salt, out key, out iv);
                res = DecryptStringFromBytesAes(encryptedBytes, key, iv);
                res = Uri.UnescapeDataString(res);
            }
            catch (Exception exp)
            {
                clsEcom common = new clsEcom();
                common.Log("Decrypt", exp.Message + "(value=" + encrypted + ")", true, exp);
            }
            return(res);
        }
Beispiel #2
0
        public bool IsContainsSpecialChars(string val)
        {
            bool res = false;

            try
            {
                if (val.ToLower().Trim().Contains("'"))
                {
                    return(true);
                }
                if (val.ToLower().Trim().Contains('"'))
                {
                    return(true);
                }
                if (val.ToLower().Trim().Contains("&"))
                {
                    return(true);
                }
                if (val.ToLower().Trim().Contains("--"))
                {
                    return(true);
                }
            }
            catch (Exception exp)
            {
                clsEcom clsecom = new clsEcom();
                clsecom.Log("IsContainsSpecialChars", exp.Message + "(val=" + val + ")", true, exp, false);
            }
            return(res);
        }
Beispiel #3
0
        public bool AuthToken(string token, string device_id)
        {
            clsEcomEncryptor enc     = new clsEcomEncryptor();
            clsEcom          clsecom = new clsEcom();
            string           res     = clsecom.get_value(token);

            if (res.Trim().Length == 0)
            {
                return(false);
            }
            res = enc.Encrypt(res);
            if (res.Trim().Length == 0)
            {
                return(false);
            }
            string[] splt = res.Split(',');
            if (splt.Length != 3)
            {
                return(false);
            }
            if (splt[2] != additional_key)
            {
                return(false);
            }
            string device_id_token = splt[1];

            if (device_id != device_id_token)
            {
                return(false);
            }
            DateTime date1 = DateTime.Now;

            if (!DateTime.TryParse(splt[0], out date1))
            {
                return(false);
            }
            TimeSpan ts = (TimeSpan)(DateTime.Now - date1);

            if (ts.TotalDays > 30)
            {
                return(false);
            }
            return(true);
        }
Beispiel #4
0
        public string Encrypt(string plainText)
        {
            string res = "";

            try
            {
                if (plainText == null)
                {
                    return("");
                }
                if (plainText.Trim().Length == 0)
                {
                    return("");
                }
                // generate salt
                byte[] key, iv;
                byte[] salt = new byte[8];
                RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
                rng.GetNonZeroBytes(salt);
                DeriveKeyAndIV(passphrase, salt, out key, out iv);
                // encrypt bytes
                byte[] encryptedBytes = EncryptStringToBytesAes(plainText, key, iv);
                // add salt as first 8 bytes
                byte[] encryptedBytesWithSalt = new byte[salt.Length + encryptedBytes.Length + 8];
                Buffer.BlockCopy(Encoding.ASCII.GetBytes("Salted__"), 0, encryptedBytesWithSalt, 0, 8);
                Buffer.BlockCopy(salt, 0, encryptedBytesWithSalt, 8, salt.Length);
                Buffer.BlockCopy(encryptedBytes, 0, encryptedBytesWithSalt, salt.Length + 8, encryptedBytes.Length);
                // base64 encode
                res = Convert.ToBase64String(encryptedBytesWithSalt);
                res = Uri.EscapeUriString(res);
            }
            catch (Exception exp)
            {
                clsEcom common = new clsEcom();
                common.Log("Encrypt", exp.Message + "(value=" + plainText + ")", true, exp);
            }
            return(res);
        }