求HMAC MD5算法,就是需要密钥加密MD5--200分,长期有效
在网上找过相关代码,有个jscript的标准源码,但C#的没有匹配的哪位大虾能提供,不胜感激!
加密后要求输出32位加密字符串 --------------------编程问答-------------------- 自己先顶一下
问题解决后另外开贴给分
以前遭遇过不能结贴的尴尬,被扣信誉分了
见谅 --------------------编程问答-------------------- 自己先顶一下
问题解决后另外开贴给分
以前遭遇过不能结贴的尴尬,被扣信誉分了
见谅 --------------------编程问答-------------------- private final static int HMAC_BLKL = 64;
private final static int HMAC_MD5L = 16;
private final static byte IPAD = 0x36;
private final static byte OPAD = 0x5C;
public static String HMAC(String origin, String key) throws Exception {
System.out.println("====="+origin);
byte[] keyBytes = key.getBytes();
byte[] orgBytes = origin.getBytes();
byte[] iBytes = new byte[HMAC_BLKL];
int conLength2 = HMAC_BLKL + HMAC_MD5L;
byte[] oBytes = new byte[conLength2];
int keyLength = keyBytes.length;
int orgLength = orgBytes.length;
int conLength = HMAC_BLKL + orgLength;
byte[] conBytes = new byte[conLength];
for (int i = 0; i < HMAC_BLKL; i++) {
if (i < keyLength) {
iBytes[i] = (byte) (keyBytes[i] ^ IPAD);
oBytes[i] = (byte) (keyBytes[i] ^ OPAD);
} else {
iBytes[i] = IPAD;
oBytes[i] = OPAD;
}
conBytes[i] = iBytes[i];
}
for (int i = HMAC_BLKL; i < conLength; i++) {
conBytes[i] = orgBytes[i - HMAC_BLKL];
}
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] res1 = md.digest(conBytes);
for (int i = HMAC_BLKL; i < conLength2; i++) {
oBytes[i] = res1[i - HMAC_BLKL];
}
byte[] res = md.digest(oBytes);
String result = byteArrayToHexString(res);
return result;
}
private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5",
"6", "7", "8", "9", "A", "B", "C", "D", "E", "F" };
public static String byteArrayToHexString(byte[] b) {
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i < b.length; i++) {
resultSb.append(byteToHexString(b[i]));
}
return resultSb.toString();
}
private static String byteToHexString(byte b) {
int n = b;
if (n < 0) {
n = 256 + n;
}
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}
--------------------编程问答-------------------- http://www.google.cn/search?q=c%23+md5&rls=com.microsoft:*:IE-SearchBox&ie=UTF-8&oe=UTF-8&sourceid=ie7&rlz=1I7GGIJ --------------------编程问答--------------------
--------------------编程问答-------------------- using System;
#region Md5加密,生成16位或32位,生成的密文都是大写
public string Md5To16(string str)
{
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(str)), 4, 8);
t2 = t2.Replace("-", "");
return t2;
}
//// <summary>
/// MD5 32位加密
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public string Md5To32(string str)
{
string pwd = "";
MD5 md5 = MD5.Create();
byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
for (int i = 0; i < s.Length; i++)
{
pwd = pwd + s[i].ToString("X");
}
return pwd;
}
#endregion
using System.Security.Cryptography;
using System.IO;
using System.Data;
using System.Text;
using System.Security;
补充:.NET技术 , ASP.NET