工具类介绍:
1. Md5加密 返回byte[]
2.byte[]解密 返回string
3.hash加密
工具类:
package com.thk.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
/**
* Md5加密方法
*
*
*/
public class Md5Utils {
private static final Logger log = LoggerFactory.getLogger(com.thk.utils.Md5Utils.class);
/**
* Md5加密 返回byte[]
*
* @param s
* @return
*/
private static byte[] md5(String s) {
MessageDigest algorithm;
try {
algorithm = MessageDigest.getInstance("MD5");
algorithm.reset();
algorithm.update(s.getBytes("UTF-8"));
byte[] messageDigest = algorithm.digest();
return messageDigest;
} catch (Exception e) {
log.error("MD5 Error...", e);
}
return null;
}
/**
* byte[]解密 返回string
*
* @param hash
* @return
*/
private static final String toHex(byte hash[]) {
if (hash == null) {
return null;
}
StringBuffer buf = new StringBuffer(hash.length * 2);
int i;
for (i = 0; i < hash.length; i++) {
if ((hash[i] & 0xff) < 0x10) {
buf.append("0");
}
buf.append(Long.toString(hash[i] & 0xff, 16));
}
return buf.toString();
}
/**
* hash加密
*
* @param s
* @return
*/
public static String hash(String s) {
try {
return new String(toHex(md5(s)).getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);
} catch (Exception e) {
log.error("not supported charset...{}", e);
return s;
}
}
public static void main(String[] args) {
String a = "123";
byte[] bytes = md5(a);
String s = toHex(bytes);
System.out.println(s);
String hash = hash(a);
System.out.println(hash);
}
}
测试:
202cb962ac59075b964b07152d234b70
202cb962ac59075b964b07152d234b70










