50,526
社区成员
发帖
与我相关
我的任务
分享
import java.io.IOException;
import java.security.MessageDigest;
import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;
/**
* 本类提供通用的安全算法
*/
public class SecurityArithmetic {
public final static byte[] md5(String s) {
byte[] md=null;
try {
byte[] btInput = s.getBytes();
MessageDigest mdInst = MessageDigest.getInstance("MD5");
mdInst.update(btInput);
md = mdInst.digest();
}
catch (Exception e) {
// e.printStackTrace();
return null;
}
return md;
}
public static String base64Encode(byte[] b){
return new BASE64Encoder().encode(b);
}
public static byte[] base64Decode(String b){
try {
return new BASE64Decoder().decodeBuffer(b);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
public static String md5AndHex(String s){
byte[] b=md5(s);
String temp="";
for (int i=0;i<b.length;i++){
temp+=StringUtils.pad(Integer.toHexString(b[i] & 0xff), 2, '0', true);
}
return temp;
}
public static String md5AndBase64(String s){
return base64Encode(md5(s));
}
/**
* @param args
*/
public static void main(String[] args) {
//d41d8cd98f00b204e9800998ecf8427e
//d41d8cd98f00b204e9800998ecf8427e
System.out.println(md5AndBase64("123456"));
}
}