博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DesUtils工具类
阅读量:7096 次
发布时间:2019-06-28

本文共 3000 字,大约阅读时间需要 10 分钟。

public final class DesUtils {    private static final String DES = "DES";    private static final String KEY = "4YztMHI7PsT4rLZN";    private DesUtils() {}    private static byte[] encrypt(byte[] src, byte[] key) throws Exception {        SecureRandom sr = new SecureRandom();        DESKeySpec dks = new DESKeySpec(key);        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);        SecretKey secretKey = keyFactory.generateSecret(dks);        Cipher cipher = Cipher.getInstance(DES);        cipher.init(Cipher.ENCRYPT_MODE, secretKey, sr);        return cipher.doFinal(src);    }    private static byte[] decrypt(byte[] src, byte[] key) throws Exception {        SecureRandom sr = new SecureRandom();        DESKeySpec dks = new DESKeySpec(key);        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);        SecretKey secretKey = keyFactory.generateSecret(dks);        Cipher cipher = Cipher.getInstance(DES);        cipher.init(Cipher.DECRYPT_MODE, secretKey, sr);        return cipher.doFinal(src);    }    private static String byte2hex(byte[] b) {        String hs = "";        String temp = "";        for (int n = 0; n < b.length; n++) {            temp = (java.lang.Integer.toHexString(b[n] & 0XFF));            if (temp.length() == 1)                hs = hs + "0" + temp;            else                hs = hs + temp;        }        return hs.toUpperCase();    }    private static byte[] hex2byte(byte[] b) {        if ((b.length % 2) != 0)            throw new IllegalArgumentException("length not even");        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;    }    private static String decode(String src, String key) {        String decryptStr = "";        try {            byte[] decrypt = decrypt(hex2byte(src.getBytes()), key.getBytes());            decryptStr = new String(decrypt);        } catch (Exception e) {            e.printStackTrace();        }        return decryptStr;    }    private static String encode(String src, String key){        byte[] bytes = null;        String encryptStr = "";        try {            bytes = encrypt(src.getBytes(), key.getBytes());        } catch (Exception ex) {            ex.printStackTrace();        }        if (bytes != null)            encryptStr = byte2hex(bytes);        return encryptStr;    }    /**     * 解密     */    public static String decode(String src) {        return decode(src, KEY);    }    /**     * 加密     */    public static String encode(String src) {        return encode(src, KEY);    }}

测试

public static void main(String[] args) {    String ss = "uu123@#$";    String encodeSS = encode(ss);    System.out.println(encodeSS);    String decodeSS = decode(encodeSS);    System.out.println(decodeSS);}result:    6EA84873A948B299936006D75B7CA819    uu123@#$

 

转载于:https://www.cnblogs.com/syp172654682/p/9128706.html

你可能感兴趣的文章
进程管理3--经典的进程同步问题
查看>>
正则表达式大全
查看>>
PostgreSQL 长事务中DML产生的数据无法被及时纳入统计信息导致的问题
查看>>
btrfs 使用指南 - 1 概念,创建,块设备管理,性能优化
查看>>
PostgreSQL 另类advisory lock保证唯一约束法
查看>>
机房重构之存储过程
查看>>
Android使用HttpClient实现文件上传到PHP服务器,并监控进度条
查看>>
Java的特点
查看>>
Apache的htaccess文件出现500错误的原因
查看>>
iOS图片压缩处理
查看>>
Codeforces Round #333 (Div. 2) A. Two Bases
查看>>
【hibernate框架】用Annotation注解表示id生成策略
查看>>
TypeScript 在 React 中使用总结
查看>>
如何将Excel转换成Markdown表格
查看>>
高仿QQ 发送图片高亮HaloProgressView
查看>>
netty入门
查看>>
我最近写了个开源项目Datura
查看>>
ubuntu编译Android源码填坑记
查看>>
Python关于使用subprocess.Popen时遇到的一个小问题记录
查看>>
Python统计列表元素出现次数
查看>>