MD5 加密和解密 (参考资料写的)

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());
}
   

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值