package com.joinintech.jaguar.function;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* MD5加密
*
*/
public class MD5 {
private String[] hexDigits={
"0","1","2","3","4","5","6","7",
"8","9","a","b","c","d","e","f"};
/*
* 转换十进制字符串
*/
private String byteToNumString(byte b){
int _b=b;
if(_b<0){
_b=_b+256;
}
return String.valueOf(_b);
}
/*
* 转换十六进制字符串
*/
private String byteToHexString(byte b){
int n=b;
if(n<0){
n=n+256;
}
int d1=n/16;
int d2=n%16;
return hexDigits[d1]+hexDigits[d2];
}
/*
* 字节数组转换成字符
*/
@SuppressWarnings("unused")
private String byteArrayToString(byte[] b){
StringBuffer str=new StringBuffer();
for (int i=0;i<b.length;i++) {
str.append(byteToHexString(b[i]));
}
return str.toString();
}
public String MD5Encode(String origin){
String result=null;
try {
result=new String(origin);
MessageDigest md=MessageDigest.getInstance("MD5");
result=byteArrayToString(md.digest(result.getBytes()));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return result;
}
/* public static void main(String[] args){
MD5 d=new MD5();
System.out.println(d.MD5Encode("123456"));
}*/
}
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* MD5加密
*
*/
public class MD5 {
private String[] hexDigits={
"0","1","2","3","4","5","6","7",
"8","9","a","b","c","d","e","f"};
/*
* 转换十进制字符串
*/
private String byteToNumString(byte b){
int _b=b;
if(_b<0){
_b=_b+256;
}
return String.valueOf(_b);
}
/*
* 转换十六进制字符串
*/
private String byteToHexString(byte b){
int n=b;
if(n<0){
n=n+256;
}
int d1=n/16;
int d2=n%16;
return hexDigits[d1]+hexDigits[d2];
}
/*
* 字节数组转换成字符
*/
@SuppressWarnings("unused")
private String byteArrayToString(byte[] b){
StringBuffer str=new StringBuffer();
for (int i=0;i<b.length;i++) {
str.append(byteToHexString(b[i]));
}
return str.toString();
}
public String MD5Encode(String origin){
String result=null;
try {
result=new String(origin);
MessageDigest md=MessageDigest.getInstance("MD5");
result=byteArrayToString(md.digest(result.getBytes()));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return result;
}
/* public static void main(String[] args){
MD5 d=new MD5();
System.out.println(d.MD5Encode("123456"));
}*/
}