update 优化 加密实现 使用 EncryptUtils 统一处理

feature/model
疯狂的狮子Li 1 year ago
parent 4a00998f13
commit 86a8f5a700

@ -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());
}
}

@ -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);
}
}

@ -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());
}
}

@ -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());
}
}

@ -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());
}
}

@ -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
*

Loading…
Cancel
Save