md5加密算法
程序员文章站
2024-03-19 11:03:22
...
package com.parwa.web.util;
import java.security.MessageDigest;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
/**
* MD5加密工具类
* @author win
*
*/
public class MD5Utils {
@SuppressWarnings("rawtypes")
public static String createSign(SortedMap<Object, Object> parameters, String key) {
StringBuffer sb = new StringBuffer();
Set es = parameters.entrySet();// 所有参与传参的参数按照accsii排序(升序)
Iterator it = es.iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
String k = (String) entry.getKey();
Object v = entry.getValue();
if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {
sb.append(k + "=" + v + "&");
}
}
sb.append("key=" + key);
String sign = MD5Encode(sb.toString(), "UTF-8").toUpperCase();
return sign;
}
/**
* MD5加密密文返回
* */
public static String getMd5Value(String sSecret) {
String result = null;
try {
MessageDigest bmd5 = MessageDigest.getInstance("MD5");
bmd5.update(sSecret.getBytes());
StringBuffer buf = new StringBuffer();
try {
result = byte2hex(bmd5.digest(buf.toString().getBytes("UTF-8")));
} catch (Exception e) {
e.printStackTrace();
}
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 二进制转字符串
*/
private static String byte2hex(byte[] b) {
StringBuffer hs = new StringBuffer();
String stmp = "";
for (int n = 0; n < b.length; n++) {
stmp = (Integer.toHexString(b[n] & 0XFF));
if (stmp.length() == 1)
hs.append("0").append(stmp);
else
hs.append(stmp);
}
return hs.toString().toUpperCase(Locale.ENGLISH);
}
/**
* **验证
*/
public static String SmsPwdMd5(String plainText ) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plainText.getBytes());
byte b[] = md.digest();
int i;
StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if(i<0) i+= 256;
if(i<16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
return buf.toString();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return "";
}
}
private 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;
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}
public static String MD5Encode(String origin, String charsetname) {
String resultString = null;
try {
resultString = new String(origin);
MessageDigest md = MessageDigest.getInstance("MD5");
if (charsetname == null || "".equals(charsetname))
resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
else
resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname)));
} catch (Exception exception) {
}
return resultString;
}
private static final String hexDigits[] = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d",
"e", "f" };
public static void main(String[] args) {
System.out.println(getMd5Value("test"));
}
}
上一篇: random应用及常见用法
下一篇: Python实现md5加密