Java MD5加密算法工具类

MD5.java

package util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {
    public static String encrypt(String s) {
        StringBuffer buf = new StringBuffer();
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            md5.update(s.getBytes());
            byte[] bytes = md5.digest();

            for (int i = 0; i < bytes.length; i++) {
                int num = bytes[i];
                if (num < 0) {
                    num += 256;
                }

// 如果小于16,则加0补齐16进制两位数 if (num < 16) { buf.append(0); } buf.append(Integer.toHexString(num)); } } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return buf.toString(); } public static void main(String[] args) { String password = "HELLOhi"; System.out.println(encrypt(password)); } }

 

posted @ 2018-06-25 09:59  诚信天下  阅读(213)  评论(0编辑  收藏  举报