zoukankan      html  css  js  c++  java
  • 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();
        }
    
    }
    
  • 相关阅读:
    Ubuntu11.04中如何将pycharm添加到系统的“应用程序”菜单里 (pycharm已成功安装)
    Office 365 离线安装
    关于wxpy,使用Python玩转微信的问题
    python3安装scrapy问题解决
    python打包exe文件-ImportError: No module named 'queue'
    Linux TOP命令按内存占用排序和按CPU占用排序
    设置iptables允许ssh、http、ftp服务
    重置linux mysql root密码
    linux.go
    return_fun.go 源码阅读
  • 原文地址:https://www.cnblogs.com/linyufeng/p/13719077.html
Copyright © 2011-2022 走看看