MD5加密

采用二次加盐策略生成消息摘要:

public final class MD5SignUtil {
 
public static void main(String[] args) throws Exception {
System.out.println(generateSignature("netease_webemail", ":【广州爆炸因货柜车装卸引起伤者增至36人】记者从有关部门了解到"));
}
 
/*************************
* 采用二次加盐策略生成消息摘要
* 
* @param salt
*            盐值
* @param data
*            数据
* @return 摘要
* @throws Exception
*             异常
*/
public static final String generateSignature(final String salt, final String data) {
 
try {
final MessageDigest md = MessageDigest.getInstance("MD5");
// 一次MD5
byte[] dataBytes = md.digest(data.getBytes("UTF-8"));
// 二次MD5
byte[] siteBytes = salt.getBytes("UTF-8");
for (int i = 0; i < dataBytes.length && i < siteBytes.length; i++) {
dataBytes[i] = (byte) (dataBytes[i] ^ siteBytes[i]);
}
return bytesToHexStr(md.digest(dataBytes));
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
 
/**
* 将字节数组转换为16进制字符串的形式.
*/
private static final String bytesToHexStr(byte[] bcd) {
StringBuffer s = new StringBuffer(bcd.length * 2);
 
for (int i = 0; i < bcd.length; i++) {
s.append(HexArray[(bcd[i] >>> 4) & 0x0f]);
s.append(HexArray[bcd[i] & 0x0f]);
}
 
return s.toString();
}
 
private static final char[] HexArray = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
'f' };
 
 
}
 

 http://bbs.csdn.net/topics/390830388

你可能感兴趣的:(实习)