From bb9fd6f1fad1a20e7536fad50f7f8c7d932a9011 Mon Sep 17 00:00:00 2001 From: liusheng <337615773@qq.com> Date: 星期二, 02 九月 2025 23:17:36 +0800 Subject: [PATCH] 查询超时问题处理 --- ruoyi-common/src/main/java/com/ruoyi/common/utils/RSAPublicKeyExample.java | 143 +++++++++++++++++++++++++++++++++-------------- 1 files changed, 99 insertions(+), 44 deletions(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/RSAPublicKeyExample.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/RSAPublicKeyExample.java index 196de03..1496ae7 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/RSAPublicKeyExample.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/RSAPublicKeyExample.java @@ -19,37 +19,71 @@ @Component public class RSAPublicKeyExample { - - /** - * 鏁版嵁瑙e瘑 - * - * @param encryptedData - * @return - */ - public String decryptedData(String encryptedData, String pri_key) { - String privateKeyString = "绉侀挜鐨凚ase64缂栫爜瀛楃涓�"; // 鍚庣绉侀挜鐨凚ase64缂栫爜瀛楃涓� - - try { - // 灏嗙閽ase64缂栫爜瀛楃涓茶浆鎹负PrivateKey瀵硅薄 - byte[] privateKeyBytes = Base64.getDecoder().decode(pri_key); - PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes); - KeyFactory keyFactory = KeyFactory.getInstance("RSA"); - PrivateKey privateKey = keyFactory.generatePrivate(keySpec); - - // 浣跨敤绉侀挜瑙e瘑鏁版嵁 - Cipher decryptCipher = Cipher.getInstance("RSA"); - decryptCipher.init(Cipher.DECRYPT_MODE, privateKey); - byte[] decryptedBytes = decryptCipher.doFinal(Base64.getDecoder().decode(encryptedData)); - - // 瑙e瘑鍚庣殑鏁版嵁 - String decryptedData = new String(decryptedBytes); - System.out.println("瑙e瘑鍚庣殑鏁版嵁锛�" + decryptedData); - return decryptedData; - } catch (Exception e) { - log.error("瑙e瘑鎶ラ敊浜�:{}", e.getMessage()); - } - return null; - } +// +// /** +// * 鏁版嵁瑙e瘑 +// * +// * @param encryptedData +// * @return +// */ +// public String decryptedData(String encryptedData, String pri_key) { +// String privateKeyString = "绉侀挜鐨凚ase64缂栫爜瀛楃涓�"; // 鍚庣绉侀挜鐨凚ase64缂栫爜瀛楃涓� +// +// try { +// // 灏嗙閽ase64缂栫爜瀛楃涓茶浆鎹负PrivateKey瀵硅薄 +// byte[] privateKeyBytes = Base64.getDecoder().decode(pri_key); +// PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes); +// KeyFactory keyFactory = KeyFactory.getInstance("RSA"); +// PrivateKey privateKey = keyFactory.generatePrivate(keySpec); +// +// // 浣跨敤绉侀挜瑙e瘑鏁版嵁 +// Cipher decryptCipher = Cipher.getInstance("RSA"); +// decryptCipher.init(Cipher.DECRYPT_MODE, privateKey); +// byte[] decryptedBytes = decryptCipher.doFinal(Base64.getDecoder().decode(encryptedData)); +// +// // 瑙e瘑鍚庣殑鏁版嵁 +// String decryptedData = new String(decryptedBytes); +// System.out.println("瑙e瘑鍚庣殑鏁版嵁锛�" + decryptedData); +// return decryptedData; +// } catch (Exception e) { +// log.error("瑙e瘑鎶ラ敊浜�:{}", e.getMessage()); +// } +// return null; +// } +// +// /** +// * 瑕佸姞瀵嗙殑鏄庢枃鏁版嵁 +// * +// * @param plainText +// * @return +// */ +// public String encryptedData(String plainText, String pub_key) { +// log.info("闇�瑕佸姞瀵嗙殑鏁版嵁锛歿}", plainText); +// try { +// +// byte[] publicKeyBytes = Base64.getDecoder().decode(pub_key); +// X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes); +// KeyFactory keyFactory = KeyFactory.getInstance("RSA"); +// PublicKey publicKey = keyFactory.generatePublic(keySpec); +// +// // 浣跨敤鍏挜鍔犲瘑鏁版嵁 +// Cipher encryptCipher = Cipher.getInstance("RSA"); +// encryptCipher.init(Cipher.ENCRYPT_MODE, publicKey); +// byte[] encryptedBytes = encryptCipher.doFinal(plainText.getBytes()); +// +// // 灏嗗姞瀵嗗悗鐨勬暟鎹浆鎹负Base64缂栫爜鐨勫瓧绗︿覆 +// String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes); +// log.info("Base64鍔犲瘑鍚庣殑鏁版嵁锛歿}", encryptedText); +// encryptedText = URLEncoder.encode(encryptedText, StandardCharsets.UTF_8.toString()); +// log.info("URLEncoder缂栫爜鍚庣殑鏁版嵁锛歿}", encryptedText); +// String decodedString = URLDecoder.decode(encryptedText, "UTF-8"); +// log.info("URLEncoder瑙g爜鍚庣殑鏁版嵁锛歿}", decodedString); +// return encryptedText; +// } catch (Exception e) { +// log.error("鍔犲瘑澶辫触浜�:{}", e.getMessage()); +// } +// return null; +// } /** * 瑕佸姞瀵嗙殑鏄庢枃鏁版嵁 @@ -58,29 +92,50 @@ * @return */ public String encryptedData(String plainText, String pub_key) { - log.info("闇�瑕佸姞瀵嗙殑鏁版嵁锛歿}", plainText); try { - byte[] publicKeyBytes = Base64.getDecoder().decode(pub_key); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PublicKey publicKey = keyFactory.generatePublic(keySpec); // 浣跨敤鍏挜鍔犲瘑鏁版嵁 - Cipher encryptCipher = Cipher.getInstance("RSA"); + Cipher encryptCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); encryptCipher.init(Cipher.ENCRYPT_MODE, publicKey); - byte[] encryptedBytes = encryptCipher.doFinal(plainText.getBytes()); + byte[] encryptedBytes = encryptCipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8)); - // 灏嗗姞瀵嗗悗鐨勬暟鎹浆鎹负Base64缂栫爜鐨勫瓧绗︿覆 - String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes); - log.info("Base64鍔犲瘑鍚庣殑鏁版嵁锛歿}", encryptedText); - encryptedText = URLEncoder.encode(encryptedText, StandardCharsets.UTF_8.toString()); - log.info("URLEncoder缂栫爜鍚庣殑鏁版嵁锛歿}", encryptedText); - String decodedString = URLDecoder.decode(encryptedText, "UTF-8"); - log.info("URLEncoder瑙g爜鍚庣殑鏁版嵁锛歿}", decodedString); - return encryptedText; + // URL Safe Base64 缂栫爜 + return Base64.getUrlEncoder().encodeToString(encryptedBytes); } catch (Exception e) { - log.error("鍔犲瘑澶辫触浜�:{}", e.getMessage()); + log.error("鍔犲瘑澶辫触: {}", e.getMessage()); + } + return null; + } + + /** + * 鏁版嵁瑙e瘑 + * + * @param encryptedData + * @return + */ + public String decryptedData(String encryptedData, String pri_key) { + try { + // 灏嗙閽ase64缂栫爜瀛楃涓茶浆鎹负PrivateKey瀵硅薄 + byte[] privateKeyBytes = Base64.getDecoder().decode(pri_key); + PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes); + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + PrivateKey privateKey = keyFactory.generatePrivate(keySpec); + + // URL Safe Base64 瑙g爜 + byte[] cipherBytes = Base64.getUrlDecoder().decode(encryptedData); + + // 浣跨敤绉侀挜瑙e瘑鏁版嵁 + Cipher decryptCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); + decryptCipher.init(Cipher.DECRYPT_MODE, privateKey); + byte[] decryptedBytes = decryptCipher.doFinal(cipherBytes); + + return new String(decryptedBytes, StandardCharsets.UTF_8); + } catch (Exception e) { + log.error("瑙e瘑澶辫触: {}", e.getMessage()); } return null; } -- Gitblit v1.9.3