diff --git a/ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/core/encryptor/AesEncryptor.java b/ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/core/encryptor/AesEncryptor.java index 228a3627..e4dc5976 100644 --- a/ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/core/encryptor/AesEncryptor.java +++ b/ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/core/encryptor/AesEncryptor.java @@ -1,14 +1,9 @@ package org.dromara.common.encrypt.core.encryptor; -import cn.hutool.core.util.ArrayUtil; -import cn.hutool.core.util.StrUtil; -import cn.hutool.crypto.SecureUtil; -import cn.hutool.crypto.symmetric.AES; import org.dromara.common.encrypt.core.EncryptContext; import org.dromara.common.encrypt.enumd.AlgorithmType; import org.dromara.common.encrypt.enumd.EncodeType; - -import java.nio.charset.StandardCharsets; +import org.dromara.common.encrypt.utils.EncryptUtils; /** * AES算法实现 @@ -18,20 +13,11 @@ import java.nio.charset.StandardCharsets; */ public class AesEncryptor extends AbstractEncryptor { - private final AES aes; + private final EncryptContext context; public AesEncryptor(EncryptContext context) { super(context); - String password = context.getPassword(); - if (StrUtil.isBlank(password)) { - throw new IllegalArgumentException("AES没有获得秘钥信息"); - } - // aes算法的秘钥要求是16位、24位、32位 - int[] array = {16, 24, 32}; - if (!ArrayUtil.contains(array, password.length())) { - throw new IllegalArgumentException("AES秘钥长度应该为16位、24位、32位,实际为" + password.length() + "位"); - } - aes = SecureUtil.aes(context.getPassword().getBytes(StandardCharsets.UTF_8)); + this.context = context; } /** @@ -51,9 +37,9 @@ public class AesEncryptor extends AbstractEncryptor { @Override public String encrypt(String value, EncodeType encodeType) { if (encodeType == EncodeType.HEX) { - return aes.encryptHex(value); + return EncryptUtils.encryptByAesHex(value, context.getPassword()); } else { - return aes.encryptBase64(value); + return EncryptUtils.encryptByAes(value, context.getPassword()); } } @@ -64,6 +50,6 @@ public class AesEncryptor extends AbstractEncryptor { */ @Override public String decrypt(String value) { - return this.aes.decryptStr(value); + return EncryptUtils.decryptByAes(value, context.getPassword()); } } diff --git a/ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/core/encryptor/Base64Encryptor.java b/ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/core/encryptor/Base64Encryptor.java index 40039543..00285483 100644 --- a/ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/core/encryptor/Base64Encryptor.java +++ b/ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/core/encryptor/Base64Encryptor.java @@ -1,9 +1,9 @@ package org.dromara.common.encrypt.core.encryptor; -import cn.hutool.core.codec.Base64; import org.dromara.common.encrypt.core.EncryptContext; import org.dromara.common.encrypt.enumd.AlgorithmType; import org.dromara.common.encrypt.enumd.EncodeType; +import org.dromara.common.encrypt.utils.EncryptUtils; /** * Base64算法实现 @@ -33,7 +33,7 @@ public class Base64Encryptor extends AbstractEncryptor { */ @Override public String encrypt(String value, EncodeType encodeType) { - return Base64.encode(value); + return EncryptUtils.encryptByBase64(value); } /** @@ -43,6 +43,6 @@ public class Base64Encryptor extends AbstractEncryptor { */ @Override public String decrypt(String value) { - return Base64.decodeStr(value); + return EncryptUtils.decryptByBase64(value); } } diff --git a/ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/core/encryptor/RsaEncryptor.java b/ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/core/encryptor/RsaEncryptor.java index 61b1dffe..5f03a4b4 100644 --- a/ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/core/encryptor/RsaEncryptor.java +++ b/ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/core/encryptor/RsaEncryptor.java @@ -1,13 +1,10 @@ package org.dromara.common.encrypt.core.encryptor; -import cn.hutool.core.codec.Base64; -import cn.hutool.crypto.SecureUtil; -import cn.hutool.crypto.asymmetric.KeyType; -import cn.hutool.crypto.asymmetric.RSA; import org.dromara.common.core.utils.StringUtils; import org.dromara.common.encrypt.core.EncryptContext; import org.dromara.common.encrypt.enumd.AlgorithmType; import org.dromara.common.encrypt.enumd.EncodeType; +import org.dromara.common.encrypt.utils.EncryptUtils; /** @@ -18,7 +15,7 @@ import org.dromara.common.encrypt.enumd.EncodeType; */ public class RsaEncryptor extends AbstractEncryptor { - private final RSA rsa; + private final EncryptContext context; public RsaEncryptor(EncryptContext context) { super(context); @@ -27,7 +24,7 @@ public class RsaEncryptor extends AbstractEncryptor { if (StringUtils.isAnyEmpty(privateKey, publicKey)) { throw new IllegalArgumentException("RSA公私钥均需要提供,公钥加密,私钥解密。"); } - this.rsa = SecureUtil.rsa(Base64.decode(privateKey), Base64.decode(publicKey)); + this.context = context; } /** @@ -47,9 +44,9 @@ public class RsaEncryptor extends AbstractEncryptor { @Override public String encrypt(String value, EncodeType encodeType) { if (encodeType == EncodeType.HEX) { - return rsa.encryptHex(value, KeyType.PublicKey); + return EncryptUtils.encryptByRsaHex(value, context.getPublicKey()); } else { - return rsa.encryptBase64(value, KeyType.PublicKey); + return EncryptUtils.encryptByRsa(value, context.getPublicKey()); } } @@ -60,6 +57,6 @@ public class RsaEncryptor extends AbstractEncryptor { */ @Override public String decrypt(String value) { - return this.rsa.decryptStr(value, KeyType.PrivateKey); + return EncryptUtils.decryptByRsa(value, context.getPrivateKey()); } } diff --git a/ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/core/encryptor/Sm2Encryptor.java b/ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/core/encryptor/Sm2Encryptor.java index c728cf83..aec5d826 100644 --- a/ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/core/encryptor/Sm2Encryptor.java +++ b/ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/core/encryptor/Sm2Encryptor.java @@ -1,13 +1,10 @@ package org.dromara.common.encrypt.core.encryptor; -import cn.hutool.core.codec.Base64; -import cn.hutool.crypto.SmUtil; -import cn.hutool.crypto.asymmetric.KeyType; -import cn.hutool.crypto.asymmetric.SM2; import org.dromara.common.core.utils.StringUtils; import org.dromara.common.encrypt.core.EncryptContext; import org.dromara.common.encrypt.enumd.AlgorithmType; import org.dromara.common.encrypt.enumd.EncodeType; +import org.dromara.common.encrypt.utils.EncryptUtils; /** * sm2算法实现 @@ -17,7 +14,7 @@ import org.dromara.common.encrypt.enumd.EncodeType; */ public class Sm2Encryptor extends AbstractEncryptor { - private final SM2 sm2; + private final EncryptContext context; public Sm2Encryptor(EncryptContext context) { super(context); @@ -26,7 +23,7 @@ public class Sm2Encryptor extends AbstractEncryptor { if (StringUtils.isAnyEmpty(privateKey, publicKey)) { throw new IllegalArgumentException("SM2公私钥均需要提供,公钥加密,私钥解密。"); } - this.sm2 = SmUtil.sm2(Base64.decode(privateKey), Base64.decode(publicKey)); + this.context = context; } /** @@ -46,9 +43,9 @@ public class Sm2Encryptor extends AbstractEncryptor { @Override public String encrypt(String value, EncodeType encodeType) { if (encodeType == EncodeType.HEX) { - return sm2.encryptHex(value, KeyType.PublicKey); + return EncryptUtils.encryptBySm2Hex(value, context.getPublicKey()); } else { - return sm2.encryptBase64(value, KeyType.PublicKey); + return EncryptUtils.encryptBySm2(value, context.getPublicKey()); } } @@ -59,6 +56,6 @@ public class Sm2Encryptor extends AbstractEncryptor { */ @Override public String decrypt(String value) { - return this.sm2.decryptStr(value, KeyType.PrivateKey); + return EncryptUtils.decryptBySm2(value, context.getPrivateKey()); } } diff --git a/ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/core/encryptor/Sm4Encryptor.java b/ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/core/encryptor/Sm4Encryptor.java index 2a32faf2..adaf6748 100644 --- a/ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/core/encryptor/Sm4Encryptor.java +++ b/ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/core/encryptor/Sm4Encryptor.java @@ -1,13 +1,9 @@ package org.dromara.common.encrypt.core.encryptor; -import cn.hutool.core.util.StrUtil; -import cn.hutool.crypto.SmUtil; -import cn.hutool.crypto.symmetric.SM4; import org.dromara.common.encrypt.core.EncryptContext; import org.dromara.common.encrypt.enumd.AlgorithmType; import org.dromara.common.encrypt.enumd.EncodeType; - -import java.nio.charset.StandardCharsets; +import org.dromara.common.encrypt.utils.EncryptUtils; /** * sm4算法实现 @@ -17,19 +13,11 @@ import java.nio.charset.StandardCharsets; */ public class Sm4Encryptor extends AbstractEncryptor { - private final SM4 sm4; + private final EncryptContext context; public Sm4Encryptor(EncryptContext context) { super(context); - String password = context.getPassword(); - if (StrUtil.isBlank(password)) { - throw new IllegalArgumentException("SM4没有获得秘钥信息"); - } - // sm4算法的秘钥要求是16位长度 - if (16 != password.length()) { - throw new IllegalArgumentException("SM4秘钥长度应该为16位,实际为" + password.length() + "位"); - } - this.sm4 = SmUtil.sm4(password.getBytes(StandardCharsets.UTF_8)); + this.context = context; } /** @@ -49,9 +37,9 @@ public class Sm4Encryptor extends AbstractEncryptor { @Override public String encrypt(String value, EncodeType encodeType) { if (encodeType == EncodeType.HEX) { - return sm4.encryptHex(value); + return EncryptUtils.encryptBySm4Hex(value, context.getPassword()); } else { - return sm4.encryptBase64(value); + return EncryptUtils.encryptBySm4(value, context.getPassword()); } } @@ -62,6 +50,6 @@ public class Sm4Encryptor extends AbstractEncryptor { */ @Override public String decrypt(String value) { - return this.sm4.decryptStr(value); + return EncryptUtils.decryptBySm4(value, context.getPassword()); } } diff --git a/ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/utils/EncryptUtils.java b/ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/utils/EncryptUtils.java index 3504573c..8e34843a 100644 --- a/ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/utils/EncryptUtils.java +++ b/ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/utils/EncryptUtils.java @@ -67,6 +67,25 @@ public class EncryptUtils { return SecureUtil.aes(password.getBytes(StandardCharsets.UTF_8)).encryptBase64(data, StandardCharsets.UTF_8); } + /** + * AES加密 + * + * @param data 待解密数据 + * @param password 秘钥字符串 + * @return 加密后字符串, 采用Hex编码 + */ + public static String encryptByAesHex(String data, String password) { + if (StrUtil.isBlank(password)) { + throw new IllegalArgumentException("AES需要传入秘钥信息"); + } + // aes算法的秘钥要求是16位、24位、32位 + int[] array = {16, 24, 32}; + if (!ArrayUtil.contains(array, password.length())) { + throw new IllegalArgumentException("AES秘钥长度要求为16位、24位、32位"); + } + return SecureUtil.aes(password.getBytes(StandardCharsets.UTF_8)).encryptHex(data, StandardCharsets.UTF_8); + } + /** * AES解密 * @@ -105,6 +124,25 @@ public class EncryptUtils { return SmUtil.sm4(password.getBytes(StandardCharsets.UTF_8)).encryptBase64(data, StandardCharsets.UTF_8); } + /** + * sm4加密 + * + * @param data 待加密数据 + * @param password 秘钥字符串 + * @return 加密后字符串, 采用Base64编码 + */ + public static String encryptBySm4Hex(String data, String password) { + if (StrUtil.isBlank(password)) { + throw new IllegalArgumentException("SM4需要传入秘钥信息"); + } + // sm4算法的秘钥要求是16位长度 + int sm4PasswordLength = 16; + if (sm4PasswordLength != password.length()) { + throw new IllegalArgumentException("SM4秘钥长度要求为16位"); + } + return SmUtil.sm4(password.getBytes(StandardCharsets.UTF_8)).encryptHex(data, StandardCharsets.UTF_8); + } + /** * sm4解密 * @@ -152,6 +190,21 @@ public class EncryptUtils { return sm2.encryptBase64(data, StandardCharsets.UTF_8, KeyType.PublicKey); } + /** + * sm2公钥加密 + * + * @param data 待加密数据 + * @param publicKey 公钥 + * @return 加密后字符串, 采用Hex编码 + */ + public static String encryptBySm2Hex(String data, String publicKey) { + if (StrUtil.isBlank(publicKey)) { + throw new IllegalArgumentException("SM2需要传入公钥进行加密"); + } + SM2 sm2 = SmUtil.sm2(null, publicKey); + return sm2.encryptHex(data, StandardCharsets.UTF_8, KeyType.PublicKey); + } + /** * sm2私钥解密 * @@ -195,6 +248,21 @@ public class EncryptUtils { return rsa.encryptBase64(data, StandardCharsets.UTF_8, KeyType.PublicKey); } + /** + * rsa公钥加密 + * + * @param data 待加密数据 + * @param publicKey 公钥 + * @return 加密后字符串, 采用Hex编码 + */ + public static String encryptByRsaHex(String data, String publicKey) { + if (StrUtil.isBlank(publicKey)) { + throw new IllegalArgumentException("RSA需要传入公钥进行加密"); + } + RSA rsa = SecureUtil.rsa(null, publicKey); + return rsa.encryptHex(data, StandardCharsets.UTF_8, KeyType.PublicKey); + } + /** * rsa私钥解密 *