package utils; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** * 加密类 * * @author Administrator * */ public class Encrypt { /** * MD5 16位 * @param s * @return * @author Administrator * @since 2016年9月9日 上午11:16:09 */ public static String md516(String s) { if (s != null && s.trim().length() > 0) { return textToEncrypt(s, "MD5").substring(8, 24); } return ""; } /** * MD5 32位 * * @param s * @return * @author Administrator * @since 2016年9月9日 上午11:14:08 */ public static String md532(String s) { return textToEncrypt(s, "MD5"); } /** * 加密 * * @param plainText * @param type * @return * @author Administrator * @since 2016年9月9日 上午11:13:43 */ public static String textToEncrypt(String plainText, String type) { String result = ""; // 首先判断是否为空 if (null == plainText || "".equals(plainText.trim())) { return result; } try { // 首先进行实例化和初始化 MessageDigest md = MessageDigest.getInstance(type); // 得到一个操作系统默认的字节编码格式的字节数组 byte[] btInput = plainText.getBytes(); // 对得到的字节数组进行处理 md.update(btInput); // 进行哈希计算并返回结果 byte[] btResult = md.digest(); // 进行哈希计算后得到的数据的长度 StringBuffer sb = new StringBuffer(); for (byte b : btResult) { int bt = b & 0xff; if (bt < 16) { sb.append(0); } sb.append(Integer.toHexString(bt)); } result = sb.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return result; } } |
java 加密算法
最新推荐文章于 2025-06-09 09:33:17 发布