From ff8f051d296fc0584deb919925bfbca94b41a423 Mon Sep 17 00:00:00 2001 From: bynt Date: Mon, 15 May 2023 11:11:00 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=8A=A0=E5=AF=86=E5=8F=82?= =?UTF-8?q?=E6=95=B0=20=E4=B8=8A=E4=BC=A0=E6=95=B0=E9=87=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../baiye/constant/AdPlatFormConstants.java | 4 + .../main/java/com/baiye/util/EncryptUtil.java | 101 +++++++++++++++--- 2 files changed, 92 insertions(+), 13 deletions(-) diff --git a/ad-platform-common/ad-platform-common-core/src/main/java/com/baiye/constant/AdPlatFormConstants.java b/ad-platform-common/ad-platform-common-core/src/main/java/com/baiye/constant/AdPlatFormConstants.java index 8d644c4e..5de801c0 100644 --- a/ad-platform-common/ad-platform-common-core/src/main/java/com/baiye/constant/AdPlatFormConstants.java +++ b/ad-platform-common/ad-platform-common-core/src/main/java/com/baiye/constant/AdPlatFormConstants.java @@ -10,5 +10,9 @@ public class AdPlatFormConstants { public static final String AD_PLATFORM = "ad-platform"; + public static final String PLATFORM_USER = "platform-user"; + + + public static final String PLAT_DECRYPTION = "CXpRNIJC4xlZd6vk"; } diff --git a/ad-platform-common/ad-platform-common-core/src/main/java/com/baiye/util/EncryptUtil.java b/ad-platform-common/ad-platform-common-core/src/main/java/com/baiye/util/EncryptUtil.java index 82e1ce22..4c32e948 100644 --- a/ad-platform-common/ad-platform-common-core/src/main/java/com/baiye/util/EncryptUtil.java +++ b/ad-platform-common/ad-platform-common-core/src/main/java/com/baiye/util/EncryptUtil.java @@ -1,17 +1,20 @@ package com.baiye.util; -import cn.hutool.core.codec.Base64; import cn.hutool.core.util.CharsetUtil; -import com.baiye.constant.AdPlatFormConstants; +import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; +import sun.misc.BASE64Decoder; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.Mac; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; +import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.SecureRandom; +import java.util.Base64; import java.util.Objects; /** @@ -28,10 +31,16 @@ public class EncryptUtil { public static final String HmacSHA1 = "HmacSHA1"; public static final String DES = "DES"; public static final String AES = "AES"; + + /** * 签名算法 */ public static final String SIGN_ALGORITHMS = "SHA1PRNG"; + + + private static final String ALGORITHMSTR = "AES/ECB/PKCS5Padding"; + /** * 编码格式;默认使用uft-8 */ @@ -152,7 +161,7 @@ public class EncryptUtil { } private String base64(byte[] res) { - return Base64.encode(res); + return Base64.getEncoder().encodeToString(res); } /** @@ -313,24 +322,90 @@ public class EncryptUtil { return res ^ key.hashCode(); } + /** - * 使用Base64进行加密 + * base 64 encode * - * @param res 密文 - * @return + * @param bytes 待编码的byte[] + * @return 编码后的base 64 code */ - public String Base64Encode(String res) { - return Base64.encode(res.getBytes()); + public static String base64Encode(byte[] bytes) { + return Base64.getEncoder().encodeToString(bytes); } /** - * 使用Base64进行解密 + * base 64 decode * - * @param res - * @return + * @param base64Code 待解码的base 64 code + * @return 解码后的byte[] + * @throws Exception */ - public String Base64Decode(String res) { - return new String(Base64.decode(res)); + public static byte[] base64Decode(String base64Code) throws Exception { + return StringUtils.isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code); } + + /** + * AES加密 + * + * @param content 待加密的内容 + * @param encryptKey 加密密钥 + * @return 加密后的byte[] + * @throws Exception + */ + @SneakyThrows + public static byte[] aesEncryptToBytes(String content, String encryptKey) { + KeyGenerator kgen = KeyGenerator.getInstance(AES); + kgen.init(128); + Cipher cipher = Cipher.getInstance(ALGORITHMSTR); + cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), AES)); + return cipher.doFinal(content.getBytes(StandardCharsets.UTF_8)); + + } + + + /** + * AES加密为base 64 code + * + * @param content 待加密的内容 + * @param encryptKey 加密密钥 + * @return 加密后的base 64 code + * @throws Exception + */ + public static String aesEncrypt(String content, String encryptKey) { + return base64Encode(aesEncryptToBytes(content, encryptKey)); + } + + /** + * AES解密 + * + * @param encryptBytes 待解密的byte[] + * @param decryptKey 解密密钥 + * @return 解密后的String + * @throws Exception + */ + public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception { + KeyGenerator kgen = KeyGenerator.getInstance(AES); + kgen.init(128); + + Cipher cipher = Cipher.getInstance(ALGORITHMSTR); + cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), AES)); + byte[] decryptBytes = cipher.doFinal(encryptBytes); + return new String(decryptBytes); + } + + + /** + * 将base 64 code AES解密 + * + * @param encryptStr 待解密的base 64 code + * @param decryptKey 解密密钥 + * @return 解密后的string + * @throws Exception + */ + public static String aesDecrypt(String encryptStr, String decryptKey) throws Exception { + return StringUtils.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr), decryptKey); + } + + }