public class EncodesUtil {
private static final String DEFAULT_URL_ENCODING = "UTF-8";
/**
* Base64编码.
*/
public static String encodeBase64(byte[] input) {
return new String(Base64.encodeBase64(input));
}
/**
* Base64编码.
*/
public static String encodeBase64(String input) {
try {
return new String(Base64.encodeBase64(input.getBytes(DEFAULT_URL_ENCODING)));
} catch (UnsupportedEncodingException e) {
return "";
}
}
/**
* Base64解码.
*/
public static byte[] decodeBase64(String input) {
return Base64.decodeBase64(input.getBytes());
}
/**
* Base64解码.
*/
public static String decodeBase64String(String input) {
try {
return new String(Base64.decodeBase64(input.getBytes()), DEFAULT_URL_ENCODING);
} catch (UnsupportedEncodingException e) {
return "";
}
}
/**
* MD5编码.
*/
public static String encodeMD5(String input) {
String s = input == null ? "":input;//如果为空,则返回""
char hexDigists[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d',
'e','f'};//字典
try {
byte[] strTemp =s.getBytes();//获取二进制
MessageDigest mdTemp =MessageDigest.getInstance("MD5");
mdTemp.update(strTemp);//执行加密
byte[] md = mdTemp.digest();//加密结果
int j = md.length;//结果长度
char str[] = new char[j*2];//字符数组
int k = 0;
for (int i = 0; i < j; i++) { //将二进制加密结果转化为字符
byte byte0 = md[i];
str[k++] = hexDigists[byte0 >>> 4 &0xf];
str[k++] = hexDigists[byte0 & 0xf];
}
return new String(str);//输出加密后的字符
} catch (Exception e) {
// TODO Auto-generated catch block
return null;
}
}