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); }
时间: 2024-10-11 05:37:51