• 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();
        }
    
    }
    
  • 相关阅读:
    分布式集群环境下运行Wordcount程序
    VM搭建hadoop分布式集群
    安装运行Hadoop
    网络问题
    Golang依赖工具
    会话进程组终端 · 守护进程
    Golang笔记
    [转]GDB
    [转]用户态与内核态
    【转】linux环境内存分配原理 malloc info
  • 原文地址:https://www.cnblogs.com/linyufeng/p/13719077.html
Copyright © 2020-2023  润新知