• DES加密解密


    import javax.crypto.Cipher;
    import javax.crypto.SecretKey;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.DESKeySpec;
    import java.security.SecureRandom;
    
    public class DESUtil {
    
        private static Cipher getDES(String key, boolean b) throws Exception {
            SecureRandom sr = new SecureRandom();
            DESKeySpec dks = new DESKeySpec(key.getBytes());
            SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DES");
            SecretKey skey = keyfactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance("DES");
            if (b) {  // 加密
                cipher.init(Cipher.ENCRYPT_MODE, skey, sr);
            } else {  //解密
                cipher.init(Cipher.DECRYPT_MODE, skey, sr);
            }
    
            return cipher;
        }
    
        /**
         * 加密
         *
         * @param txt
         * @param key
         * @return
         * @author 张九怀
         * @email zjh1983314@163.com
         */
        public static String encrypt(String txt, String key) {
            String r = null;
            try {
                Cipher cipher = getDES(key, true);
                byte[] b = cipher.doFinal(txt.getBytes());
                r = byte2hex(b);
    
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
            return r;
    
        }
    
        /**
         * 解密
         *
         * @param d
         * @param key
         * @return
         * @author 张九怀
         * @email zjh1983314@163.com
         */
        public static String decrypt(String d, String key) {
            String r = null;
            try {
                Cipher cipher = getDES(key, false);
                byte[] b = cipher.doFinal(hex2byte(d));
                r = new String(b);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
            return r;
        }
    
        private static byte[] hex2byte(String d) throws IllegalArgumentException {
            if (d.length() % 2 != 0) {
                throw new IllegalArgumentException();
            }
            char[] arr = d.toCharArray();
            byte[] b = new byte[d.length() / 2];
            for (int i = 0, j = 0; i < d.length(); i++, j++) {
                String wap = "" + arr[i++] + arr[i];
                int byteint = Integer.parseInt(wap, 16) & 0xFF;
                b[j] = new Integer(byteint).byteValue();
            }
            return b;
        }
    
        /**
         * byte 转 16进制
         *
         * @param b
         * @return
         * @author 张九怀
         * @email zjh1983314@163.com
         */
        private static String byte2hex(byte[] b) {
            StringBuffer sb = new StringBuffer();
            for (int n = 0; n < b.length; n++) {
                String stmp = (Integer.toHexString(b[n] & 0XFF));
                if (stmp.length() == 1) {
                    sb.append("0" + stmp);
                } else {
                    sb.append(stmp);
                }
            }
            return sb.toString();
        }
    
    }
    
  • 相关阅读:
    眼睛的颜色 博弈
    codevs1281 矩阵乘法 快速幂 !!!手写乘法取模!!! 练习struct的构造函数和成员函数
    10 25日考试 数学题目练习 斐波拉契 打表
    线段树 模板
    榨取kkksc03 luogu1855 dp 裸二维费用背包
    低价购买 洛谷1108 codevs4748 dp
    [转] 经典排序算法
    [USACO08DEC] Trick or Treat on the Farm
    [NOIP2009] 靶形数独(搜索+剪枝)
    各种蒟蒻模板【如此简单】
  • 原文地址:https://www.cnblogs.com/linyufeng/p/13719077.html
Copyright © 2020-2023  润新知