package com.leng.demo.utils; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Base64; /* * 加密工具类(MD5:1+2次加密,SHA-256加密,AES加密+解密) * */ public class DoubleEncryptionUtil { /** * MD5加密方法 * * @param input 原始字符串 * @return 加密后的字符串 */ public static String md5(String input) { try { // 获取MD5算法实例 MessageDigest md = MessageDigest.getInstance("MD5"); // 计算消息摘要 byte[] messageDigest = md.digest(input.getBytes(StandardCharsets.UTF_8)); // 创建StringBuilder存储十六进制字符串 StringBuilder hexString = new StringBuilder(); for (byte b : messageDigest) { // 将字节转为十六进制字符串 String hex = Integer.toHexString(0xff & b); if (hex.length() == 1) { // 若长度为1则前缀补0 hexString.append('0'); } hexString.append(hex); } return hexString.toString(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } } /** * 双重加密方法,先进行MD5加密再进行SHA-256加密 * * @param input 原始字符串 * @return 加密后的字符串 */ public static String doubleEncrypt(String input) { try { // 对原始字符串进行MD5加密 String firstEncrypt = md5(input); System.out.println("md5第一次加密:" + firstEncrypt); // 获取SHA-256算法实例 MessageDigest md = MessageDigest.getInstance("SHA-256"); // 计算消息摘要 byte[] messageDigest = md.digest(firstEncrypt.getBytes(StandardCharsets.UTF_8)); // 创建StringBuilder存储十六进制字符串 StringBuilder hexString = new StringBuilder(); for (byte b : messageDigest) { // 将字节转为十六进制字符串 String hex = Integer.toHexString(0xff & b); if (hex.length() == 1) { // 若长度为1则前缀补0 hexString.append('0'); } hexString.append(hex); } return hexString.toString(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } } /** * 对字符串进行SHA-256加密 * * @param input 要加密的字符串 * @return 加密后的字符串 */ public static String sha256(String input) { try { // 获取SHA-256算法实例 MessageDigest md = MessageDigest.getInstance("SHA-256"); // 计算消息摘要 byte[] messageDigest = md.digest(input.getBytes(StandardCharsets.UTF_8)); // 创建StringBuilder存储十六进制字符串 StringBuilder hexString = new StringBuilder(); for (byte b : messageDigest) { // 将字节转为十六进制字符串 String hex = Integer.toHexString(0xff & b); if (hex.length() == 1) { // 若长度为1则前缀补0 hexString.append('0'); } hexString.append(hex); } return hexString.toString(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } } /** * 对字符串进行AES加密 * * @param input 要加密的字符串 * @param key 密钥数组 * @param iv 偏移量数组 * @return 加密后的字符串 */ public static String encrypt(String input, byte[] key, byte[] iv) throws Exception { //获取Cipher实例,使用AES算法、CBC模式和PKCS5Padding填充 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); //根据给定的key字节数组构造一个密钥 SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); //根据给定的iv字节数组构造一个偏移量对象 IvParameterSpec ivSpec = new IvParameterSpec(iv); //初始化Cipher对象为加密模式 cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); //对输入数据进行加密 byte[] encrypted = cipher.doFinal(input.getBytes()); //将字节数组转换为Base64编码的字符串返回 return Base64.getEncoder().encodeToString(encrypted); } /** * 对字符串进行AES解密 * * @param input 要解密的字符串 * @param key 密钥数组 * @param iv 偏移量数组 * @return 解密后的字符串 */ public static String decrypt(String input, byte[] key, byte[] iv) throws Exception { //获取Cipher实例,使用AES算法、CBC模式和PKCS5Padding填充 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); // 根据给定的key字节数组构造一个密钥 SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); //根据给定的iv字节数组构造一个偏移量对象 IvParameterSpec ivSpec = new IvParameterSpec(iv); //初始化Cipher对象为解密模式 cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); //对输入数据进行Base64解码并解密,获取原始字节数组 byte[] original = cipher.doFinal(Base64.getDecoder().decode(input)); //使用UTF-8编码将字节数组转换为字符串返回 return new String(original); } }
常用加密工具类
最新推荐文章于 2024-04-24 16:35:55 发布