update 优化 加密实现 使用 EncryptUtils 统一处理
This commit is contained in:
parent
4a00998f13
commit
86a8f5a700
@ -1,14 +1,9 @@
|
|||||||
package org.dromara.common.encrypt.core.encryptor;
|
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.core.EncryptContext;
|
||||||
import org.dromara.common.encrypt.enumd.AlgorithmType;
|
import org.dromara.common.encrypt.enumd.AlgorithmType;
|
||||||
import org.dromara.common.encrypt.enumd.EncodeType;
|
import org.dromara.common.encrypt.enumd.EncodeType;
|
||||||
|
import org.dromara.common.encrypt.utils.EncryptUtils;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* AES算法实现
|
* AES算法实现
|
||||||
@ -18,20 +13,11 @@ import java.nio.charset.StandardCharsets;
|
|||||||
*/
|
*/
|
||||||
public class AesEncryptor extends AbstractEncryptor {
|
public class AesEncryptor extends AbstractEncryptor {
|
||||||
|
|
||||||
private final AES aes;
|
private final EncryptContext context;
|
||||||
|
|
||||||
public AesEncryptor(EncryptContext context) {
|
public AesEncryptor(EncryptContext context) {
|
||||||
super(context);
|
super(context);
|
||||||
String password = context.getPassword();
|
this.context = context;
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -51,9 +37,9 @@ public class AesEncryptor extends AbstractEncryptor {
|
|||||||
@Override
|
@Override
|
||||||
public String encrypt(String value, EncodeType encodeType) {
|
public String encrypt(String value, EncodeType encodeType) {
|
||||||
if (encodeType == EncodeType.HEX) {
|
if (encodeType == EncodeType.HEX) {
|
||||||
return aes.encryptHex(value);
|
return EncryptUtils.encryptByAesHex(value, context.getPassword());
|
||||||
} else {
|
} else {
|
||||||
return aes.encryptBase64(value);
|
return EncryptUtils.encryptByAes(value, context.getPassword());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,6 +50,6 @@ public class AesEncryptor extends AbstractEncryptor {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String decrypt(String value) {
|
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;
|
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.core.EncryptContext;
|
||||||
import org.dromara.common.encrypt.enumd.AlgorithmType;
|
import org.dromara.common.encrypt.enumd.AlgorithmType;
|
||||||
import org.dromara.common.encrypt.enumd.EncodeType;
|
import org.dromara.common.encrypt.enumd.EncodeType;
|
||||||
|
import org.dromara.common.encrypt.utils.EncryptUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Base64算法实现
|
* Base64算法实现
|
||||||
@ -33,7 +33,7 @@ public class Base64Encryptor extends AbstractEncryptor {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String encrypt(String value, EncodeType encodeType) {
|
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
|
@Override
|
||||||
public String decrypt(String value) {
|
public String decrypt(String value) {
|
||||||
return Base64.decodeStr(value);
|
return EncryptUtils.decryptByBase64(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,10 @@
|
|||||||
package org.dromara.common.encrypt.core.encryptor;
|
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.core.utils.StringUtils;
|
||||||
import org.dromara.common.encrypt.core.EncryptContext;
|
import org.dromara.common.encrypt.core.EncryptContext;
|
||||||
import org.dromara.common.encrypt.enumd.AlgorithmType;
|
import org.dromara.common.encrypt.enumd.AlgorithmType;
|
||||||
import org.dromara.common.encrypt.enumd.EncodeType;
|
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 {
|
public class RsaEncryptor extends AbstractEncryptor {
|
||||||
|
|
||||||
private final RSA rsa;
|
private final EncryptContext context;
|
||||||
|
|
||||||
public RsaEncryptor(EncryptContext context) {
|
public RsaEncryptor(EncryptContext context) {
|
||||||
super(context);
|
super(context);
|
||||||
@ -27,7 +24,7 @@ public class RsaEncryptor extends AbstractEncryptor {
|
|||||||
if (StringUtils.isAnyEmpty(privateKey, publicKey)) {
|
if (StringUtils.isAnyEmpty(privateKey, publicKey)) {
|
||||||
throw new IllegalArgumentException("RSA公私钥均需要提供,公钥加密,私钥解密。");
|
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
|
@Override
|
||||||
public String encrypt(String value, EncodeType encodeType) {
|
public String encrypt(String value, EncodeType encodeType) {
|
||||||
if (encodeType == EncodeType.HEX) {
|
if (encodeType == EncodeType.HEX) {
|
||||||
return rsa.encryptHex(value, KeyType.PublicKey);
|
return EncryptUtils.encryptByRsaHex(value, context.getPublicKey());
|
||||||
} else {
|
} else {
|
||||||
return rsa.encryptBase64(value, KeyType.PublicKey);
|
return EncryptUtils.encryptByRsa(value, context.getPublicKey());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,6 +57,6 @@ public class RsaEncryptor extends AbstractEncryptor {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String decrypt(String value) {
|
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;
|
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.core.utils.StringUtils;
|
||||||
import org.dromara.common.encrypt.core.EncryptContext;
|
import org.dromara.common.encrypt.core.EncryptContext;
|
||||||
import org.dromara.common.encrypt.enumd.AlgorithmType;
|
import org.dromara.common.encrypt.enumd.AlgorithmType;
|
||||||
import org.dromara.common.encrypt.enumd.EncodeType;
|
import org.dromara.common.encrypt.enumd.EncodeType;
|
||||||
|
import org.dromara.common.encrypt.utils.EncryptUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sm2算法实现
|
* sm2算法实现
|
||||||
@ -17,7 +14,7 @@ import org.dromara.common.encrypt.enumd.EncodeType;
|
|||||||
*/
|
*/
|
||||||
public class Sm2Encryptor extends AbstractEncryptor {
|
public class Sm2Encryptor extends AbstractEncryptor {
|
||||||
|
|
||||||
private final SM2 sm2;
|
private final EncryptContext context;
|
||||||
|
|
||||||
public Sm2Encryptor(EncryptContext context) {
|
public Sm2Encryptor(EncryptContext context) {
|
||||||
super(context);
|
super(context);
|
||||||
@ -26,7 +23,7 @@ public class Sm2Encryptor extends AbstractEncryptor {
|
|||||||
if (StringUtils.isAnyEmpty(privateKey, publicKey)) {
|
if (StringUtils.isAnyEmpty(privateKey, publicKey)) {
|
||||||
throw new IllegalArgumentException("SM2公私钥均需要提供,公钥加密,私钥解密。");
|
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
|
@Override
|
||||||
public String encrypt(String value, EncodeType encodeType) {
|
public String encrypt(String value, EncodeType encodeType) {
|
||||||
if (encodeType == EncodeType.HEX) {
|
if (encodeType == EncodeType.HEX) {
|
||||||
return sm2.encryptHex(value, KeyType.PublicKey);
|
return EncryptUtils.encryptBySm2Hex(value, context.getPublicKey());
|
||||||
} else {
|
} else {
|
||||||
return sm2.encryptBase64(value, KeyType.PublicKey);
|
return EncryptUtils.encryptBySm2(value, context.getPublicKey());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -59,6 +56,6 @@ public class Sm2Encryptor extends AbstractEncryptor {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String decrypt(String value) {
|
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;
|
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.core.EncryptContext;
|
||||||
import org.dromara.common.encrypt.enumd.AlgorithmType;
|
import org.dromara.common.encrypt.enumd.AlgorithmType;
|
||||||
import org.dromara.common.encrypt.enumd.EncodeType;
|
import org.dromara.common.encrypt.enumd.EncodeType;
|
||||||
|
import org.dromara.common.encrypt.utils.EncryptUtils;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* sm4算法实现
|
* sm4算法实现
|
||||||
@ -17,19 +13,11 @@ import java.nio.charset.StandardCharsets;
|
|||||||
*/
|
*/
|
||||||
public class Sm4Encryptor extends AbstractEncryptor {
|
public class Sm4Encryptor extends AbstractEncryptor {
|
||||||
|
|
||||||
private final SM4 sm4;
|
private final EncryptContext context;
|
||||||
|
|
||||||
public Sm4Encryptor(EncryptContext context) {
|
public Sm4Encryptor(EncryptContext context) {
|
||||||
super(context);
|
super(context);
|
||||||
String password = context.getPassword();
|
this.context = context;
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -49,9 +37,9 @@ public class Sm4Encryptor extends AbstractEncryptor {
|
|||||||
@Override
|
@Override
|
||||||
public String encrypt(String value, EncodeType encodeType) {
|
public String encrypt(String value, EncodeType encodeType) {
|
||||||
if (encodeType == EncodeType.HEX) {
|
if (encodeType == EncodeType.HEX) {
|
||||||
return sm4.encryptHex(value);
|
return EncryptUtils.encryptBySm4Hex(value, context.getPassword());
|
||||||
} else {
|
} else {
|
||||||
return sm4.encryptBase64(value);
|
return EncryptUtils.encryptBySm4(value, context.getPassword());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,6 +50,6 @@ public class Sm4Encryptor extends AbstractEncryptor {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public String decrypt(String value) {
|
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);
|
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解密
|
* AES解密
|
||||||
*
|
*
|
||||||
@ -105,6 +124,25 @@ public class EncryptUtils {
|
|||||||
return SmUtil.sm4(password.getBytes(StandardCharsets.UTF_8)).encryptBase64(data, StandardCharsets.UTF_8);
|
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解密
|
* sm4解密
|
||||||
*
|
*
|
||||||
@ -152,6 +190,21 @@ public class EncryptUtils {
|
|||||||
return sm2.encryptBase64(data, StandardCharsets.UTF_8, KeyType.PublicKey);
|
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私钥解密
|
* sm2私钥解密
|
||||||
*
|
*
|
||||||
@ -195,6 +248,21 @@ public class EncryptUtils {
|
|||||||
return rsa.encryptBase64(data, StandardCharsets.UTF_8, KeyType.PublicKey);
|
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私钥解密
|
* rsa私钥解密
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user