Ejemplo n.º 1
0
        public static string DecodeString(string value, string key = staticKey)
        {
            String dataValue  = "";
            String calcHash   = "";
            String storedHash = "";

            System.Security.Cryptography.MACTripleDES             mac3des = new System.Security.Cryptography.MACTripleDES();
            System.Security.Cryptography.MD5CryptoServiceProvider md5     = new System.Security.Cryptography.MD5CryptoServiceProvider();
            mac3des.Key = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(key));

            try
            {
                dataValue  = System.Text.Encoding.UTF8.GetString(System.Convert.FromBase64String(value.Split(System.Convert.ToChar("-"))[0]));
                storedHash = System.Text.Encoding.UTF8.GetString(System.Convert.FromBase64String(value.Split(System.Convert.ToChar("-"))[1]));
                calcHash   = System.Text.Encoding.UTF8.GetString(mac3des.ComputeHash(System.Text.Encoding.UTF8.GetBytes(dataValue)));

                if (storedHash != calcHash)
                {
                    //Data was corrupted
                    throw new ArgumentException("Hash value does not match");
                }
            }
            catch (System.Exception)
            {
                throw new ArgumentException("Invalid String");
            }
            return(dataValue);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// The tamper proof string encode.
 /// </summary>
 /// <param name="strValue">The string value.</param>
 /// <param name="strKey">The string key.</param>
 /// <returns>The <see cref="string" />.</returns>
 private static string TamperProofStringEncode(string strValue, string strKey)
 {
     System.Security.Cryptography.MACTripleDES             mac3Des = new System.Security.Cryptography.MACTripleDES();
     System.Security.Cryptography.MD5CryptoServiceProvider md5     = new System.Security.Cryptography.MD5CryptoServiceProvider();
     mac3Des.Key = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strKey));
     return(Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(strValue)) + Convert.ToChar("-") + Convert.ToBase64String(mac3Des.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strValue))));
 }
Ejemplo n.º 3
0
        /// <summary>
        /// The tamper proof string decode.
        /// </summary>
        /// <param name="strValue">The string value.</param>
        /// <param name="strKey">The string key.</param>
        /// <returns>The <see cref="string" />.</returns>
        /// <exception cref="ArgumentException">exception Argument Exception</exception>
        private static string TamperProofStringDecode(string strValue, string strKey)
        {
            string strDataValue;

            strValue = strValue.Trim();
            strValue = strValue.Replace(" ", "+");

            System.Security.Cryptography.MACTripleDES             mac3Des = new System.Security.Cryptography.MACTripleDES();
            System.Security.Cryptography.MD5CryptoServiceProvider md5     = new System.Security.Cryptography.MD5CryptoServiceProvider();
            mac3Des.Key = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strKey));

            try
            {
                strDataValue = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(strValue.Split(Convert.ToChar("-"))[0]));
                var strCalcHash = System.Text.Encoding.UTF8.GetString(mac3Des.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strDataValue)));

                Console.Write(strCalcHash);
            }
            catch (Exception)
            {
                return(strValue);
            }

            return(strDataValue);
        }
Ejemplo n.º 4
0
        public string TamperProofStringDecode(string value, string key)
        {
            string dataValue  = "";
            string calcHash   = "";
            string storedHash = "";

            System.Security.Cryptography.MACTripleDES             mac3des = new System.Security.Cryptography.MACTripleDES();
            System.Security.Cryptography.MD5CryptoServiceProvider md5     = new System.Security.Cryptography.MD5CryptoServiceProvider();
            mac3des.Key = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(key));

            try
            {
                dataValue  = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(value.Split('-')[0]));
                storedHash = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(value.Split('-')[1]));
                calcHash   = System.Text.Encoding.UTF8.GetString(mac3des.ComputeHash(System.Text.Encoding.UTF8.GetBytes(dataValue)));
                if ((storedHash != calcHash))
                {
                    throw new ArgumentException("Hash value does not match");
                    // This error is immediately caught below
                }
            }
            catch (Exception ex)
            {
                throw new ArgumentException("Invalid TamperProofString");
            }
            return(dataValue);
        }
Ejemplo n.º 5
0
 public string TamperProofStringEncode(string value, string key)
 {
     System.Security.Cryptography.MACTripleDES             mac3des = new System.Security.Cryptography.MACTripleDES();
     System.Security.Cryptography.MD5CryptoServiceProvider md5     = new System.Security.Cryptography.MD5CryptoServiceProvider();
     mac3des.Key = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(key));
     return(Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(value)) + "-" + Convert.ToBase64String(mac3des.ComputeHash(System.Text.Encoding.UTF8.GetBytes(value))));
 }
