java:AES、MD5等java加解密

转载自:https://blog.csdn.net/hong_myth/article/details/105309623
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;
    }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值