仿ecshop 里的加密数的c# 实现

class Program
    {
        public static string GetMD5Hash(String input) {
            // Create a new instance of the MD5CryptoServiceProvider object.
            MD5 md5Hasher = MD5.Create();

            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString();
        }
        /// <summary>
        /// 时间戳转为C#格式时间
        /// </summary>
        /// <param name=”timeStamp”></param>
        /// <returns></returns>
        private DateTime GetTime(string timeStamp)
        {

            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000000");
            TimeSpan toNow = new TimeSpan(lTime); return dtStart.Add(toNow);

        }
        /// <summary>
        /// DateTime时间格式转换为Unix时间戳格式
        /// </summary>
        /// <param name=”time”></param>
        /// <returns></returns>
        public  static int ConvertDateTimeInt(System.DateTime time)
        {

            System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));

            return (int)(time - startTime).TotalSeconds;

        }
        public static string  passport_encrypt(string txt, string key)
        {
            Random rand=new Random(ConvertDateTimeInt(DateTime.Now));
            string encrypt_key = GetMD5Hash(rand.Next(0,32000).ToString());
            int ctr = 0;
            string tmp = "";
            for(int i = 0; i < txt.Length; i++ )
            {
                ctr = ctr == encrypt_key.Length ? 0 : ctr;
                tmp += encrypt_key[ctr] + Char.ConvertFromUtf32(txt[i] ^ encrypt_key[ctr++]);
            }
            string result=passport_key(tmp, key);
            byte[] bytes = Encoding.Default.GetBytes(result);
            return Convert.ToBase64String(bytes);
        }
        public static string  passport_key(string text,string key)
        {
            string encrypt_key = GetMD5Hash(key);
            Console.WriteLine(encrypt_key);
            int ctr = 0;
            string tmp = "";
            for(int i = 0; i < text.Length; i++)
            {
                ctr = (ctr == encrypt_key.Length)?0:ctr;
                tmp += Char.ConvertFromUtf32(text[i]^encrypt_key[ctr++]);
            }
            return tmp;
        }
        static void Main(string[] args)
        {
            Console.WriteLine(Char.ConvertFromUtf32(91));
            Console.WriteLine(passport_encrypt("60113ee0220001", "eefdee"));
            
            System.Console.ReadLine();
        }
    }

  为了能和ecshop 对接,编写了仿ecshop 的加密方法

posted on 2013-10-26 16:54  陈哲  阅读(175)  评论(0)    收藏  举报

导航