zoukankan      html  css  js  c++  java
  • 加解密工具类

    import com.for.vsolution.hx.travel.common.constant.EncryptionTypeEnum;
    import com.for.vsolution.hx.travel.common.constant.MyRuntimeException;
    import com.for.vsolution.hx.travel.common.util.encrypt.Aes256EncryptUtils;
    import com.for.vsolution.hx.travel.common.util.encrypt.AesEncryptUtil;
    import com.for.vsolution.hx.travel.common.util.encrypt.MD5Util;
    import com.for.vsolution.hx.travel.common.util.encrypt.RsaUtil;
    import org.apache.commons.lang3.StringUtils;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;

    /**
    * @description: 加解密工具类
    * @date: 2019-08-26 14:17
    */

    @Component
    public class EncryptionUtil {


    @Value("${rsa.publicKey}")
    private String publicKey;
    @Value("${rsa.privateKey}")
    private String privateKey;

    /**
    * 加密
    * @param dataSource
    * @param encryptionType
    * @return
    */
    public String encryption(String dataSource, EncryptionTypeEnum encryptionType){
    if (StringUtils.isBlank(dataSource)){
    return null;
    }
    String encrypt=null;
    try {
    switch (encryptionType){
    case AES:
    encrypt = AesEncryptUtil.encrypt(dataSource);
    break;
    case MD5:
    encrypt = MD5Util.MD5(dataSource);
    break;
    case RSA:
    encrypt = RsaUtil.encryptByPubKey(dataSource,publicKey);
    break;
    case AES256:
    encrypt = Aes256EncryptUtils.encrypt(dataSource);
    break;
    default:
    break;
    }
    }catch (Exception e){
    throw new MyRuntimeException("加密异常");
    }
    return encrypt.trim();
    }

    public String decryption(String dataSource,EncryptionTypeEnum encryptionType){

    if (StringUtils.isBlank(dataSource)){
    return null;
    }
    String encrypt=null;
    try {
    switch (encryptionType){
    case AES:
    encrypt = AesEncryptUtil.desEncrypt(dataSource);
    break;
    case MD5:
    throw new MyRuntimeException("MD5无法解密");
    case RSA:
    encrypt = RsaUtil.decryptByPriKey(dataSource,privateKey);
    break;
    case AES256:
    encrypt = Aes256EncryptUtils.desEncrypt(dataSource);
    break;
    default:
    break;
    }
    }catch (Exception e){
    e.printStackTrace();
    return dataSource;
    }
    return encrypt.trim();
    }

    public static void main(String[] args) {

    EncryptionUtil encryptionUtil = new EncryptionUtil();
    String name = encryptionUtil.decryption("ppp02125166", EncryptionTypeEnum.AES256);
    System.out.println("+++++++++++++++++++++" + name);

    }

    }
  • 相关阅读:
    用电脑Python控制Arduino
    Arduino-LiquidCrystal_I2C 液晶库
    Arduino通讯串口
    nginx statistics in multi-workers
    Nginx RTMP 模块 nginx-rtmp-module 指令详解
    windows下搭建nginx-rtmp服务器
    超实用压力测试工具-ab工具
    (总结)Nginx配置文件nginx.conf中文详解
    NGINX conf 配置文件中的变量大全 可用变量列表及说明
    「自己开发直播」实现nginx-rtmp-module多频道输入输出与权限控制
  • 原文地址:https://www.cnblogs.com/pxzbky/p/12105818.html
Copyright © 2011-2022 走看看