import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* MD5加密解密工具类
*/
public class MD5Util {
/**
* 使用 MD5算法加密生成32位md5
* @param str
* @return
*/
public static String string2MD5(String str) {
byte[] secretBytes = null;
try {
secretBytes = MessageDigest.getInstance("md5").digest(
str.getBytes());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("没有md5这个算法!");
}
String md5code = new BigInteger(1, secretBytes).toString(16);// 16进制数字
// 如果生成数字未满32位,需要前面补0
for (int i = 0; i < 32 - md5code.length(); i++) {
md5code = "0" + md5code;
}
return md5code;
}
/**
* 可逆的加密解密算法,执行一次加密,两次解密
* @param str
* @return
*/
public static String convertMD5(String str){
char[] a = str.toCharArray();
for (int i = 0; i < a.length; i++){
a[i] = (char) (a[i] ^ 't');
}
String s = new String(a);
return s;
}
}
MD5加密解密工具类
于 2023-09-22 10:43:49 首次发布