package com.until;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/*
* 处理MD5加密工具类
* 密码加密
*
* */
public class UtilMD5 {
/**
* 将byte类型转成 String 类型
*
* @param ib byte 输入 byte 类型
* @return String 返回 String 类型
*/
private static String byteHEX(byte ib) {
//输出小写的
char[] DigitNormal = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A','B','C','D','E','F'};
//输出大写的
/* char[] DigitCap = { '0','1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F' }; */
char[] ob = new char[2];
ob[0] = DigitNormal[ (ib >>> 4) & 0X0F];
// ob[0] = DigitCap[(ib >>> 4) & 0X0F]; //大写
ob[1] = DigitNormal[ib & 0X0F];
// ob[1] = DigitCap[ib & 0X0F]; //大写
return new String(ob);
}
/**
* @Title: MD5加密
* @Description: 加工方法
* @创建时间 2015-1-19
*/
public static String getByMD5(String str){
if(null == str || str.isEmpty()){return null ;};
try {
StringBuffer buffer = new StringBuffer();
char[] chars = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] date = md.digest(str.getBytes());
for(byte b : date){
buffer.append(chars[(b >> 4) & 0x0F]);
buffer.append(chars[b & 0x0F]);
}
return buffer.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* @Title: java 中的MD5
* @Description: java.security.MessageDigest
* @创建时间 2015-1-19
*/
public static byte[] getMd5(String input) {
try {
byte[] by = input.getBytes("UTF-8");
MessageDigest det = MessageDigest.getInstance("MD5");
return det.digest(by);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 将二进制转换成16进制
*
* @param buf
* @return
*/
public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
/**
* 将16进制转换为二进制
*
* @param hexStr
* @return
*/
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length() / 2];
for (int i = 0; i < hexStr.length() / 2; i++) {
int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2),
16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
public static void main(String[] args) {
String st = "admin";
//String tt ="";
System.out.println(getByMD5(st));
System.out.println(getMd5("E10ADC3949BA59ABBE56E057F20F883E").toString());
}
}