You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
package com.utils ; // 声明工具类所在包
import java.math.BigInteger ; // 导入大整数处理类
import java.security.MessageDigest ; // 导入消息摘要类
import java.security.NoSuchAlgorithmException ; // 导入无此算法异常类
public class MD5Utils { // MD5加密工具类
// MD5加密方法
public static String md5 ( String plainText ) {
byte [ ] secretBytes = null ; // 声明字节数组存储加密结果
try {
// 获取MD5算法实例并执行加密
secretBytes = MessageDigest . getInstance ( "md5" ) . digest (
plainText . getBytes ( ) ) ;
} catch ( NoSuchAlgorithmException e ) {
// 抛出运行时异常( MD5算法不存在时)
throw new RuntimeException ( "没有这个md5算法! " ) ;
}
// 将字节数组转换为16进制字符串
String md5code = new BigInteger ( 1 , secretBytes ) . toString ( 16 ) ;
// 补全32位MD5字符串( 前面补0)
for ( int i = 0 ; i < 32 - md5code . length ( ) ; i + + ) {
md5code = "0" + md5code ;
}
return md5code ; // 返回32位MD5加密字符串
}
}