博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MD5 加密和解密 (参考资料写的)
阅读量:4036 次
发布时间:2019-05-24

本文共 2524 字,大约阅读时间需要 8 分钟。

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

转载地址:http://tfjdi.baihongyu.com/

你可能感兴趣的文章
js弹窗插件
查看>>
自定义 select 下拉框 多选插件
查看>>
js判断数组内是否有重复值
查看>>
js获取url链接携带的参数值
查看>>
gdb 调试core dump
查看>>
gdb debug tips
查看>>
arm linux 生成火焰图
查看>>
linux和windows内存布局验证
查看>>
linux config
查看>>
linux insmod error -1 required key invalid
查看>>
linux kconfig配置
查看>>
linux不同模块completion通信
查看>>
linux printf获得时间戳
查看>>
C语言位扩展
查看>>
linux dump_backtrace
查看>>
linux irqdebug
查看>>
git 常用命令
查看>>
linux位操作API
查看>>
uboot.lds文件分析
查看>>
uboot start.s文件分析
查看>>