package com.java.activiti.util;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
public class EncodeUtil {
//MD5加密
public String md5Encode(String inStr){
MessageDigest md5 = null;
try{
md5 = MessageDigest.getInstance("MD5");
}catch(Exception e){
System.out.println(e.toString());
e.printStackTrace();
return "";
}
byte[] byteArray = null;
try {
byteArray = inStr.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for(int i=0;i<md5Bytes.length;i++){
int val = md5Bytes[i] & 0xff;
if(val<16){
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
// 测试主函数
public static void main(String args[]) {
EncodeUtil util = new EncodeUtil();
String s = new String("123456");
System.out.println("原始:" + s);
System.out.println("MD5后:" + util.md5Encode(s));
}
}
在接口里写
// EncodeUtil util = new EncodeUtil();
// String s = new String(user.getPassword());
// String d = new String(user.getUserName());
// user.setPassword(util.md5Encode(s));
// user.setUserName(util.md5Encode(d));
这样添加的时候就加密了