码迷,mamicode.com
首页 > 编程语言 > 详细

MD5加密算法工具类

时间:2017-10-15 17:34:14      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:uri   jdk   end   ring   ram   bool   turn   十六进制   password   

 很多公司使用MD5存放用户密码,但是当摘要过的MD5值泄漏出去后还是有很大可能通过别的方法获得原文。\

 

通过以下网站很容易获得原文
http://www.cmd5.com/
http://pmd5.com/

 

 技术分享

 

 

package com.bootdo.common.demo;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

import org.apache.shiro.codec.Hex;

public class MD5Util {
     /**
      * 普通MD5加密<br>  
      *   
      * @param input
      * @return
      */
    public static String MD5(String input) {  
        MessageDigest md5 = null;  
        try {  
            md5 = MessageDigest.getInstance("MD5");  
        } catch (NoSuchAlgorithmException e) {  
            return "check jdk";  
        } catch (Exception e) {  
            e.printStackTrace();  
            return "";  
        }  
        char[] charArray = input.toCharArray();  
        byte[] byteArray = new byte[charArray.length];  
  
        for (int i = 0; i < charArray.length; i++)  
            byteArray[i] = (byte) charArray[i];  
        byte[] md5Bytes = md5.digest(byteArray);  
        StringBuffer hexValue = new StringBuffer();  
        for (int i = 0; i < md5Bytes.length; i++) {  
            int val = ((int) md5Bytes[i]) & 0xff;  
            if (val < 16)  
                hexValue.append("0");  
            hexValue.append(Integer.toHexString(val));  
        }  
        return hexValue.toString();  
  
    }  
   
       
       
       
        /**
         * 加盐MD5加密<br>
         * 
         * @param password
         * @return
         */
        public static String generate(String password) {  
            Random r = new Random();  
            StringBuilder sb = new StringBuilder(16);  
            sb.append(r.nextInt(99999999)).append(r.nextInt(99999999));  
            int len = sb.length();  
            if (len < 16) {  
                for (int i = 0; i < 16 - len; i++) {  
                    sb.append("0");  
                }  
            }  
            String salt = sb.toString();  
            password = md5Hex(password + salt);  
            char[] cs = new char[48];  
            for (int i = 0; i < 48; i += 3) {  
                cs[i] = password.charAt(i / 3 * 2);  
                char c = salt.charAt(i / 3);  
                cs[i + 1] = c;  
                cs[i + 2] = password.charAt(i / 3 * 2 + 1);  
            }  
            return new String(cs);  
        }  
  
        /**
         * 校验加盐后是否和原文一致<br>
         *   
         * @param password
         * @param md5
         * @return
         */
        public static boolean verify(String password, String md5) {  
            char[] cs1 = new char[32];  
            char[] cs2 = new char[16];  
            for (int i = 0; i < 48; i += 3) {  
                cs1[i / 3 * 2] = md5.charAt(i);  
                cs1[i / 3 * 2 + 1] = md5.charAt(i + 2);  
                cs2[i / 3] = md5.charAt(i + 1);  
            }  
            String salt = new String(cs2);  
            return md5Hex(password + salt).equals(new String(cs1));  
        }  
  
        /**  
         * 获取十六进制字符串形式的MD5摘要  
         */  
        private static String md5Hex(String src) {  
            try {  
                MessageDigest md5 = MessageDigest.getInstance("MD5");  
                byte[] bs = md5.digest(src.getBytes());  
                return new String(new Hex().encode(bs));  
            } catch (Exception e) {  
                return null;  
            }  
        }  
  
        
   public static void main(String[] args) {
       // 原文  
       String plaintext = "DingSai";  
   //  plaintext = "123456";  
       System.out.println("原始:" + plaintext);  
       System.out.println("普通MD5后:" + MD5Util.MD5(plaintext));  
 
       // 获取加盐后的MD5值  
       String ciphertext = MD5Util.generate(plaintext);  
       System.out.println("加盐后MD5:" + ciphertext);  
       System.out.println("是否是同一字符串:" + MD5Util.verify(plaintext, ciphertext));  
       /**  
        * 其中某次DingSai字符串的MD5值  
        */  
       String[] tempSalt = { "c4d980d6905a646d27c0c437b1f046d4207aa2396df6af86", 
                  "66db82d9da2e35c95416471a147d12e46925d38e1185c043", 
                  "61a718e4c15d914504a41d95230087a51816632183732b5a" };  
 
       for (String temp : tempSalt) {  
           System.out.println("是否是同一字符串:" + MD5Util.verify(plaintext, temp));  
       }  
    }
       
       
       
}

运行结果:

技术分享

 

MD5加密算法工具类

标签:uri   jdk   end   ring   ram   bool   turn   十六进制   password   

原文地址:http://www.cnblogs.com/mr-wuxiansheng/p/7672520.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!