DES加解密工具类

Des算法加解密.注意,这里的key使用的是固定值+ip地址的八个字节的字节数组.在生成数组的时候,byte值为-128-127,所以,如果是192什么的,会超出byte的大小,可以直接对数字进行强转为byte型,也可以转为16进制数,还可以直接用他的负值来替换.这里的数组{0,1,0,8,192,168,1,103}不管怎么转换,最后其实都是{0,1,0,8,-64,-88,1,103}

package com.freetek.cc.utils;

import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
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;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.net.InetAddress;
import java.security.SecureRandom;

/**
 * DES加密 解密算法
 * @author Young
 * @version 2017/5/5
 * @email yangxiaoping@freetek.cc
 */
public class DesUtil {
    private final static String DES = "DES";
    private final static String ENCODE = "utf-8";
    //默认key值
    private final static String defaultKey = "test1234";
    //运营系统提供的ticket
    private final static String sysTicket = "123d8ce011a5abf1edf62df84123c6b2";

    private final static String localTicket = "freetekcc";

    public static void main(String[] args) throws Exception {
        // 获取IP地址
        byte[] address = InetAddress.getLocalHost().getAddress();
        //直接数字转byte
        byte[] k = {(byte) 0,(byte) 1,(byte) 0,(byte) 8, (byte) 192,(byte) 168,(byte) 1,(byte) 103};
        //转换为16进制转byte
        byte[] k1 = {(byte)0x0,(byte)0x1,(byte)0,(byte)8, (byte) 0xC0,(byte)0xA8,(byte)1,(byte)0x67};
        //直接获取ip拼接
        byte[] pre_k = {0,1,0,8};
        byte[] key = addBytes(pre_k,address);

        /*String encrypt = encrypt(sysTicket, k);
        System.out.println("加密后的数据:" + encrypt);
        String decrypt = decrypt(encrypt, k);
        System.out.println("解密后的数据:" + decrypt);*/

        String encrypt2 = encryptIV(sysTicket, key);
        System.out.println("带IV值的加密后的数据:" + encrypt2);
        String decrypt2 = decryptIV(encrypt2, key);
        System.out.println("带IV值的解密后的数据:" + decrypt2);
    }

    /**
     * 带iv值的解密
     * 这里的iv值跟key值一样
     * @param decryptString
     * @param decryptKey
     * @return
     * @throws Exception
     */
    public static String decryptIV(String decryptString, byte[] decryptKey)
            throws Exception {
        IvParameterSpec iv = new IvParameterSpec(decryptKey);
        SecretKeySpec key = new SecretKeySpec(decryptKey, "DES");
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, key, iv);

        return new String(cipher.doFinal(Base64.decode(decryptString)));
    }

    /**
     * 带iv值的加密
     * 这里的iv值跟key值一样
     * @param encryptString
     * @param encryptKey
     * @return
     * @throws Exception
     */
    public static String encryptIV(String encryptString, byte[] encryptKey)
            throws Exception {
        IvParameterSpec iv = new IvParameterSpec(encryptKey);
        DESKeySpec dks = new DESKeySpec(encryptKey);
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey key = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key, iv);

        return Base64.encode(cipher.doFinal(encryptString.getBytes()));
    }

    /**
     * 使用 默认key 加密
     */
    public static String encrypt(String data) throws Exception {
        byte[] bt = encrypt(data.getBytes(ENCODE), defaultKey.getBytes(ENCODE));
        String strs = new BASE64Encoder().encode(bt);
        return strs;
    }

    /**
     * 使用 默认key 解密
     */
    public static String decrypt(String data) throws IOException, Exception {
        if (data == null)
            return null;
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] buf = decoder.decodeBuffer(data);
        byte[] bt = decrypt(buf, defaultKey.getBytes(ENCODE));
        return new String(bt, ENCODE);
    }

    /**
     * Description 根据键值进行加密
     *
     * @param data
     * @param key
     *            加密键byte数组
     * @return
     * @throws Exception
     */
    public static String encrypt(String data, String key) throws Exception {
        byte[] bt = encrypt(data.getBytes(ENCODE), key.getBytes(ENCODE));
        String strs = new BASE64Encoder().encode(bt);
        return strs;
    }

    /**
     * Description 根据键值进行解密
     *
     * @param data
     * @param key
     *            加密键byte数组
     * @return
     * @throws IOException
     * @throws Exception
     */
    public static String decrypt(String data, String key) throws IOException,
            Exception {
        if (data == null)
            return null;
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] buf = decoder.decodeBuffer(data);
        byte[] bt = decrypt(buf, key.getBytes(ENCODE));
        return new String(bt, ENCODE);
    }

    /**
     * Description 根据键值进行加密
     *
     * @param data
     * @param key
     *            加密键byte数组
     * @return
     * @throws Exception
     */
    public static String encrypt(String data, byte[] key) throws Exception {
        byte[] bt = encrypt(data.getBytes(ENCODE), key);
        String strs = new BASE64Encoder().encode(bt);
        return strs;
    }

    /**
     * Description 根据键值进行解密
     *
     * @param data
     * @param key
     *            加密键byte数组
     * @return
     * @throws IOException
     * @throws Exception
     */
    public static String decrypt(String data, byte[] key) throws IOException,
            Exception {
        if (data == null)
            return null;
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] buf = decoder.decodeBuffer(data);
        byte[] bt = decrypt(buf, key);
        return new String(bt, ENCODE);
    }

    /**
     * Description 根据键值进行加密
     *
     * @param data
     * @param key
     *            加密键byte数组
     * @return
     * @throws Exception
     */
    private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        // 生成一个可信任的随机数源
        SecureRandom sr = new SecureRandom();

        // 从原始密钥数据创建DESKeySpec对象
        DESKeySpec dks = new DESKeySpec(key);

        // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);

        // Cipher对象实际完成加密操作
        Cipher cipher = Cipher.getInstance(DES);

        // 用密钥初始化Cipher对象
        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);

        return cipher.doFinal(data);
    }

    /**
     * Description 根据键值进行解密
     *
     * @param data
     * @param key
     *            加密键byte数组
     * @return
     * @throws Exception
     */
    private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        // 生成一个可信任的随机数源
        SecureRandom sr = new SecureRandom();

        // 从原始密钥数据创建DESKeySpec对象
        DESKeySpec dks = new DESKeySpec(key);

        // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);

        // Cipher对象实际完成解密操作
        Cipher cipher = Cipher.getInstance(DES);

        // 用密钥初始化Cipher对象
        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);

        return cipher.doFinal(data);
    }

    /**
     *
     * @param data1
     * @param data2
     * @return data1 与 data2拼接的结果
     */
    public static byte[] addBytes(byte[] data1, byte[] data2) {
        byte[] data3 = new byte[data1.length + data2.length];
        System.arraycopy(data1, 0, data3, 0, data1.length);
        System.arraycopy(data2, 0, data3, data1.length, data2.length);
        return data3;

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值