签名加密解密

        // DES加密数据
        public static string Encrypt(string Text)
        {
            return Encrypt(Text, "Michael");
        }
        public static string Encrypt(string encryptText, string encryptKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            byte[] inputByteArray = Encoding.Default.GetBytes(encryptText);
            des.Key = ASCIIEncoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(encryptKey, "md5").Substring(0, 8));
            des.IV = ASCIIEncoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(encryptKey, "md5").Substring(0, 8));
            MemoryStream stream = new MemoryStream();
            CryptoStream cryptStream = new CryptoStream(stream, des.CreateEncryptor(), CryptoStreamMode.Write);
            cryptStream.Write(inputByteArray, 0, inputByteArray.Length);
            cryptStream.FlushFinalBlock();
            StringBuilder builder = new StringBuilder();
            foreach (var item in stream.ToArray())
            {
                builder.AppendFormat("{0:X2}", item);
            }
            return builder.ToString();
        }

     // DES解密数据
        public static string Decrypt(string Text)
        {
            return Decrypt(Text, "Michael");
        }
        public static string Decrypt(string decryptText, string decryptKey)
        {
            DESCryptoServiceProvider des = new DESCryptoServiceProvider();
            int len;
            len = decryptText.Length / 2;
            byte[] inputByteArray = new byte[len];
            int x, i;
            for (x = 0; x < len; x++)
            {
                i = Convert.ToInt32(decryptText.Substring(x * 2, 2), 16);
                inputByteArray[x] = (byte)i;
            }
            des.Key = ASCIIEncoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(decryptKey, "md5").Substring(0, 8));
            des.IV = ASCIIEncoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(decryptKey, "md5").Substring(0, 8));
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            return Encoding.Default.GetString(ms.ToArray());
        }

     //MD5加密

    public string MD5Sign(string text)
        {
            StringBuilder md5Str = new StringBuilder();
            MD5 md5 = MD5.Create();
            byte[] md5Byte = md5.ComputeHash(Encoding.Default.GetBytes(text));
            md5.Clear();
            for (int i = 0; i < md5Byte.Length; i++)
            {
                md5Str.Append(md5Byte[i].ToString("x2"));
            }
            return md5Str.ToString();
        }

        public static string MD5(string pwd)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] data = System.Text.Encoding.Default.GetBytes(pwd);
            byte[] md5data = md5.ComputeHash(data);
            md5.Clear();
            string str = "";
            for (int i = 0; i < md5data.Length; i++)
            {
                str += md5data[i].ToString("x").PadLeft(2, '0');

            }
            return str;
        }

posted @ 2017-06-05 16:12  Alwayswantmore  阅读(225)  评论(0)    收藏  举报