diff --git a/src/main/java/com/jeesite/modules/util/AESUtil.java b/src/main/java/com/jeesite/modules/util/AESUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..16de703a9eaec21a9a149da0c8f607eff18a0e2d --- /dev/null +++ b/src/main/java/com/jeesite/modules/util/AESUtil.java @@ -0,0 +1,34 @@ +package com.asiainfo.ai.admin.utils; + +import cn.hutool.core.util.CharsetUtil; +import cn.hutool.crypto.symmetric.SymmetricAlgorithm; +import cn.hutool.crypto.symmetric.SymmetricCrypto; + +public class AESUtil { + + /** + * aes加密 + * + * @param clientSecret 原始密钥 + */ + public static String encryptAesEncrypt(String clientId, String clientSecret) { + //构建--用clientId作为密钥 + SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, "bc39e70327ac4b1186a9a69ead73474e".getBytes()); + //加密为16进制表示 + String encryptHex = aes.encryptHex(clientSecret); + return encryptHex; + } + + /** + * aes解密 + * + * @param encryptClientSecret clientApp表里边的aes加密后密码 + */ + public static String decryptAesEncrypt(String clientId, String encryptClientSecret) { + //构建--用clientId作为密钥 + SymmetricCrypto aes = new SymmetricCrypto(SymmetricAlgorithm.AES, "bc39e70327ac4b1186a9a69ead73474e".getBytes()); + //加密为16进制表示 + String decryptStr = aes.decryptStr(encryptClientSecret, CharsetUtil.CHARSET_UTF_8); + return decryptStr; + } +} diff --git a/src/main/java/com/jeesite/modules/util/NameConvert2code.java b/src/main/java/com/jeesite/modules/util/NameConvert2code.java new file mode 100644 index 0000000000000000000000000000000000000000..06945cf8c3e7f5b05ab17a6d8e5e372ef39b7ae5 --- /dev/null +++ b/src/main/java/com/jeesite/modules/util/NameConvert2code.java @@ -0,0 +1,45 @@ +package com.asiainfo.ai.admin.utils; + +import cn.hutool.core.util.CharUtil; +import net.sourceforge.pinyin4j.PinyinHelper; +import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType; +import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; +import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; +import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType; +import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; + +public class NameConvert2code { + /** + * 如果传入参数中有汉字把汉字变为拼音,截取拼音首字母,多个汉字首字母拼接字符串,如果包含英文或者其他字符则不变输出 + * + * @param param param + * @return String + */ + public static String charactersChanged2PinyinAndTakeFirstLetter(String param) { + HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); + format.setCaseType(HanyuPinyinCaseType.LOWERCASE); + format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); + format.setVCharType(HanyuPinyinVCharType.WITH_V); + char[] input = param.trim().toCharArray(); + StringBuilder output = new StringBuilder(); + try { + for (char c : input) { + //忽略空格 + if (CharUtil.equals(c, ' ', true)) { + continue; + } + //判断字符是否是中文 + if (Character.toString(c).matches("[\\u4E00-\\u9FA5]+")) { + String[] temp = PinyinHelper.toHanyuPinyinStringArray(c, format); + output.append(temp[0].charAt(0)); + } else { + output.append(c); + } + } + } catch (BadHanyuPinyinOutputFormatCombination e) { + throw new RuntimeException("param=" + param + "中文名称转换出现异常", e); + } + return output.toString(); + } + +} diff --git a/src/main/java/com/jeesite/modules/util/PasswordHashUtil.java b/src/main/java/com/jeesite/modules/util/PasswordHashUtil.java new file mode 100644 index 0000000000000000000000000000000000000000..eda187e3a62875174bd58a194db056374083c089 --- /dev/null +++ b/src/main/java/com/jeesite/modules/util/PasswordHashUtil.java @@ -0,0 +1,129 @@ +package com.asiainfo.ai.admin.utils; + +import javax.crypto.Cipher; +import javax.crypto.SecretKey; +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.PBEKeySpec; +import javax.crypto.spec.SecretKeySpec; +import javax.xml.bind.DatatypeConverter; + +import org.springframework.util.Base64Utils; +import org.springframework.util.DigestUtils; + +import lombok.extern.slf4j.Slf4j; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.SecureRandom; +import java.security.spec.KeySpec; +import java.util.Base64; + +/** + * 符合业界安全标准的密码hash处理,涉及的加密算法不要改动!!! + * **/ +@Slf4j +public class PasswordHashUtil { + public static final String PBKDF2_ALGORITHM = "PBKDF2WithHmacSHA256"; + public static final int SALT_SIZE = 32; + public static final int HASH_SIZE = 256; + public static final int PBKDF2_ITERATIONS = 10000; + + public static void main(String[] args) throws Exception { + + } + + public static String decriptAES(String SecretText, String key, String iv) throws Exception { + try { + + byte[] keyBytes = key.getBytes(StandardCharsets.UTF_8); + byte[] ivBytes = iv.getBytes(StandardCharsets.UTF_8); + + SecretKeySpec secretKeySpec = new SecretKeySpec(keyBytes, "AES"); + IvParameterSpec ivParameterSpec = new IvParameterSpec(ivBytes); + + Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); + + cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec); + + byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(SecretText)); + String decryptedResult = new String(decryptedBytes, StandardCharsets.UTF_8); + + return decryptedResult; + } + catch (Exception e) { + String msg = "AES解密失败 - " + e.getMessage(); + log.error(msg, e); + throw new RuntimeException(msg); + } + + } + + public static String decryptByWeb(String userId, String passwdBase64) { + byte[] data = Base64Utils.decodeFromString(passwdBase64); + //这里需要md5是为了保证密钥长度 + byte[] key = DigestUtils.md5Digest(userId.getBytes(StandardCharsets.UTF_8)); + try { + SecretKey k = new SecretKeySpec(key, "AES"); + Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); + // 初始化,设置为加密模式 + cipher.init(Cipher.DECRYPT_MODE, k); + // 执行操作 + return new String(cipher.doFinal(data), StandardCharsets.UTF_8); + } catch (Exception e) { + throw new RuntimeException("fail to decrypt", e); + } + } + + public static String hash(String password, String salt) { + try { + return getPBKDF2(password, salt); + } + catch (Exception e) { + throw new RuntimeException("fail to hash", e); + } + } + + public static String getRandomSalt() { + try { + SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); + byte[] bytes = new byte[SALT_SIZE / 2]; + random.nextBytes(bytes); + return DatatypeConverter.printHexBinary(bytes); + } + catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** + * 前端form提交的加密方式 + * @Description + * @param key + * @param str + * @return + * @throws Exception + */ + public static String encryptByWeb(String userId, String pwd) throws Exception { + // 计算MD5摘要 + MessageDigest md = MessageDigest.getInstance("MD5"); + byte[] keyBytes = md.digest(userId.getBytes(StandardCharsets.UTF_8)); + SecretKeySpec secretKey = new SecretKeySpec(keyBytes, "AES"); + + // 创建AES加密器 + Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); + cipher.init(Cipher.ENCRYPT_MODE, secretKey); + + // 加密 + byte[] encryptedBytes = cipher.doFinal(pwd.getBytes(StandardCharsets.UTF_8)); + return Base64.getEncoder().encodeToString(encryptedBytes); + } + + private static String getPBKDF2(String password, String salt) throws Exception { + byte[] bytes = DatatypeConverter.parseHexBinary(salt); + KeySpec spec = new PBEKeySpec(password.toCharArray(), bytes, PBKDF2_ITERATIONS, HASH_SIZE); + SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(PBKDF2_ALGORITHM); + byte[] hash = secretKeyFactory.generateSecret(spec).getEncoded(); + return DatatypeConverter.printHexBinary(hash); + } +}