Md5加密密码 加密字符串

public static String md5(String str) {
        String result = "";
        try {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] bytes = md.digest(str.getBytes());
        result = converByteToString(bytes);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return result;
    }
    /**
     * Purpose:将byte数组写出为String
     * @author Hermanwang
     * @param bytes
     * @return
     * @return String
     */
    public static String converByteToString(byte[] bytes) {
        String result ="";
        for(int i=0; i<bytes.length ;i++){
            int temp = bytes[i] & 0xff;
            String temphex =Integer.toHexString(temp);
            if(temphex.length()<2){
                result += "0"+temphex;
            }else{
                result += temphex;
            }
        }
        return result;
    }

猜你喜欢

转载自blog.csdn.net/cyberHerman/article/details/81510228