/// <summary> /// HMAC_MD5加密 /// </summary> /// <param name="original">明文</param> /// <param name="key">密钥</param> /// <returns>返回加密的字符串</returns> public static string HMAC_MD5(string original,string key) { byte[] b_tmp; byte[] b_tmp1; if (key == null) { return null; } byte[] digest = new byte[512]; byte[] k_ipad = new byte[64]; byte[] k_opad = new byte[64]; byte[] source = System.Text.ASCIIEncoding.ASCII.GetBytes(key); System.Security.Cryptography.MD5 shainner = new MD5CryptoServiceProvider(); for (int i = 0; i < 64; i++) { k_ipad[i] = 0 ^ 0x36; k_opad[i] = 0 ^ 0x5c; } try { if (source.Length > 64) { shainner = new MD5CryptoServiceProvider(); source = shainner.ComputeHash(source); } for (int i = 0; i < source.Length; i++) { k_ipad[i] = (byte)(source[i] ^ 0x36); k_opad[i] = (byte)(source[i] ^ 0x5c); } b_tmp1 = System.Text.ASCIIEncoding.ASCII.GetBytes(original); b_tmp = Adding(k_ipad,b_tmp1); shainner = new MD5CryptoServiceProvider(); digest = shainner.ComputeHash(b_tmp); b_tmp = Adding(k_opad,digest); shainner = new MD5CryptoServiceProvider(); digest = shainner.ComputeHash(b_tmp); return BytetoString(digest); } catch (Exception e) { throw e; } } /// <summary> /// 填充byte /// </summary> /// <param name="a"></param> /// <param name="b"></param> /// <returns></returns> private static byte[] Adding(byte[] a,byte[] b) { byte[] c = new byte[a.Length + b.Length]; a.copyTo(c,0); b.copyTo(c,a.Length); return c; } /// <summary> /// Byte To String /// </summary> /// <param name="buff"></param> /// <returns></returns> private static string BytetoString(byte[] buff) { string sbinary = ""; for (int i = 0; i < buff.Length; i++) { sbinary += buff[i].ToString("X2"); // hex format } return (sbinary); } ////////////////////// C# PC 只需要HMACMD5类就可以实现 /////////////////////// public byte[] EncodeString(byte[] key,byte[] source) { // Initialize the keyed hash object. HMACMD5 myhmacMD5 = new HMACMD5(key); // Compute the hash of the input file. return myhmacMD5.ComputeHash(source); }