Ejemplo n.º 6
0
        //Function to decode the string
        //Throws an exception if the data is corrupt
        static public string DesCodificar(string value, string key)
        {
            String dataValue  = "";
            String calcHash   = "";
            String storedHash = "";

            System.Security.Cryptography.MACTripleDES             mac3des = new System.Security.Cryptography.MACTripleDES();
            System.Security.Cryptography.MD5CryptoServiceProvider md5     = new System.Security.Cryptography.MD5CryptoServiceProvider();
            mac3des.Key = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(key));

            try
            {
                dataValue  = System.Text.Encoding.UTF8.GetString(System.Convert.FromBase64String(value.Split(System.Convert.ToChar("-"))[0]));
                storedHash = System.Text.Encoding.UTF8.GetString(System.Convert.FromBase64String(value.Split(System.Convert.ToChar("-"))[1]));
                calcHash   = System.Text.Encoding.UTF8.GetString(mac3des.ComputeHash(System.Text.Encoding.UTF8.GetBytes(dataValue)));

                if (storedHash != calcHash)
                {
                    //Data was corrupted
                    throw new ArgumentException("N");
                    //This error is immediately caught below
                }
            }
            catch (System.Exception)
            {
                throw new ArgumentException("Codigo invalido");
            }
            return(dataValue);
        }
Ejemplo n.º 7
0
 //Function to encode the string
 static public string Codificar(string value, string key)
 {
     System.Security.Cryptography.MACTripleDES             mac3des = new System.Security.Cryptography.MACTripleDES();
     System.Security.Cryptography.MD5CryptoServiceProvider md5     = new System.Security.Cryptography.MD5CryptoServiceProvider();
     mac3des.Key = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(key));
     return(System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(value)) +
            System.Convert.ToChar("-") + System.Convert.ToBase64String(mac3des.ComputeHash(System.Text.Encoding.UTF8.GetBytes(value))));
 }
Ejemplo n.º 8
0
        private string decrypt(string value, string key)
        {
            string dataValue  = "";
            string calcHash   = "";
            string storedHash = "";

            System.Security.Cryptography.MACTripleDES             mac3des = new System.Security.Cryptography.MACTripleDES();
            System.Security.Cryptography.MD5CryptoServiceProvider md5     = new System.Security.Cryptography.MD5CryptoServiceProvider();
            mac3des.Key = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(key));

            try
            {
                dataValue  = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(value.Split('-')[0]));
                storedHash = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(value.Split('-')[1]));
                calcHash   = System.Text.Encoding.UTF8.GetString(mac3des.ComputeHash(System.Text.Encoding.UTF8.GetBytes(dataValue)));
                return(dataValue);
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
            return(dataValue);
        }
Ejemplo n.º 9
0
        public static string TamperProofStringDecode(string value1)
        {
            string key        = "0920";
            string dataValue  = "";
            string calcHash   = "";
            string storedHash = "";

            //value1 = value1.Replace('¿', '+');
            value1 = HttpUtility.UrlDecode(value1);
            value1 = value1.Replace(' ', '+');
            System.Security.Cryptography.MACTripleDES             mac3des = new System.Security.Cryptography.MACTripleDES();
            System.Security.Cryptography.MD5CryptoServiceProvider md5     = new System.Security.Cryptography.MD5CryptoServiceProvider();
            mac3des.Key = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(key));

            try
            {
                dataValue  = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(value1.Split('-')[0]));
                storedHash = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(value1.Split('-')[1]));
                calcHash   = System.Text.Encoding.UTF8.GetString(mac3des.ComputeHash(System.Text.Encoding.UTF8.GetBytes(dataValue)));

                if (storedHash != calcHash)
                {
                    //Data was corrupted
                    return("");
                    // throw new ArgumentException("Hash value does not match");
                }
                //This error is immediately caught below
            }
            catch (Exception ex)
            {
                return("");
                //throw new ArgumentException("Invalid TamperProofString");
            }


            return(dataValue);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// The tamper proof string decode.
        /// </summary>
        /// <param name="strValue">
        /// The string value.
        /// </param>
        /// <param name="strKey">
        /// The string key.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// exception Argument Exception
        /// </exception>
        public static string TamperProofStringDecode(string strValue, string strKey)
        {
            String strDataValue;
            String strCalcHash = "";

            strValue = strValue.Trim();
            strValue = strValue.Replace(" ", "+");

            System.Security.Cryptography.MACTripleDES             mac3Des = new System.Security.Cryptography.MACTripleDES();
            System.Security.Cryptography.MD5CryptoServiceProvider md5     = new System.Security.Cryptography.MD5CryptoServiceProvider();
            mac3Des.Key = md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strKey));

            try
            {
                strDataValue = System.Text.Encoding.UTF8.GetString(Convert.FromBase64String(strValue.Split(Convert.ToChar("-"))[0]));
                strCalcHash  = System.Text.Encoding.UTF8.GetString(mac3Des.ComputeHash(System.Text.Encoding.UTF8.GetBytes(strDataValue)));
            }
            catch (Exception)
            {
                //throw new ArgumentException("Invalid TamperProofString. " + ex.Message);
                return(strValue);
            }
            return(strDataValue);
        }