zoukankan      html  css  js  c++  java
  • Des加密解密算法java实现

    package tech.fullink.eaglehorn.utils;
    
    import javax.crypto.Cipher;
    import javax.crypto.SecretKey;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.DESKeySpec;
    import javax.crypto.spec.IvParameterSpec;
    
    /**
     * DES CBC 加密、解密算法
     * 
     * @author xiaoliang.chen
     * @version $Id: DesCbcSecurity.java, v 0.1 2017年12月16日 下午12:55:29 xiaoliang.chen Exp $
     */
    public class DesCbcSecurity {
    
        public static final String SECRET_DES    = "DES";
        public static final String ALGORITHM_DES = "DES/CBC/PKCS5Padding";
    
        public static void main(String[] args) throws Exception {
            String aaaString = "{"customerUserId": "10","customerUserType": 1,"name": "hello","mobile": "13366666666","idCardNo": "330106200009096666","deviceId": "qwert","token": "qwertyuiop12345"}";
            String key = "0uw5Wzwe";
            System.out.println("加密前:" + aaaString);
            String encrypedString = encrypt(aaaString, key);
            
            System.out.println("加密后: " + encrypedString);
            String bString = decrypt(encrypedString, key);
            System.out.println("解密后:" + bString);
        }
        /**
         * 加密
         * @author xiaoliang.chen
         * 2017年12月16日 下午12:59:28
         * @param content
         * @param key
         * @return
         */
        public static String encrypt(String content, String key) {
            return byteToHexString(encrypt(content.getBytes(), key.getBytes()));
        }
    
        public static byte[] encrypt(byte[] content, byte[] keyBytes) {
            try {
                DESKeySpec keySpec = new DESKeySpec(keyBytes);
                SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(SECRET_DES);
                SecretKey key = keyFactory.generateSecret(keySpec);
    
                Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
                cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(keySpec.getKey()));
                byte[] result = cipher.doFinal(content);
                return result;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
        /**
         * 解密
         * @author xiaoliang.chen
         * 2017年12月16日 下午1:01:01
         * @param content
         * @param key
         * @return
         */
        public static String decrypt(String content, String key) {
            byte[] contentBytes = hexStringToBytes(content);
            return decrypt(contentBytes, key.getBytes());
        }
        
        public static String decrypt(byte[] content, byte[] keyBytes) {
            try {
                DESKeySpec keySpec = new DESKeySpec(keyBytes);
                SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(SECRET_DES);
                SecretKey key = keyFactory.generateSecret(keySpec);
    
                Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
                cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(keyBytes));
                byte[] result = cipher.doFinal(content);
                String contentString = new String(result);
                return contentString;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }
    
        public static String byteToHexString(byte[] bytes) {
            StringBuffer sb = new StringBuffer(bytes.length);
            String sTemp;
            for (int i = 0; i < bytes.length; i++) {
                sTemp = Integer.toHexString(0xFF & bytes[i]);
                if (sTemp.length() < 2)
                    sb.append(0);
                sb.append(sTemp.toUpperCase());
            }
            return sb.toString();
        }
        
        public static byte[] hexStringToBytes(String hexString) {
            if (hexString == null || hexString.equals("")) {
                return null;
            }
            hexString = hexString.toUpperCase();
            int length = hexString.length() / 2;
            char[] hexChars = hexString.toCharArray();
            byte[] d = new byte[length];
            for (int i = 0; i < length; i++) {
                int pos = i * 2;
                d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
    
            }
            return d;
        }
    
        private static byte charToByte(char c) {
            return (byte) "0123456789ABCDEF".indexOf(c);
        }
    }
    

      

  • 相关阅读:
    jquery获取tr并更改tr内容
    jquery获取元素索引值index()
    禁止apache显示目录索引 apache禁止列目录
    mysql启动错误之mysql启动报1067错误如何解决
    Expo大作战(四)--快速用expo构建一个app,expo中的关键术语
    Expo大作战(三)--针对已经开发过react native项目开发人员有针对性的介绍了expo,expo的局限性,开发时项目选型注意点等
    Expo大作战(二)--expo的生命周期,expo社区交流方式,expo学习必备资源,开发使用expo时关注的一些问题
    Expo大作战(一)--什么是expo,如何安装expo clinet和xde,xde如何使用
    Linux(CentOS)之-性能监控
    [转]winform程序textbox滚动条保持在最下面 内容不闪烁
  • 原文地址:https://www.cnblogs.com/cs99lzzs/p/7245960.html
Copyright © 2011-2022 走看看