| | |
| | | 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算法实现 |
| | |
| | | */ |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | |
| | | @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()); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | @Override |
| | | public String decrypt(String value) { |
| | | return this.aes.decryptStr(value); |
| | | return EncryptUtils.decryptByAes(value, context.getPassword()); |
| | | } |
| | | } |