• EncryptUtils


    package me.zhengjie.core.utils;
    
    import org.springframework.util.DigestUtils;
    import javax.crypto.Cipher;
    import javax.crypto.SecretKey;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.DESKeySpec;
    import javax.crypto.spec.IvParameterSpec;
    
    /**
     * 加密
     * @author jie
     * @date 2018-11-23
     */
    public class EncryptUtils {
    
        private static String strKey = "Passw0rd", strParam = "Passw0rd";
    
        /**
         * 对称加密
         * @param source
         * @return
         * @throws Exception
         */
        public static String desEncrypt(String source) throws Exception {
            if (source == null || source.length() == 0){
                return null;
            }
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            DESKeySpec desKeySpec = new DESKeySpec(strKey.getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            IvParameterSpec iv = new IvParameterSpec(strParam.getBytes("UTF-8"));
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
            return byte2hex(
                    cipher.doFinal(source.getBytes("UTF-8"))).toUpperCase();
        }
    
        public static String byte2hex(byte[] inStr) {
            String stmp;
            StringBuffer out = new StringBuffer(inStr.length * 2);
            for (int n = 0; n < inStr.length; n++) {
                stmp = Integer.toHexString(inStr[n] & 0xFF);
                if (stmp.length() == 1) {
                    // 如果是0至F的单位字符串,则添加0
                    out.append("0" + stmp);
                } else {
                    out.append(stmp);
                }
            }
            return out.toString();
        }
    
    
        public 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;
        }
    
        /**
         * 对称解密
         * @param source
         * @return
         * @throws Exception
         */
        public static String desDecrypt(String source) throws Exception {
            if (source == null || source.length() == 0){
                return null;
            }
            byte[] src = hex2byte(source.getBytes());
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            DESKeySpec desKeySpec = new DESKeySpec(strKey.getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            IvParameterSpec iv = new IvParameterSpec(strParam.getBytes("UTF-8"));
            cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
            byte[] retByte = cipher.doFinal(src);
            return new String(retByte);
        }
    
        /**
         * 密码加密
         * @param password
         * @return
         */
        public static String encryptPassword(String password){
            return  DigestUtils.md5DigestAsHex(password.getBytes());
        }
    
        public static void main(String[] args) {
            System.out.println(encryptPassword("e10adc3949ba59abbe56e057f20f883e"));
        }
    }
    package me.zhengjie.core.utils;
    
    import org.springframework.util.DigestUtils;
    import javax.crypto.Cipher;
    import javax.crypto.SecretKey;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.DESKeySpec;
    import javax.crypto.spec.IvParameterSpec;
    
    /**
     * 加密
     * @author jie
     * @date 2018-11-23
     */
    public class EncryptUtils {
    
        private static String strKey = "Passw0rd", strParam = "Passw0rd";
    
        /**
         * 对称加密
         * @param source
         * @return
         * @throws Exception
         */
        public static String desEncrypt(String source) throws Exception {
            if (source == null || source.length() == 0){
                return null;
            }
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            DESKeySpec desKeySpec = new DESKeySpec(strKey.getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            IvParameterSpec iv = new IvParameterSpec(strParam.getBytes("UTF-8"));
            cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
            return byte2hex(
                    cipher.doFinal(source.getBytes("UTF-8"))).toUpperCase();
        }
    
        public static String byte2hex(byte[] inStr) {
            String stmp;
            StringBuffer out = new StringBuffer(inStr.length * 2);
            for (int n = 0; n < inStr.length; n++) {
                stmp = Integer.toHexString(inStr[n] & 0xFF);
                if (stmp.length() == 1) {
                    // 如果是0至F的单位字符串,则添加0
                    out.append("0" + stmp);
                } else {
                    out.append(stmp);
                }
            }
            return out.toString();
        }
    
    
        public 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;
        }
    
        /**
         * 对称解密
         * @param source
         * @return
         * @throws Exception
         */
        public static String desDecrypt(String source) throws Exception {
            if (source == null || source.length() == 0){
                return null;
            }
            byte[] src = hex2byte(source.getBytes());
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            DESKeySpec desKeySpec = new DESKeySpec(strKey.getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            IvParameterSpec iv = new IvParameterSpec(strParam.getBytes("UTF-8"));
            cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
            byte[] retByte = cipher.doFinal(src);
            return new String(retByte);
        }
    
        /**
         * 密码加密
         * @param password
         * @return
         */
        public static String encryptPassword(String password){
            return  DigestUtils.md5DigestAsHex(password.getBytes());
        }
    
        public static void main(String[] args) {
            System.out.println(encryptPassword("e10adc3949ba59abbe56e057f20f883e"));
        }
    }
  • 相关阅读:
    数据结构复习代码——栈的顺序实现以及一些基本操作
    数据结构复习代码——栈的顺序实现下的相关应用
    数据结构复习代码——线性表结构体定义,线性表动态内存分配的初始化以及元素插入
    数据结构复习代码——顺序结构下实现循环队列、基于顺寻存储串结构相关操作的实现
    数据结构复习代码——堆栈结构下串基本操作的实现
    数据结构复习代码——线性表合并
    数据结构小学期(温习温习。。。)(2021)——实现矩阵的加减乘、矩阵的逆、矩阵的特征值和特征值等运算
    clock moved backwards. Refusing to generate id for XX milliseconds.
    rsync实时备份监控命令(详细大全) rsync实时备份监控命令(详细大全)
    在Linux服务器上安装VNC并进行远程桌面连接
  • 原文地址:https://www.cnblogs.com/tonggc1668/p/11216940.html
Copyright © 2020-2023  润新知