MD5加密时指定输出编码格式

/**
 * 
 */
package xxxxxxxxxxxxxxxxx;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 *@ClassName: MD5Tool
 *@author: malz
 *@Description:
 *@date: 2018-9-13下午2:31:37
 */
public class MD5Tool {
    public static String md5Encryption(String str) {
        String result = null;
        if (str != null) {
            try {
                
                byte[] btInput = str.getBytes("utf-8");
                
                MessageDigest md = MessageDigest.getInstance("MD5");
                //md.update(str.getBytes());
                md.update(btInput);
                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));
                }
                result = buf.toString();

                // 16位
                // result = buf.toString().substring(8, 24);
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return result;
    }
}

猜你喜欢

转载自blog.csdn.net/malz_zh/article/details/84983963