mirror of
				https://github.com/dromara/RuoYi-Vue-Plus.git
				synced 2025-11-04 08:13:44 +08:00 
			
		
		
		
	update 优化 加密实现 使用 EncryptUtils 统一处理
This commit is contained in:
		@@ -1,14 +1,9 @@
 | 
			
		||||
package com.ruoyi.common.encrypt.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 com.ruoyi.common.encrypt.EncryptContext;
 | 
			
		||||
import com.ruoyi.common.enums.AlgorithmType;
 | 
			
		||||
import com.ruoyi.common.enums.EncodeType;
 | 
			
		||||
 | 
			
		||||
import java.nio.charset.StandardCharsets;
 | 
			
		||||
import com.ruoyi.common.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());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,9 +1,9 @@
 | 
			
		||||
package com.ruoyi.common.encrypt.encryptor;
 | 
			
		||||
 | 
			
		||||
import cn.hutool.core.codec.Base64;
 | 
			
		||||
import com.ruoyi.common.encrypt.EncryptContext;
 | 
			
		||||
import com.ruoyi.common.enums.AlgorithmType;
 | 
			
		||||
import com.ruoyi.common.enums.EncodeType;
 | 
			
		||||
import com.ruoyi.common.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);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,12 +1,9 @@
 | 
			
		||||
package com.ruoyi.common.encrypt.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 com.ruoyi.common.encrypt.EncryptContext;
 | 
			
		||||
import com.ruoyi.common.enums.AlgorithmType;
 | 
			
		||||
import com.ruoyi.common.enums.EncodeType;
 | 
			
		||||
import com.ruoyi.common.utils.EncryptUtils;
 | 
			
		||||
import com.ruoyi.common.utils.StringUtils;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@@ -18,7 +15,7 @@ import com.ruoyi.common.utils.StringUtils;
 | 
			
		||||
 */
 | 
			
		||||
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());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,13 +1,10 @@
 | 
			
		||||
package com.ruoyi.common.encrypt.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 com.ruoyi.common.encrypt.EncryptContext;
 | 
			
		||||
import com.ruoyi.common.enums.AlgorithmType;
 | 
			
		||||
import com.ruoyi.common.enums.EncodeType;
 | 
			
		||||
import com.ruoyi.common.utils.EncryptUtils;
 | 
			
		||||
import com.ruoyi.common.utils.StringUtils;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
@@ -18,7 +15,7 @@ import com.ruoyi.common.utils.StringUtils;
 | 
			
		||||
 */
 | 
			
		||||
public class Sm2Encryptor extends AbstractEncryptor {
 | 
			
		||||
 | 
			
		||||
    private final SM2 sm2;
 | 
			
		||||
    private final EncryptContext context;
 | 
			
		||||
 | 
			
		||||
    public Sm2Encryptor(EncryptContext context) {
 | 
			
		||||
        super(context);
 | 
			
		||||
@@ -27,7 +24,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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
@@ -47,9 +44,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());
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -60,6 +57,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());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,13 +1,9 @@
 | 
			
		||||
package com.ruoyi.common.encrypt.encryptor;
 | 
			
		||||
 | 
			
		||||
import cn.hutool.core.util.StrUtil;
 | 
			
		||||
import cn.hutool.crypto.SmUtil;
 | 
			
		||||
import cn.hutool.crypto.symmetric.SM4;
 | 
			
		||||
import com.ruoyi.common.encrypt.EncryptContext;
 | 
			
		||||
import com.ruoyi.common.enums.AlgorithmType;
 | 
			
		||||
import com.ruoyi.common.enums.EncodeType;
 | 
			
		||||
 | 
			
		||||
import java.nio.charset.StandardCharsets;
 | 
			
		||||
import com.ruoyi.common.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());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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私钥解密
 | 
			
		||||
     *
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user