update 优化 加密实现 使用 EncryptUtils 统一处理
| | |
| | | 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()); |
| | | } |
| | | } |
| | |
| | | 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算法实现 |
| | |
| | | */ |
| | | @Override |
| | | public String encrypt(String value, EncodeType encodeType) { |
| | | return Base64.encode(value); |
| | | return EncryptUtils.encryptByBase64(value); |
| | | } |
| | | |
| | | /** |
| | |
| | | */ |
| | | @Override |
| | | public String decrypt(String value) { |
| | | return Base64.decodeStr(value); |
| | | return EncryptUtils.decryptByBase64(value); |
| | | } |
| | | } |
| | |
| | | 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; |
| | | |
| | | |
| | | /** |
| | |
| | | */ |
| | | public class RsaEncryptor extends AbstractEncryptor { |
| | | |
| | | private final RSA rsa; |
| | | private final EncryptContext context; |
| | | |
| | | public RsaEncryptor(EncryptContext context) { |
| | | super(context); |
| | |
| | | if (StringUtils.isAnyEmpty(privateKey, publicKey)) { |
| | | throw new IllegalArgumentException("RSA公私钥均需要提供,公钥加密,私钥解密。"); |
| | | } |
| | | this.rsa = SecureUtil.rsa(Base64.decode(privateKey), Base64.decode(publicKey)); |
| | | this.context = context; |
| | | } |
| | | |
| | | /** |
| | |
| | | @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()); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | @Override |
| | | public String decrypt(String value) { |
| | | return this.rsa.decryptStr(value, KeyType.PrivateKey); |
| | | return EncryptUtils.decryptByRsa(value, context.getPrivateKey()); |
| | | } |
| | | } |
| | |
| | | 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算法实现 |
| | |
| | | */ |
| | | public class Sm2Encryptor extends AbstractEncryptor { |
| | | |
| | | private final SM2 sm2; |
| | | private final EncryptContext context; |
| | | |
| | | public Sm2Encryptor(EncryptContext context) { |
| | | super(context); |
| | |
| | | if (StringUtils.isAnyEmpty(privateKey, publicKey)) { |
| | | throw new IllegalArgumentException("SM2公私钥均需要提供,公钥加密,私钥解密。"); |
| | | } |
| | | this.sm2 = SmUtil.sm2(Base64.decode(privateKey), Base64.decode(publicKey)); |
| | | this.context = context; |
| | | } |
| | | |
| | | /** |
| | |
| | | @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()); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | @Override |
| | | public String decrypt(String value) { |
| | | return this.sm2.decryptStr(value, KeyType.PrivateKey); |
| | | return EncryptUtils.decryptBySm2(value, context.getPrivateKey()); |
| | | } |
| | | } |
| | |
| | | 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算法实现 |
| | |
| | | */ |
| | | 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; |
| | | } |
| | | |
| | | /** |
| | |
| | | @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()); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | @Override |
| | | public String decrypt(String value) { |
| | | return this.sm4.decryptStr(value); |
| | | return EncryptUtils.decryptBySm4(value, context.getPassword()); |
| | | } |
| | | } |
| | |
| | | } |
| | | |
| | | /** |
| | | * 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解密 |
| | | * |
| | | * @param data 待解密数据 |
| | |
| | | throw new IllegalArgumentException("SM4秘钥长度要求为16位"); |
| | | } |
| | | 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); |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | /** |
| | | * 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私钥解密 |
| | | * |
| | | * @param data 待加密数据 |
| | |
| | | } |
| | | |
| | | /** |
| | | * 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私钥解密 |
| | | * |
| | | * @param data 待加密数据 |