public class MD5Test {
/**
* 对明文字符串进行MD5加密
* @param source 传入的明文字符串
* @return 加密结果
*/
public static String md5(String source) {
// 1.判断source是否有效
if(source == null || source.length() == 0 ) {
//2.如果不是有效的字符串抛出异常
throw new RuntimeException("不是有效字符串");
}
try {
//3.获取MessageDigest对象
String algorithm = "md5";
MessageDigest messageDigest = MessageDigest.getInstance(algorithm);
//4.获取明文字符串对应的字节数组
byte[] input = source.getBytes();
//5.执行加密
byte[] output = messageDigest.digest(input);
//6.创建BigInteger对象
int signum = 1;
BigInteger bigInteger = new BigInteger(signum,output);
//7.按照16进制将bigInteger的值转换为字符串
int radix = 16;
String encoded = bigInteger.toString(radix).toUpperCase();
return encoded;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String sourceString = "123456";
System.out.println(MD5Test.md5(sourceString));
}
}
Java实现MD5加密
最新推荐文章于 2025-09-07 16:46:40 发布
该博客详细介绍了如何在Java中实现MD5加密方法,包括获取MessageDigest对象、执行加密过程、转换为16进制字符串等步骤。通过调用`MD5Test.md5()`方法,可以对任意明文字符串进行加密,例如示例中的123456。
385

被折叠的 条评论
为什么被折叠?



