MD5
程序员文章站
2024-03-19 09:38:10
...
package com.nier.utils;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MD5Util2 {
/**
* 使用md5的算法进行加密
*/
public static String md5(String plainText) throws UnsupportedEncodingException {
byte[] secretBytes = null;
try {
//StandardCharsets.UTF_8 可以替换 "utf-8"
secretBytes = MessageDigest.getInstance("md5").digest(
plainText.getBytes("utf-8"));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("没有md5这个算法!");
}
StringBuilder md5code = new StringBuilder(new BigInteger(1, secretBytes).toString(16));// 16进制数字
// 如果生成数字未满32位,需要前面补0
for (int i = 0; i < 32 - md5code.length(); i++) {
md5code.insert(0, "0");
}
return md5code.toString();
}
public static void main(String[] args) throws UnsupportedEncodingException {
for (int i = 0; i < 8; i++) {
//36a95acd09fcaa4bcb645ccd82a87859
System.out.println(md5("yunuotianming"));
}
}
}
上一篇: java中使用md5算法加密