public static string HmacSign(string aValue, string aKey) { int num; byte[] data = new byte[0x40]; byte[] buffer2 = new byte[0x40]; byte[] bytes = Encoding.UTF8.GetBytes(aKey); byte[] buffer4 = Encoding.UTF8.GetBytes(aValue); for (num = bytes.Length; num < 0x40; num++) { data[num] = 0x36; } for (num = bytes.Length; num < 0x40; num++) { buffer2[num] = 0x5c; } for (num = 0; num < bytes.Length; num++) { data[num] = (byte)(bytes[num] ^ 0x36); buffer2[num] = (byte)(bytes[num] ^ 0x5c); } HmacMD5 cmd = new HmacMD5(); cmd.update(data, (uint)data.Length); cmd.update(buffer4, (uint)buffer4.Length); byte[] buffer5 = cmd.finalize(); cmd.init(); cmd.update(buffer2, (uint)buffer2.Length); cmd.update(buffer5, 0x10); return(toHex(cmd.finalize())); }
/// <summary> /// 生成mac值 /// 手机支付平台的mac值需要先调用HmacSign(String source),再调用HmacSign(string aValue, string aKey) /// </summary> /// <param name="aValue">生成mac值的原文</param> /// <param name="aKey">MD5算法的key值</param> /// <returns></returns> public static string HmacSign(string aValue, string aKey) { byte[] k_ipad = new byte[0x40]; byte[] k_opad = new byte[0x40]; byte[] keyb = Encoding.UTF8.GetBytes(aKey); byte[] Value = Encoding.UTF8.GetBytes(aValue); for (int i = keyb.Length; i < 0x40; i++) { k_ipad[i] = 0x36; } for (int i = keyb.Length; i < 0x40; i++) { k_opad[i] = 0x5c; } for (int i = 0; i < keyb.Length; i++) { k_ipad[i] = (byte)(keyb[i] ^ 0x36); k_opad[i] = (byte)(keyb[i] ^ 0x5c); } HmacMD5 md = new HmacMD5(); md.update(k_ipad, (uint)k_ipad.Length); md.update(Value, (uint)Value.Length); byte[] dg = md.finalize(); md.init(); md.update(k_opad, (uint)k_opad.Length); md.update(dg, 0x10); String result = toHex(md.finalize()); //log.Info("HmacSign-RSP:Hmac="+result); return(result); }