码迷,mamicode.com
首页 > 编程语言 > 详细

各种加密算法

时间:2015-02-16 20:56:44      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:

md5:

 private string MD5(string source)
        {
            StringBuilder sb = new StringBuilder();
            System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create();
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(source));
            for (int i = 0; i < s.Length; i++)
            {
                sb.Append(s[i].ToString("X2"));
            }
            return sb.ToString().ToLower();
        }

 

Hmac_MD5:

    private string Hmac_MD5(string key,string message)
        {
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            byte[] keyByte = encoding.GetBytes(key);
            HMACMD5 hmacmd5 = new HMACMD5(keyByte);
            byte[] messageBytes = encoding.GetBytes(message);
            byte[] hashmessage = hmacmd5.ComputeHash(messageBytes);
            string rtnVal = ByteToString(hashmessage);
            return rtnVal.ToLower();
        }

        private static string ByteToString(byte[] buff)
        {
            string sbinary = "";
            for (int i = 0; i < buff.Length; i++)
            {
                sbinary += buff[i].ToString("X2"); 
            }
            return (sbinary);
        }

Hmac_SHA1加密:

/// <summary>
        /// HmacSHA1方式进行macmacmac签名
        /// </summary>
        /// <param name="text">加密的内容</param>
        /// <param name="key">加密key</param>
        /// <returns></returns>
        public string HmacSha1(string text, string key)
        {
            HMACSHA1 hmacsha1 = new HMACSHA1();
            hmacsha1.Key = Encoding.UTF8.GetBytes(key);
            byte[] dataBuffer = Encoding.UTF8.GetBytes(text);
            byte[] hashBytes = hmacsha1.ComputeHash(dataBuffer);
            return Convert.ToBase64String(hashBytes);
        }

 

各种加密算法

标签:

原文地址:http://www.cnblogs.com/yonguibe/p/4294467.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!