记录一个MD5加密的工具类

2019-04-26 19:04:01  卢浮宫  阅读量:117  版权声明:本文为站长原创文章,转载请写明出处


一、一个对字符串进行MD5加密的工具类,代码如下:

package com.xmy.pengpeng.Util;

import javax.xml.bind.JAXBContext;

import javax.xml.bind.Unmarshaller;

import java.io.*;

import java.security.MessageDigest;



	

public class Md5Util {



	

    public static String MD5(String test){

        String re_md5 = new String();

        try{

            MessageDigest md = MessageDigest.getInstance("MD5");

            md.update(test.getBytes("UTF-8"));

            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));

            }

            re_md5 = buf.toString();

        }catch(Exception e){

            e.printStackTrace();

        }

        return re_md5;

    }

    /**

     * xml转换成JavaBean

     * @param xml

     * @param c

     * @return

     */

    @SuppressWarnings("unchecked")

    public static <T> T converyToJavaBean(String xml, Class<T> c) {

        T t = null;

        try {

            JAXBContext context = JAXBContext.newInstance(c);

            Unmarshaller unmarshaller = context.createUnmarshaller();

            t = (T) unmarshaller.unmarshal(new StringReader(xml));

        } catch (Exception e) {

            e.printStackTrace();

        }



	

        return t;

    }



	

    public static String InputStreamToString(InputStream in){



	

        BufferedReader br = null;

        try {

            //br = new BufferedReader(new InputStreamReader(in));

            br = new BufferedReader(new InputStreamReader(in,"UTF-8"));

        } catch (Exception e1) {

            // TODO Auto-generated catch block

            e1.printStackTrace();

        }

        StringBuffer stringBuffer = new StringBuffer();

        String str = "";

        try {



	

            while ((str = br.readLine()) != null) {

                stringBuffer.append(str);

                stringBuffer.append("\n");

            }

        } catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }

        return stringBuffer.toString();

    }

}


最新评论:
123 2019-04-28 01:25:57