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); } }
DES加密解密
最新推荐文章于 2019-07-10 09:41:23 发布