java字符串的3DES+base64加密

3DES加密工具类

import org.apache.commons.codec.binary.Base64;
import org.apache.log4j.Logger;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;

/**
 * 3DES加密解密和BASE64编码解码混合使用
 */
public class ThreeDESUtil {

    private static Logger logger = Logger
            .getLogger(ThreeDESUtil.class);
    private static ThreeDESUtil threeDESUtil = null;

    private ThreeDESUtil() {
        init();
    }

    public synchronized static ThreeDESUtil getInstance() {
        if (threeDESUtil == null) threeDESUtil = new ThreeDESUtil();
        return threeDESUtil;
    }

    //key必须在16位/24位/32位,下面使用的是24位,不足24位则后面余数补0填充满24位
    private static final String key="f4605a21fc8e4077a21397fd";
    //定义加密算法,有DES、DESede(即3DES)、Blowfish
    private static final String Algorithm = "DESede/ECB/PKCS5Padding";
    //如果使用DESede,默认则使用"DESede/ECB/PKCS5Padding"  ECB:电子密码本形式加密,
    // BCB:密码块链接加密
    //    private static final String DES = "DESede/ECB/PKCS5Padding";
    //    private static final String DES = "DESede/CBC/PKCS5Padding";
    //    private static final String DES = "DESede/ECB/NoPadding";

    private static Cipher decryptCipher;
    private static Cipher encryptCipher;

    private void init(){
        try{
            SecretKey deskey = new SecretKeySpec(build3DesKey(key), "DESede");    //生成密钥
            encryptCipher = Cipher.getInstance(Algorithm);    //实例化负责加密/解密的Cipher工具类
            encryptCipher.init(Cipher.ENCRYPT_MODE, deskey);

            decryptCipher = Cipher.getInstance(Algorithm);    //实例化负责加密/解密的Cipher工具类
            decryptCipher.init(Cipher.DECRYPT_MODE,deskey);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 3DES加密方法
     * @param src 源数据的字节数组
     * @return
     */
    public byte[] encryptMode(byte[] src,String key) {
        try {
            return encryptCipher.doFinal(src);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    //3DES解密
    public byte[] decrypt(byte[] data,String key){
        try {
            return decryptCipher.doFinal(data);
        } catch (Exception ex) {
            //解密失败,打日志
            ex.printStackTrace();
        }
        return null;
    }

    /**
     *  3DES加密Base64编码处理方法
     * @param bytes 字符串转的数组
     * @return  经过3DES+Base64加密后的字符串
     */
    public String encode3DesBase64(byte[] bytes){
        byte [] base = encryptMode(bytes,key);
        return Base64.encodeBase64String(base);
    }

    public String encode3DesBase64(String src){
        try{
            return  encode3DesBase64(src.getBytes("utf-8"));
        }catch (Exception e){
           logger.error(e);
        }
        return null;
    }

    /**
     * 将3DES+Base64加密后的byte数组进行解密
     * @param bytes 先3DES+Base64加密后的 byte数组
     * @return 未加密前的字符串
     */
    public String decode3DesBase64(byte[] bytes){
        byte[] b = null;
        String result = null;
        try {
            b = decrypt(Base64.decodeBase64(bytes),key);
            result = new String(b, "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public String decode3DesBase64(String src){
        try {
            return decode3DesBase64(src.getBytes("utf-8"));
        }catch (Exception e){
            logger.error(e);
        }
        return null;
    }

    //构建3DES密钥
    public byte[] build3DesKey(String keyStr) throws UnsupportedEncodingException {
        byte[] key = new byte[24];    //声明一个24位的字节数组,默认里面都是0
        byte[] temp = keyStr.getBytes("UTF-8");    //将字符串转成字节数组
         /*
          * 执行数组拷贝
          * System.arraycopy(源数组,从源数组哪里开始拷贝,目标数组,拷贝多少位)
          */
        if(key.length > temp.length){
            //如果temp不够24位,则拷贝temp数组整个长度的内容到key数组中
            System.arraycopy(temp, 0, key, 0, temp.length);
        }else{
            //如果temp大于24位,则拷贝temp数组24个长度的内容到key数组中
            System.arraycopy(temp, 0, key, 0, key.length);
        }
        return key;
    }
}

注意

SecretKey deskey = new SecretKeySpec(build3DesKey(key), "DESede");
生成密钥的代码,第二个参数为DESede即为3DES加密

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值