对数据进行DNS加密

DNSCrypt是一个确保客户与DNS服务器之间传输安全的工具,基于DNSCurve修改而来。


import javax.crypto.Cipher;

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

/**
 * DES对称加解密工具
 */
public final class DESUtil {

    //默认密钥
    private static final String DEFAULT_KEY = "default8";
    //字符编码
    private static final String CHAESET = "UTF-8";
    //加密算法
    private static final String ALGORITHM = "DES/CBC/PKCS5Padding";
    
    private DESUtil(){
        //禁止外部构造
    }

    /**
     * 对数据进行DES加密.
     * @param data 待进行DES加密的数据
     * @param secretKey 密钥
     * @return 返回经过DES加密后的数据
     * @throws Exception
     */
    public static String decrypt(String data,String secretKey) throws Exception {
        String key = secretKey;
        if(key == null || key == "")
            key = DEFAULT_KEY;
        return new String(cryptHandle(false,hex2byte(data.getBytes(CHAESET)),key.getBytes(CHAESET)),CHAESET);
    }

    /**
     * 对用DES加密过的数据进行解密.
     * @param data DES加密数据
     * @param secretKey 密钥
     * @return 返回解密后的数据
     * @throws Exception
     */
    public static String encrypt(String data,String secretKey) throws Exception {
        String key = secretKey;
        if(key == null || key == "")
            key = DEFAULT_KEY;
        return byte2hex(cryptHandle(true,data.getBytes(CHAESET), key.getBytes(CHAESET)));
    }

    /**
     * 用指定的key对数据进行DES加密或解密.
     * @param isEncrypt 是否加密操作,true表示加密,false表示解密
     * @param data 待处理的数据
     * @param key 密钥
     * @return 返回处理后的数据
     * @throws Exception
     */
    private static byte[] cryptHandle(boolean isEncrypt,byte[] data, byte[] key) throws Exception {
        DESKeySpec dks = new DESKeySpec(key);
        IvParameterSpec iv = new IvParameterSpec(key);
        SecretKey securekey = SecretKeyFactory.getInstance("DES").generateSecret(dks);
        
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        if(isEncrypt)
            cipher.init(Cipher.ENCRYPT_MODE, securekey, iv);
        else
            cipher.init(Cipher.DECRYPT_MODE, securekey, iv);
        return cipher.doFinal(data);
    }

    //16进制转换为2进制
    private static byte[] hex2byte(byte[] b) {
        if ((b.length % 2) != 0)
            throw new IllegalArgumentException("长度不是偶数");
        byte[] b2 = new byte[b.length / 2];
        for (int n = 0; n < b.length; n += 2) {
            String item = new String(b, n, 2);
            b2[n / 2] = (byte) Integer.parseInt(item, 16);
        }
        return b2;
    }

    //2进制转换为16进制
    private static String byte2hex(byte[] b) {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1)
                hs = hs + "0" + stmp;
            else
                hs = hs + stmp;
        }
        return hs.toUpperCase();
    }
    
//    public static void main(String args[])throws Exception{
//        String str = "测试数据";
//        String 密文 = DESUtil.encrypt(str,null);
//        System.out.println(密文);
//        String 明文 = DESUtil.decrypt(密文,null);
//        System.out.println(明文);
//        
//    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值