Skip to content

Commit

Permalink
style: 优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
KouShenhai committed Jul 20, 2024
1 parent fcbbc10 commit 4ad73fa
Showing 1 changed file with 24 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,91 +58,71 @@ 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) {
try {
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) {
try {
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() {
Expand All @@ -150,6 +131,7 @@ public static String getPublicKey() {

/**
* base64解密.
*
* @param body 数据
* @return 解密后的字符串
*/
Expand All @@ -159,6 +141,7 @@ private static byte[] decryptBase64(String body) {

/**
* base64加密.
*
* @param bodyBytes 数据
* @return 加密后的字符串
*/
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 4ad73fa

Please sign in to comment.