简单MD5加密工具-UTF-8
import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; /** * 简单MD5加密工具 * @author Jack */ public class MD5Util { public static String hexMD5(String string) { String md5 = MD5(string); if (null != md5 && md5.length() >= 24) md5 = md5.substring(8, 24); return md5; } public static String MD5(String string) { try { MessageDigest md = MessageDigest.getInstance("MD5"); return byteArrayToHexString(md.digest(string.getBytes("UTF-8"))); } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) { return null; } } public static String MD5(byte[] bytes) { try { MessageDigest md = MessageDigest.getInstance("MD5"); return byteArrayToHexString(md.digest(bytes)); } catch (NoSuchAlgorithmException e) { return null; } } private static String byteArrayToHexString(byte[] bytes) { StringBuffer buf = new StringBuffer(bytes.length * 2); for (int i = 0; i < bytes.length; i++) { if ((bytes[i] & 0xFF) < 16) buf.append("0"); buf.append(Long.toString((bytes[i] & 0xFF), 16)); } return buf.toString(); } /** * 返回当前日期年月日时分秒 * @return */ private static String getCurrentDate() { String currentTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")); return currentTime; } public static void main(String[] args) { String module = "ptsOperanDayFeignApi.staicsList";//模块 String account = "G111209";//专属账号 String pwd = "C22711113514";//专属密码 String signKey= "84911111111KKJ816";//密钥 String currentDate = getCurrentDate(); StringBuilder sb = new StringBuilder(module).append(account).append(signKey).append(pwd).append(currentDate); String reuslt = MD5((sb.toString())).toUpperCase(); System.out.println("加密前参数:"); System.out.println("module:" + module); System.out.println("account:" + account); System.out.println("signKey:" + signKey); System.out.println("pwd:" + pwd); System.out.println("currentDate:" + currentDate +" 格式:yyyyMMddHHmmss"); System.out.println("------------------------------------"); System.out.println("参数组合:" + sb.toString()); System.out.println("------------------------------------"); System.out.println("加密结果"); System.out.println("32位大写:" + reuslt); System.out.println("16位大写:" + reuslt.substring(8, 24)); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端