• MD5加密


    写法一:

    /**
     * md5加密
     * @param origin 明文
     * @param charsetname 格式“utf-8”
     * @return
     */
    public static String MD5Encode(String origin, String charsetname) {
        String resultString = null;
        try {
            resultString = new String(origin);
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(origin.getBytes("UTF-8"));
            BigInteger hash = new BigInteger(1, md.digest());
            resultString = hash.toString(16);
            if ((resultString.length() % 2) != 0) {
                resultString = "0" + resultString;
            }
        } catch (Exception exception) {
        }
        return resultString;
    }

    写法二:

    private static final String MD5_SUFFIX = "_www.baidu.com_"; 
    
    private static final String hexDigits[] = {
        "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", 
        "a", "b", "c", "d", "e", "f"
    };
    
    /** 
     * 字符加密 
     * @param str 明文 
     * @return 返回加了后缀的加密字符 
     */
    public static String encryptByMD5(String str) {  
        try {  
            if (str == null || str.length() < 1 || "0".equals(str))  
                str = "0";  
            String tmp = md5(str + MD5_SUFFIX, "UTF-8");  
           if (null != tmp) {  
               return replace(tmp, ":", "", -1).toLowerCase();  
           }  
        } catch (NoSuchAlgorithmException e) {  
            e.printStackTrace();  
        } catch (UnsupportedEncodingException e) {  
            e.printStackTrace();  
        }
        return "";  
    }  
    
    /** 
     * md5加密方法,不可逆转 
     * @param str 明文 
     * @param charset 字符编码 
     * @return 
     * @throws NoSuchAlgorithmException 
     * @throws UnsupportedEncodingException 
     */  
    public static String md5(String str, String charset)   
            throws NoSuchAlgorithmException, UnsupportedEncodingException {  
        byte[] tmpInput = null;  
        if (null != str) {  
            if (null != charset) {  
                tmpInput = str.getBytes(charset);  
            } else {  
                tmpInput = str.getBytes();  
            }  
        } else {  
            return null;  
        }  
        MessageDigest alg = MessageDigest.getInstance("MD5"); // or "SHA-1"  
        alg.update(tmpInput);  
        return byte1hex(alg.digest());  
    }
    public static String replace(String text, String repl, String with, int max) {  
        if (isEmpty(text) || isEmpty(repl) || with == null || max == 0) {  
            return text;  
        }  
        int start = 0;  
        int end = text.indexOf(repl, start);  
        if (end == -1) {  
            return text;  
        }  
        int replLength = repl.length();  
        int increase = with.length() - replLength;  
        increase = (increase < 0 ? 0 : increase);  
        increase *= (max < 0 ? 16 : (max > 64 ? 64 : max));  
        StringBuffer buf = new StringBuffer(text.length() + increase);  
        while (end != -1) {  
            buf.append(text.substring(start, end)).append(with);  
            start = end + replLength;  
            if (--max == 0) {  
                break;  
            }  
            end = text.indexOf(repl, start);  
        }  
        buf.append(text.substring(start));  
        return buf.toString();  
    }  
    /** 
         * 字节码转换成16进制字符串 
         * @param inputByte 
         * @return 
         */ 
        public static String byte1hex(byte[] inputByte) {  
            if (null == inputByte) {  
                return null;  
            }  
            String resultStr = "";  
            String tmpStr = "";  
            for (int n = 0; n < inputByte.length; n++) {  
                tmpStr = (Integer.toHexString(inputByte[n] & 0XFF));  
                if (tmpStr.length() == 1)  
                    resultStr = resultStr + "0" + tmpStr;  
                else  
                    resultStr = resultStr + tmpStr;  
                if (n < inputByte.length - 1)  
                    resultStr = resultStr + "";  
            }  
            return resultStr.toLowerCase();  
        }
  • 相关阅读:
    STL逆序迭代器(reverse_iterator)
    STL容器之vector容器API(二)
    STL容器之vector容器巧用swap收缩空间
    STL容器之vector容器API(一)
    STL容器vector概念和注意事项(每次扩充都会重新开辟空间,释放原空间,即首元素地址会变一次)
    STL容器之string内存重定义
    STL容器之string与c_style类型转换
    STL容器之string插入和删除
    STL容器之string字串
    STL容器之string比较
  • 原文地址:https://www.cnblogs.com/gjq1126-web/p/11207161.html
Copyright © 2020-2023  润新知