zoukankan      html  css  js  c++  java
  • MD5安全加密

    package com.wjz.util;
    
    import java.security.MessageDigest;
    
    import org.apache.log4j.Logger;
    
    /**
     * MD5安全加密
     * @author wjz
     *
     */
    public class MD5Util {
        
        private static Logger logger = Logger.getLogger(MD5Util.class);
    
        private MD5Util(){
        }
        
        /**
         * MD5安全加密
         * @param value 原始密文
         * @param count 加密次数
         * @return 加密密文
         */
        public static String encrypt(String value , int count){
            String result = null;
            try {
                 MessageDigest md5 = MessageDigest.getInstance("MD5");
                 //使用指定的字节加密原始字节
                 md5.update(value.getBytes());
                 //获得加密之后的字节
                 byte[] bs = md5.digest();
                 if(count > 1) 
                     result = encrypt(byte2String(bs), count-1);
                 //把密文转换成十六进制的字符串形式
                 result = byte2String(bs);
            } catch (Exception e) {
                logger.error(e.getMessage());
                e.printStackTrace();
            }
            return result;
        }
        
        private static String byte2String(byte[] bs) {
            //String本质上是字符数组
            //byte是字节,char是字符,一个字符等于两个字节
            char[] hexDigits = {'0','1','2','3','4','5','6','7','8','9', 'A','B','C','D','E','F' };
            int i = 0;
            char[] resultArry = new char[bs.length*2];
            for (byte b : bs) {
                //0xf表示16进制的F=15(二进制:00001111);
                resultArry[i++] = hexDigits[b >>> 4 & 0xf];
                resultArry[i++] = hexDigits[b & 0xf];
            }
            // 字符数组组合成字符串返回
            return new String(resultArry);
        }
    }
  • 相关阅读:
    FJ省队集训DAY3 T1
    FJ省队集训DAY2 T2
    FJ省队集训DAY2 T1
    FJ省队集训DAY1 T1
    POJ 1225 Substrings
    BZOJ 2732 射箭
    Light OJ 1314 Names for Babies
    SPOJ220 Relevant Phrases of Annihilation
    POJ3683 Falsita
    ES6 常用语法
  • 原文地址:https://www.cnblogs.com/BINGJJFLY/p/7477219.html
Copyright © 2011-2022 走看看