From c111e3eff1191b29d2d2baf2f485a4bf3a2edc00 Mon Sep 17 00:00:00 2001 From: liusheng <337615773@qq.com> Date: 星期六, 16 八月 2025 14:34:07 +0800 Subject: [PATCH] 代码提交 --- ruoyi-common/src/main/java/com/ruoyi/common/utils/RSAPublicKeyExample.java | 95 ++++++++++++++++++++--------------------------- 1 files changed, 41 insertions(+), 54 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 2159f6d..4570e97 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 @@ -20,71 +20,58 @@ public class RSAPublicKeyExample { - /** - * 鏁版嵁瑙e瘑 - * - * @param encryptedData - * @return - */ - public String decryptedData(String encryptedData, String pri_key) { - String privateKeyString = "绉侀挜鐨凚ase64缂栫爜瀛楃涓�"; // 鍚庣绉侀挜鐨凚ase64缂栫爜瀛楃涓� - + public static String encryptedData(String plainText, String pubKey) { 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.error("闇�瑕佸姞瀵嗙殑鏁版嵁锛歿}", plainText); - try { - - byte[] publicKeyBytes = Base64.getDecoder().decode(pub_key); + byte[] publicKeyBytes = Base64.getDecoder().decode(pubKey); 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()); + Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); + cipher.init(Cipher.ENCRYPT_MODE, publicKey); + byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8)); - // 灏嗗姞瀵嗗悗鐨勬暟鎹浆鎹负Base64缂栫爜鐨勫瓧绗︿覆 + // Base64 缂栫爜锛堝繀椤讳繚鐣欙級 String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes); - log.error("Base64鍔犲瘑鍚庣殑鏁版嵁锛歿}", encryptedText); - encryptedText = URLEncoder.encode(encryptedText, StandardCharsets.UTF_8.toString()); - log.error("URLEncoder缂栫爜鍚庣殑鏁版嵁锛歿}", encryptedText); - String decodedString = URLDecoder.decode(encryptedText, "UTF-8"); - log.error("URLEncoder瑙g爜鍚庣殑鏁版嵁锛歿}", decodedString); - return encryptedText; + + // 鍙�夛細濡傛灉鐢ㄤ簬 URL 浼犺緭锛屽啀杩涜 URL 缂栫爜 + return URLEncoder.encode(encryptedText, StandardCharsets.UTF_8.toString()); } catch (Exception e) { - log.error("鍔犲瘑澶辫触浜�:{}", e.getMessage()); + System.err.println("鍔犲瘑澶辫触: " + e.getMessage()); + e.printStackTrace(); + return null; } - return null; } + /** + * RSA 绉侀挜瑙e瘑锛堣В瀵� Base64 缂栫爜鍚庣殑瀵嗘枃锛屾敮鎸� URL 瑙g爜锛� + */ + public static String decryptedData(String encryptedData, String priKey) { + try { + // 鍙�夛細鍏� URL 瑙g爜锛堝鏋滃姞瀵嗗墠鍋氫簡 URLEncoder锛� + String base64Encrypted = URLDecoder.decode(encryptedData, StandardCharsets.UTF_8.toString()); + + // Base64 瑙g爜 + byte[] encryptedBytes = Base64.getDecoder().decode(base64Encrypted); + + byte[] privateKeyBytes = Base64.getDecoder().decode(priKey); + PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes); + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + PrivateKey privateKey = keyFactory.generatePrivate(keySpec); + + Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); + cipher.init(Cipher.DECRYPT_MODE, privateKey); + byte[] decryptedBytes = cipher.doFinal(encryptedBytes); + + return new String(decryptedBytes, StandardCharsets.UTF_8); + } catch (Exception e) { + System.err.println("瑙e瘑澶辫触: " + e.getMessage()); + e.printStackTrace(); + return null; + } + } + + public static void main(String[] args) { String decodedString = null; try { -- Gitblit v1.9.3