From f6ad52119ce752858edda6d5bf95f8e89a3e3e39 Mon Sep 17 00:00:00 2001 From: 13693261870 <252740454@qq.com> Date: 星期日, 25 八月 2024 19:33:38 +0800 Subject: [PATCH] 1 --- se-common/se-common-core/src/main/java/com/se/common/core/utils/AesUtils.java | 130 +++++++++++++++++++++ se-common/se-common-core/src/main/java/com/se/common/core/utils/RsaUtils.java | 212 +++++++++++++++++++++++++++++++++++ 2 files changed, 342 insertions(+), 0 deletions(-) diff --git a/se-common/se-common-core/src/main/java/com/se/common/core/utils/AesUtils.java b/se-common/se-common-core/src/main/java/com/se/common/core/utils/AesUtils.java new file mode 100644 index 0000000..1076e28 --- /dev/null +++ b/se-common/se-common-core/src/main/java/com/se/common/core/utils/AesUtils.java @@ -0,0 +1,130 @@ +package com.se.common.core.utils; + +import javax.crypto.Cipher; +import javax.crypto.spec.SecretKeySpec; +import java.util.Base64; + +/** + * AES鍔犲瘑宸ュ叿 + * + * @author WWW + * @date 2024-08-25 + */ +@SuppressWarnings("ALL") +public class AesUtils { + /** + * 瀵嗛挜闀垮害蹇呴』鏄�16 + */ + private static final String DEFAULT_KEY = "A#s_zZ_sErve_k.y"; + + private static final String KEY_ALGORITHM = "AES"; + + /** + * 绠楁硶 + */ + private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding"; + + /** + * aes瑙e瘑 + * + * @param encrypt 鍐呭 + * @return + * @throws Exception + */ + public static String decrypt(String encrypt) throws Exception { + return decrypt(encrypt, DEFAULT_KEY); + } + + /** + * aes鍔犲瘑 + * + * @param content 鍐呭 + * @return + * @throws Exception + */ + public static String encrypt(String content) throws Exception { + return encrypt(content, DEFAULT_KEY); + } + + /** + * base 64 encode + * + * @param bytes 寰呯紪鐮佺殑byte[] + * @return 缂栫爜鍚庣殑base64 code + */ + private static String base64Encode(byte[] bytes) { + return Base64.getEncoder().encodeToString(bytes); + } + + /** + * base 64 decode + * + * @param base64Code 寰呰В鐮佺殑base64 code + * @return 瑙g爜鍚庣殑byte[] + * @throws Exception + */ + private static byte[] base64Decode(String base64Code) { + return StringUtils.isEmpty(base64Code) ? null : Base64.getDecoder().decode(base64Code); + } + + /** + * AES鍔犲瘑 + * + * @param content 寰呭姞瀵嗙殑鍐呭 + * @param encryptKey 鍔犲瘑瀵嗛挜 + * @return 鍔犲瘑鍚庣殑byte[] + * @throws Exception + */ + private static byte[] aesEncryptToBytes(String content, String encryptKey) throws Exception { + // KeyGenerator kGen = KeyGenerator.getInstance(KEY_ALGORITHM) + // kGen.init(128) + + Cipher cipher = Cipher.getInstance(ALGORITHMSTR); + cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), KEY_ALGORITHM)); + + return cipher.doFinal(content.getBytes("utf-8")); + } + + /** + * AES鍔犲瘑涓篵ase 64 code + * + * @param content 寰呭姞瀵嗙殑鍐呭 + * @param encryptKey 鍔犲瘑瀵嗛挜 + * @return 鍔犲瘑鍚庣殑base 64 code + * @throws Exception + */ + public static String encrypt(String content, String encryptKey) throws Exception { + return base64Encode(aesEncryptToBytes(content, encryptKey)); + } + + /** + * 灏哹ase 64 code AES瑙e瘑 + * + * @param encryptStr 寰呰В瀵嗙殑base 64 code + * @param decryptKey 瑙e瘑瀵嗛挜 + * @return 瑙e瘑鍚庣殑string + * @throws Exception + */ + public static String decrypt(String encryptStr, String decryptKey) throws Exception { + return StringUtils.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr), decryptKey); + } + + /** + * AES瑙e瘑 + * + * @param encryptBytes 寰呰В瀵嗙殑byte[] + * @param decryptKey 瑙e瘑瀵嗛挜 + * @return 瑙e瘑鍚庣殑String + * @throws Exception + */ + private static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception { + // KeyGenerator kGen = KeyGenerator.getInstance(KEY_ALGORITHM) + // kGen.init(128) + + Cipher cipher = Cipher.getInstance(ALGORITHMSTR); + cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), KEY_ALGORITHM)); + byte[] decryptBytes = cipher.doFinal(encryptBytes); + + return new String(decryptBytes); + } +} diff --git a/se-common/se-common-core/src/main/java/com/se/common/core/utils/RsaUtils.java b/se-common/se-common-core/src/main/java/com/se/common/core/utils/RsaUtils.java new file mode 100644 index 0000000..27010cf --- /dev/null +++ b/se-common/se-common-core/src/main/java/com/se/common/core/utils/RsaUtils.java @@ -0,0 +1,212 @@ +package com.se.common.core.utils; + +import org.apache.commons.codec.binary.Base64; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.io.ClassPathResource; + +import javax.crypto.Cipher; +import java.io.*; +import java.nio.charset.StandardCharsets; +import java.security.*; +import java.security.interfaces.RSAPrivateKey; +import java.security.interfaces.RSAPublicKey; +import java.security.spec.PKCS8EncodedKeySpec; +import java.security.spec.X509EncodedKeySpec; +import java.util.HashMap; +import java.util.Map; + +/** + * RSA宸ュ叿绫� + * + * @author WWW + * @date 2024-08-25 + */ +public class RsaUtils { + /** + * 绉侀挜 + */ + private static String privateKey; + + /** + * 鍏挜 + */ + private static String publicKey; + + /** + * 瀵嗛挜绠楁硶 + */ + private static final String KEY_ALGORITHM = "RSA"; + + /** + * RSA瀵嗛挜闀垮害锛�1024 鎴� 2048 + */ + private static final int DEFAULT_RSA_KEY_SIZE = 1024; + + /** + * 鏃ュ織 + */ + private static final Logger log = LoggerFactory.getLogger(RsaUtils.class); + + /** + * 鐢熸垚鍏閽� + */ + public static void generate() { + Map<String, String> result = generateRsaKey(DEFAULT_RSA_KEY_SIZE); + System.out.println("鍏挜涓猴細" + result.get("publicKey")); + System.out.println("绉侀挜涓猴細" + result.get("privateKey")); + } + + /** + * 鑾峰彇RSA鍔犲瘑绉侀挜 + * + * @return + * @throws IOException + */ + public static String getPrivateKey() throws IOException { + if (privateKey == null) { + InputStream inPrivate = new ClassPathResource("config" + File.separator + "rsa_private_key.txt").getInputStream(); + privateKey = inputStream2String(inPrivate); + inPrivate.close(); + } + + return privateKey; + } + + /** + * 鑾峰彇RSA鍔犲瘑鍏挜 + * + * @return + * @throws IOException + */ + public static String getPublicKey() throws IOException { + if (publicKey == null) { + InputStream inPrivate = new ClassPathResource("config" + File.separator + "rsa_public_key.txt").getInputStream(); + publicKey = inputStream2String(inPrivate); + inPrivate.close(); + } + + return publicKey; + } + + /** + * 璇诲彇鏂囨湰鏂囦欢 + * + * @param fileName 鏂囦欢璺緞 + * @return + * @throws IOException + */ + public static String readFile(String fileName) throws IOException { + File file = new File(fileName); + BufferedReader br = new BufferedReader(new FileReader(file)); + + StringBuilder result = new StringBuilder(); + + String line = null; + while ((line = br.readLine()) != null) { + result.append(System.lineSeparator() + line); + } + br.close(); + + return result.toString(); + } + + /** + * 鎶奿nputStream杞垚String + * + * @param is + * @return + * @throws IOException + */ + private static String inputStream2String(InputStream is) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + int i = -1; + while ((i = is.read()) != -1) { + baos.write(i); + } + + String str = baos.toString(); + baos.close(); + + return str; + } + + /** + * 鐢熸垚RSA鐨勫叕绉侀挜 + * + * @param keySize 1025 鎴� 2048 + * @return + */ + public static Map<String, String> generateRsaKey(int keySize) { + Map<String, String> result = new HashMap<>(2); + try { + KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM); + + // 鍒濆鍖栧瘑閽ュ鐢熸垚鍣紝瀵嗛挜澶у皬涓�1024 2048浣� + keyPairGen.initialize(keySize, new SecureRandom()); + + // 鐢熸垚涓�涓瘑閽ュ锛屼繚瀛樺湪keyPair涓� + KeyPair keyPair = keyPairGen.generateKeyPair(); + + // 寰楀埌鍏挜瀛楃涓� + String pub = new String(Base64.encodeBase64(keyPair.getPublic().getEncoded())); + result.put("publicKey", pub); + + // 寰楀埌绉侀挜瀛楃涓� + String pri = new String(Base64.encodeBase64(keyPair.getPrivate().getEncoded())); + result.put("privateKey", pri); + } catch (Exception ex) { + log.error(ex.getMessage(), ex); + } + + return result; + } + + /** + * RSA绉侀挜瑙e瘑 + * + * @param str 鍔犲瘑鐨勫瓧绗︿覆 + * @return 瑙e瘑瀛楃涓� + * @throws Exception 鍔犲瘑杩囩▼涓殑寮傚父淇℃伅 + */ + public static String decrypt(String str) throws Exception { + // 64浣嶈В鐮佸姞瀵嗗悗鐨勫瓧绗︿覆 + byte[] inputByte = Base64.decodeBase64(str.getBytes(StandardCharsets.UTF_8)); + + // Base64缂栫爜鐨勭閽� + byte[] decoded = Base64.decodeBase64(getPrivateKey()); + RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded)); + + // RSA瑙e瘑锛歊SA/ECB/NoPadding + Cipher cipher = Cipher.getInstance("RSA"); + cipher.init(Cipher.DECRYPT_MODE, priKey); + + String outStr = new String(cipher.doFinal(inputByte)); + + return outStr; + } + + /** + * RSA鍏挜鍔犲瘑 + * + * @param str 闇�瑕佸姞瀵嗙殑瀛楃涓� + * @return 瀵嗘枃 + * @throws Exception 鍔犲瘑杩囩▼涓殑寮傚父淇℃伅 + */ + public static String encrypt(String str) throws Exception { + // Base64缂栫爜鐨勫叕閽� + byte[] decoded = Base64.decodeBase64(getPublicKey()); + + RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded)); + + // RSA鍔犲瘑锛歊SA/ECB/NoPadding + Cipher cipher = Cipher.getInstance("RSA"); + cipher.init(Cipher.ENCRYPT_MODE, pubKey); + + String outStr = Base64.encodeBase64String(cipher.doFinal(str.getBytes(StandardCharsets.UTF_8))); + + return outStr; + } +} + -- Gitblit v1.9.3