关于java:MD5加密

30次阅读

共计 707 个字符,预计需要花费 2 分钟才能阅读完成。

package com.xiaoan.utils;

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

/**
 * @author 23105
 * @version 1.0
 * @description: TODO
 * @date 13/2/2023 下午 5:52
 */
public final class MD5 {public static String encrypt(String strSrc) {
        try {char hexChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8',
                    '9', 'a', 'b', 'c', 'd', 'e', 'f' };
            byte[] bytes = strSrc.getBytes();
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(bytes);
            bytes = md.digest();
            int j = bytes.length;
            char[] chars = new char[j * 2];
            int k = 0;
            for (int i = 0; i < bytes.length; i++) {byte b = bytes[i];
                chars[k++] = hexChars[b >>> 4 & 0xf];
                chars[k++] = hexChars[b & 0xf];
            }
            return new String(chars);
        } catch (NoSuchAlgorithmException e) {e.printStackTrace();
            throw new RuntimeException("MD5 加密出错!!+" + e);
        }
    }
}

正文完
 0