MD5

 

import java.io.UnsupportedEncodingException;

import java.math.BigInteger;

import java.security.MessageDigest;

import java.security.NoSuchAlgorithmException;

 

public class MD5 {

 

    public static String hex2asc(String str) {

        String[] s = str.trim().split("\\\\x");

        // System.out.println(s.length);

        StringBuffer sb = new StringBuffer();

        for (String sItem : s) {

            // System.out.println(sItem);

            sb.append(sItem);

        }

        int len = sb.toString().length();

 

        // String data = null;

        StringBuffer sb1 = new StringBuffer();

 

        for (int i = 0; i < len; i += 2) {

            int x = Integer.parseInt(sb.toString().substring(i, i + 2), 16);

            char ss = (char) x;

 

            System.out.println(x + "::" + ss + "转换后->" + MD5.md5(String.valueOf(ss)));

 

            sb1.append(String.valueOf(ss));

        }

 

        return sb1.toString();

    }

 

    public static String md5(String plainText) {

 

        byte[] secretBytes = null;

        try {

            try {

                secretBytes = MessageDigest.getInstance("md5").digest(plainText.getBytes("UTF-8"));

            } catch (UnsupportedEncodingException e) {

                System.out.println("不存在UTF-8编码");

                e.printStackTrace();

            }

        } catch (NoSuchAlgorithmException e) {

            throw new RuntimeException("没有md5这个算法!");

        }

        String md5code = new BigInteger(1, secretBytes).toString(16);

        for (int i = 0; i < 32 - md5code.length(); i++) {

            md5code = "0" + md5code;

        }

        if(md5code.length()==31)

        {

                 md5code = "0" + md5code;

        }

        return md5code;

    }

         public static void main(String[] str)

         {

                   String persion ="";

                  System.out.println(MD5.md5("account870108card4096709300491381mobile15808907777type25f7af31ccb3544659373715bcc320b96").toUpperCase());

         }

   

}

posted on 2019-02-19 13:21  我是司  阅读(191)  评论(0编辑  收藏  举报

导航