1、新建EncryptEnums枚举,用于定义组件提供的加解密种类
package com.hua.common.tools.encrypt;
import lombok.Getter;
/**
* @Deacription TODO
* @Author huazi
* @Date 2020/4/4 13:03
**/
@Getter
public enum EncryptEnums {
AES("AES"),
MD5("MD5"),
;
private String type;
EncryptEnums(String type) {
this.type = type;
}
}
2、新建EncryptException类,用于自定义异常类
package com.hua.common.tools.encrypt;
/**
* @Deacription TODO
* @Author huazi
* @Date 2020/4/4 13:05
**/
public class EncryptException extends RuntimeException {
private String code;
private String message;
public EncryptException(String message) {
super(message);
}
public EncryptException(String code, String message) {
super(message);
this.code = code;
this.message = message;
}
public EncryptException(String message, Throwable cause) {
super(message, cause);
}
}
3、新建EncryptFactory ,用于初始化具体加解密组件
package com.hua.common.tools.encrypt;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* @Deacription TODO
* @Author huazi
* @Date 2020/4/4 13:40
**/
public final class EncryptFactory {
protected static Map<String, EncryptService> ENCRYPT_SERVICE = new ConcurrentHashMap<>();
static {
ENCRYPT_SERVICE.put(EncryptEnums.AES.getType(), new AesEncryptService());
}
}
4、新建EncryptService,用于定义加解密接口
package com.hua.common.tools.encrypt;
/**
* @Deacription TODO
* @Author huazi
* @Date 2020/4/4 12:53
**/
public interface EncryptService {
/**
* @Description:
* @Param: [planText 明文, secretKey 秘钥]
* @Author: hongjianhua
* @Date: 2020/4/4 12:57
*/
String encode(String planText, String secretKey);
/**
* @Description: 解密
* @Param: [secretText 密文,secretKey 秘钥]
* @Author: hongjianhua
* @Date: 2020/4/4 12:55
*/
String decode(String secretText, String secretKey);
}
5、新建AbstractEncrypt,用于提供加解密公共方法
package com.hua.common.tools.encrypt;
import org.apache.commons.lang3.StringUtils;
/**
* @Deacription TODO
* @Author huazi
* @Date 2020/4/4 12:58
**/
public abstract class AbstractEncrypt implements EncryptService {
protected final void encodeValid(String planText, String secretKey) {
if (StringUtils.isBlank(planText)) {
throw new EncryptException("encode planText is null");
}
if (StringUtils.isBlank(secretKey)) {
throw new EncryptException("encode secretKey is null");
}
}
}
6、新建AesEncryptService,用于AES加解密
package com.hua.common.tools.encrypt;
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
/**
* @Deacription TODO
* @Author huazi
* @Date 2020/4/4 12:52
**/
public class AesEncryptService extends AbstractEncrypt {
private static final String ALGORITHMSTR = "AES";
private static final String DEFAULT_CHARSET = "utf-8";
private static final int SIZE = 128;
private static final int KEY_LENGTH = 16;
@Override
public String encode(String planText, String secretKey) {
super.encodeValid(planText, secretKey);
try {
Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(secretKey.getBytes(), ALGORITHMSTR));
byte[] b = cipher.doFinal(planText.getBytes(DEFAULT_CHARSET));
/*采用base64算法进行转码,避免出现中文乱码*/
return Base64.encodeBase64String(b);
} catch (Exception e) {
throw new EncryptException("AES encode exception", e);
}
}
@Override
public String decode(String secretText, String secretKey) {
try {
Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(secretKey.getBytes(), ALGORITHMSTR));
/*采用base64算法进行转码,避免出现中文乱码*/
byte[] encryptBytes = Base64.decodeBase64(secretText);
byte[] decryptBytes = cipher.doFinal(encryptBytes);
return new String(decryptBytes);
} catch (Exception e) {
throw new EncryptException("AES decode exception", e);
}
}
}
7、新建EncryptComponent类,用于提供统一对外加解密功能
package com.hua.common.tools.encrypt;
/**
* @Deacription TODO
* @Author huazi
* @Date 2020/4/4 13:43
**/
public final class EncryptComponent {
public static String encode(final String planText, final String secretKey, EncryptEnums encryptEnums) {
EncryptService encryptService = EncryptFactory.ENCRYPT_SERVICE.get(encryptEnums.getType());
String secretText = encryptService.encode(planText, secretKey);
return secretText;
}
public static String decode(final String secretText, final String secretKey, EncryptEnums encryptEnums) {
EncryptService encryptService = EncryptFactory.ENCRYPT_SERVICE.get(encryptEnums.getType());
String planText = encryptService.decode(secretText, secretKey);
return planText;
}
}