java加密解密接口

import org.apache.commons.lang3.StringUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.Key;
import java.security.spec.AlgorithmParameterSpec;

/**
 * 加密解密接口
 */
public class EncryptUtil {

    /** 向量 */
    private final byte[] DESIV = new byte[]{0x12, 0x34, 0x56, 120, (byte) 0x90, (byte) 0xab, (byte) 0xcd,
            (byte) 0xef};
    /**加密算法的参数接口*/
    private AlgorithmParameterSpec iv = null;
    private Key key = null;

    private String charset = "UTF-8";
    private String encryptKey = "http://www.shutdown.cn";

    /**
     * 初始化
     *
     * @param defineKey 密钥
     * @param defineCharset 字符集
     * @throws Exception
     */
    public EncryptUtil(String defineKey, String defineCharset) throws Exception {
        if (StringUtils.isNotBlank(defineCharset)) {
            this.charset = defineCharset;
        }
        if (StringUtils.isNotBlank(defineKey)) {
            this.encryptKey = defineKey;
        }// 设置密钥参数
        DESKeySpec keySpec = new DESKeySpec(encryptKey.getBytes(charset));
        // 设置向量
        iv = new IvParameterSpec(DESIV);
        // 获得密钥工厂
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        // 得到密钥对象
        key = keyFactory.generateSecret(keySpec);
    }

    /**
     * 初始化
     * @throws Exception
     */
    public EncryptUtil() throws Exception {
        // 设置密钥参数
        DESKeySpec keySpec = new DESKeySpec(encryptKey.getBytes(charset));
        // 设置向量
        iv = new IvParameterSpec(DESIV);
        // 获得密钥工厂
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        // 得到密钥对象
        key = keyFactory.generateSecret(keySpec);
    }

    /**
     * 加密
     * @param data
     * @return
     * @throws Exception
     */
    public String encode(String data) {
        try {
            // 得到加密对象Cipher
            Cipher enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            // 设置工作模式为加密模式,给出密钥和向量
            enCipher.init(Cipher.ENCRYPT_MODE, key, iv);
            byte[] pasByte = enCipher.doFinal(data.getBytes(charset));
            BASE64Encoder base64Encoder = new BASE64Encoder();
            return base64Encoder.encode(pasByte);
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 解密
     * @param data
     * @return
     * @throws Exception
     */
    public String decode(String data) {
        try {
            Cipher deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            deCipher.init(Cipher.DECRYPT_MODE, key, iv);
            BASE64Decoder base64Decoder = new BASE64Decoder();
            byte[] pasByte = deCipher.doFinal(base64Decoder.decodeBuffer(data));
            return new String(pasByte, charset);
        } catch (Exception e) {
            return "";
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值