Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>cn.licoy</groupId>
<artifactId>encrypt-body-spring-boot-starter</artifactId>
<version>1.2.3</version>
<version>1.2.4-SNAPSHOT</version>

<name>encrypt-body-spring-boot-starter</name>
<description>encrypt-body-spring-boot-starter是SpringBoot控制器统一的响应体加密与请求体解密的注解处理方式,支持MD5/SHA/AES/DES/RSA</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,18 @@ private String switchEncrypt(String formatStringBody, EncryptAnnotationInfoBean
String key = infoBean.getKey();
if (method == EncryptBodyMethod.DES) {
key = CommonUtils.checkAndGetKey(config.getDesKey(), key, "DES-KEY");
return SecureUtil.des(key.getBytes()).encryptHex(formatStringBody);
return config.getEncryptEncodeType() == EncryptBodyConfig.EncryptEncodeType.BASE64 ?
SecureUtil.des(key.getBytes()).encryptBase64(formatStringBody) : SecureUtil.des(key.getBytes()).encryptHex(formatStringBody);
}
if (method == EncryptBodyMethod.AES) {
key = CommonUtils.checkAndGetKey(config.getAesKey(), key, "AES-KEY");
return SecureUtil.aes(key.getBytes()).encryptHex(formatStringBody);
return config.getEncryptEncodeType() == EncryptBodyConfig.EncryptEncodeType.BASE64 ?
SecureUtil.aes(key.getBytes()).encryptBase64(formatStringBody) : SecureUtil.aes(key.getBytes()).encryptHex(formatStringBody);
}
if (method == EncryptBodyMethod.RSA) {
RSA rsa = CommonUtils.infoBeanToRsaInstance(infoBean);
return rsa.encryptHex(formatStringBody, infoBean.getRsaKeyType().toolType);
return config.getEncryptEncodeType() == EncryptBodyConfig.EncryptEncodeType.BASE64 ?
rsa.encryptBase64(formatStringBody, infoBean.getRsaKeyType().toolType) : rsa.encryptHex(formatStringBody, infoBean.getRsaKeyType().toolType);
}
throw new EncryptBodyFailException();
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/cn/licoy/encryptbody/config/EncryptBodyConfig.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cn.licoy.encryptbody.config;

import cn.licoy.encryptbody.advice.EncryptResponseBodyAdvice;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import org.springframework.context.annotation.Configuration;

import java.nio.charset.Charset;
Expand Down Expand Up @@ -31,5 +33,17 @@ public class EncryptBodyConfig {

private Charset encoding = StandardCharsets.UTF_8;

/**
* aes 或者 des 加密后使用编码处理密文(base64 或 hex, 防止特殊字符转码)
*
* @see EncryptResponseBodyAdvice#switchEncrypt(java.lang.String, cn.licoy.encryptbody.bean.EncryptAnnotationInfoBean)
*/
@NestedConfigurationProperty
private EncryptEncodeType encryptEncodeType = EncryptEncodeType.HEX;

public enum EncryptEncodeType {
HEX,
BASE64
}

}