DES加密解密

package com.todaytech.gfmis.task.common;

import com.todaytech.framework.GenericBO;
import com.todaytech.framework.exception.FatalBizException;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

/**
 * DES加密解密工具
 * User: faker add
 * Date: 2017-7-11
 * Time: 14:17:15
 */
public class DESCoderCourt extends GenericBO {
    private static String privateKey = "VIPTODAY";//密钥
    private static final DESCoderCourt ourInstance = new DESCoderCourt();
    private static final String METHOD = "DES";//加密方式
    private static final String STR_ENCODING = "UTF-8";//编码方式

    private Cipher myCipher;
    SecretKey secretKey;
    private BASE64Encoder baseEncoder;
    private BASE64Decoder baseDecoder;

    private DESCoderCourt(){
        try {
            baseEncoder = new BASE64Encoder();
            baseDecoder = new BASE64Decoder();
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(METHOD);
            DESKeySpec dKey = new DESKeySpec(privateKey.getBytes(STR_ENCODING));
            secretKey = keyFactory.generateSecret(dKey);
            myCipher = Cipher.getInstance(METHOD);
        } catch (Exception e) {
            logger.error(e);
        }
    }

    public static DESCoderCourt getInstance() {
        return ourInstance;
    }

    /**
     * 编码加密
     * @param str
     * @return
     * @throws com.todaytech.framework.exception.FatalBizException
     */
    public static synchronized String encode(String str) throws FatalBizException{
        return getInstance().encodeImp(str);
    }

    private String encodeImp(String str) throws FatalBizException {
        try {
            return baseEncoder.encode(str.getBytes(STR_ENCODING));
        } catch (Exception e) {
            logger.error(e);
            throw new FatalBizException(e.getMessage());
        }
    }

    /**
     * 解密解码
     * @param str
     * @return
     * @throws com.todaytech.framework.exception.FatalBizException
     */
    public static synchronized String decode(String str) throws FatalBizException{
        return getInstance().decodeImp(str);
    }

    private String decodeImp(String str) throws FatalBizException {
        try {
            return new String(baseDecoder.decodeBuffer(str));
        } catch (Exception e) {
            logger.error(e);
            throw new FatalBizException(e.getMessage());
        }
    }

    public static void main(String[] args) throws FatalBizException {
        String source = "085001";
        String mi = DESCoderCourt.encode(source);
        System.out.println("密钥:" + privateKey);
        System.out.println("明文:" + DESCoderCourt.decode(mi));
        System.out.println("密文:" + mi);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值