zoukankan      html  css  js  c++  java
  • 各种加密算法

    md5:

    
    
           /// <summary>
            /// MD5函数
            /// </summary>
            /// <param name="str"></param>
            /// <param name="encoding"></param>
            /// <returns></returns>
            public static string MD5(string str, Encoding encoding)
            {
                byte[] b = encoding.GetBytes(str);
                b = new MD5CryptoServiceProvider().ComputeHash(b);
                string ret = "";
                for (int i = 0; i < b.Length; i++)
                    ret += b[i].ToString("x").PadLeft(2, '0');
                return ret;
            }
    
    调用方法:
    //  必须制定编码,c#默认是gb2132,但java等语言是utf-8,所以尽量通用
    XXX.Common.Encrypt.MD5(strPrepare,Encoding.UTF8);
    
    
    
    
    

    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);
            }
  • 相关阅读:
    还有更简单的不重复随机数生成方法吗?
    SqlServer数据插入性能小记
    html页面滚动时元素错位解决方案
    为Web页中的Table对象创建一个映射表
    js实现的快速排序
    webkit内核的浏览器为什么removeAttribute('style')会失效?
    setAttribute第三个参数
    Windows转到linux中,文件乱码,文件编码转换
    查看端口的占用
    sndfile
  • 原文地址:https://www.cnblogs.com/yonguibe/p/4294467.html
Copyright © 2011-2022 走看看