From 4ad73fa1ad4aff6447643467d0874db333a85a8f Mon Sep 17 00:00:00 2001 From: koushenhai <2413176044@qq.com> Date: Sun, 21 Jul 2024 00:22:47 +0800 Subject: [PATCH] =?UTF-8?q?style:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../laokou/common/crypto/utils/RSAUtil.java | 61 ++++++++----------- 1 file changed, 24 insertions(+), 37 deletions(-) diff --git a/laokou-common/laokou-common-crypto/src/main/java/org/laokou/common/crypto/utils/RSAUtil.java b/laokou-common/laokou-common-crypto/src/main/java/org/laokou/common/crypto/utils/RSAUtil.java index 67abb6e5bd..a465b90800 100644 --- a/laokou-common/laokou-common-crypto/src/main/java/org/laokou/common/crypto/utils/RSAUtil.java +++ b/laokou-common/laokou-common-crypto/src/main/java/org/laokou/common/crypto/utils/RSAUtil.java @@ -17,6 +17,7 @@ package org.laokou.common.crypto.utils; +import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.laokou.common.i18n.utils.ObjectUtil; import org.laokou.common.i18n.utils.ResourceUtil; @@ -57,19 +58,19 @@ public class RSAUtil { static { try (InputStream inputStream1 = ResourceUtil.getResource("/conf/publicKey.scr").getInputStream(); - InputStream inputStream2 = ResourceUtil.getResource("/conf/privateKey.scr").getInputStream()) { + InputStream inputStream2 = ResourceUtil.getResource("/conf/privateKey.scr").getInputStream()) { PUBLIC_KEY = new String(inputStream1.readAllBytes(), StandardCharsets.UTF_8).trim(); PRIVATE_KEY = new String(inputStream2.readAllBytes(), StandardCharsets.UTF_8).trim(); - } - catch (IOException e) { + } catch (IOException e) { throw new RuntimeException(e); } } /** * 根据私钥解密. + * * @param body 数据 - * @param key 私钥 + * @param key 私钥 * @return 解密后的字符串 */ public static String decryptByPrivateKey(String body, String key) { @@ -77,32 +78,26 @@ public static String decryptByPrivateKey(String body, String key) { byte[] privateKey = StringUtil.isNotEmpty(key) ? decryptBase64(key) : decryptBase64(PRIVATE_KEY); byte[] bytes = decryptByPrivateKey(decryptBase64(body), privateKey); return new String(ObjectUtil.requireNotNull(bytes), StandardCharsets.UTF_8); - } - catch (Exception e) { + } catch (Exception e) { return body; } } /** * 根据私钥解密. + * * @param body 数据 * @return 解密后的字符串 */ public static String decryptByPrivateKey(String body) { - try { - byte[] privateKey = decryptBase64(PRIVATE_KEY); - byte[] bytes = decryptByPrivateKey(decryptBase64(body), privateKey); - return new String(ObjectUtil.requireNotNull(bytes), StandardCharsets.UTF_8); - } - catch (Exception e) { - return body; - } + return decryptByPrivateKey(body, PRIVATE_KEY); } /** * 根据公钥加密. + * * @param body 数据 - * @param key 公钥 + * @param key 公钥 * @return 加密后的字符串 */ public static String encryptByPublicKey(String body, String key) { @@ -110,38 +105,24 @@ public static String encryptByPublicKey(String body, String key) { byte[] publicKey = StringUtil.isNotEmpty(key) ? decryptBase64(key) : decryptBase64(PUBLIC_KEY); byte[] bytes = encryptByPublicKey(body.getBytes(StandardCharsets.UTF_8), publicKey); return encryptBase64(bytes); - } - catch (Exception e) { + } catch (Exception e) { return body; } } /** * 根据公钥加密. + * * @param body 数据 * @return 加密后的字符串 */ public static String encryptByPublicKey(String body) { - try { - byte[] publicKey = decryptBase64(PUBLIC_KEY); - byte[] bytes = encryptByPublicKey(body.getBytes(StandardCharsets.UTF_8), publicKey); - return encryptBase64(bytes); - } - catch (Exception e) { - return body; - } - } - - /** - * 获取私钥. - * @return 私钥 - */ - public static String getPrivateKey() { - return PRIVATE_KEY; + return encryptByPublicKey(body, PUBLIC_KEY); } /** * 获取公钥. + * * @return 公钥 */ public static String getPublicKey() { @@ -150,6 +131,7 @@ public static String getPublicKey() { /** * base64解密. + * * @param body 数据 * @return 解密后的字符串 */ @@ -159,6 +141,7 @@ private static byte[] decryptBase64(String body) { /** * base64加密. + * * @param bodyBytes 数据 * @return 加密后的字符串 */ @@ -168,11 +151,13 @@ private static String encryptBase64(byte[] bodyBytes) { /** * 根据公钥加密. + * * @param bodyBytes 加密字符 - * @param keyBytes 公钥 + * @param keyBytes 公钥 * @return 加密后的字符串 */ - private static byte[] encryptByPublicKey(byte[] bodyBytes, byte[] keyBytes) throws Exception { + @SneakyThrows + private static byte[] encryptByPublicKey(byte[] bodyBytes, byte[] keyBytes) { X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(RSA, SUN_RSA_SIGN_PROVIDER); PublicKey publicKey = keyFactory.generatePublic(x509KeySpec); @@ -183,11 +168,13 @@ private static byte[] encryptByPublicKey(byte[] bodyBytes, byte[] keyBytes) thro /** * 根据私钥解密. + * * @param bodyBytes 加密字符 - * @param keyBytes 私钥 + * @param keyBytes 私钥 * @return 解密后的字符串 */ - private static byte[] decryptByPrivateKey(byte[] bodyBytes, byte[] keyBytes) throws Exception { + @SneakyThrows + private static byte[] decryptByPrivateKey(byte[] bodyBytes, byte[] keyBytes) { PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(RSA, SUN_RSA_SIGN_PROVIDER); Key privateKey = keyFactory.generatePrivate(pkcs8KeySpec);