疯狂的狮子Li
2023-08-07 86a8f5a700a19d8e55da5f5f0d5edd6b1284934f
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 @@
 */
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 @@
    @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 @@
     */
    @Override
    public String decrypt(String value) {
        return this.aes.decryptStr(value);
        return EncryptUtils.decryptByAes(value, context.getPassword());
    }
}