zoukankan      html  css  js  c++  java
  • md5 算法类 java

    package com.sunyard.p2p.util;
    
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;
    
    /**
     * @author zhanghui
     * @date  2015/10/14.
     */
    public class Md5Utils {
        /**
         * MD5算法
         *
         * @param data
         * @return
         */
        public final static String getMd5(String data) {
            try {
                MessageDigest md = MessageDigest.getInstance("MD5");
                md.update(data.getBytes());
                byte b[] = md.digest();
    
                int i;
    
                StringBuffer buf = new StringBuffer("");
                for (int offset = 0; offset < b.length; offset++) {
                    i = b[offset];
                    if (i < 0)
                        i += 256;
                    if (i < 16)
                        buf.append("0");
                    buf.append(Integer.toHexString(i));
                }
                //32位加密
                return buf.toString();
                // 16位的加密
                //return buf.toString().substring(8, 24);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
                return null;
            }
        }
        public static void main(String[] args) {
            String content = "你好";
            System.err.println(content + "
    " + getMd5(content));
        }
    }

     MD5(16位)为 7a57a5a743894a0e MD5(32位)为 21232f297a57a5a743894a0e4a801fc3 明文为 admin

  • 相关阅读:
    AcWing 1081. 度的数量
    CF584D Dima and Lisa
    [ABC130F] Minimum Bounding Box
    AT4289 [ABC133E] Virus Tree 2
    Arc of Dream HDU
    Reading comprehension HDU
    【洛谷 1541】乌龟棋
    【洛谷 4880】抓住czx
    【洛谷 1525】关押罪犯
    【洛谷 1040】加分二叉树
  • 原文地址:https://www.cnblogs.com/hutuchong/p/4877730.html
Copyright © 2011-2022 走看看