zoukankan      html  css  js  c++  java
  • AES/DES 可逆性加密算法 -- java工具类

    package com.lock.demo.service;
    
    import org.apache.tomcat.util.codec.binary.Base64;
    
    import javax.crypto.Cipher;
    import javax.crypto.SecretKey;
    import javax.crypto.SecretKeyFactory;
    import javax.crypto.spec.DESKeySpec;
    
    /**
     * @author niunafei
     * @function
     *         des 对称性算法加密 解密工具类 可逆性算法
     * @email niunafei0315@163.com
     * @date 2018/12/12  下午2:05
     */
    public class DesUtils {
    
        private static final String DES="DES";
    
        /**
         * 公钥  8位以上
         */
        private static final String SECRET_KEY="12345678";
    
        /**
         * 获取秘钥对象
         * @return
         * @throws Exception
         */
        private static final SecretKey getSecretKeyFactory() throws Exception {
            SecretKeyFactory des = SecretKeyFactory.getInstance(DES);
            SecretKey secretKey = des.generateSecret(new DESKeySpec(SECRET_KEY.getBytes()));
            return secretKey;
        }
    
        /**
         * 加密
         * @param param
         * @return
         * @throws Exception
         */
        public static final String encryption(String param) throws Exception {
            Cipher cipher = Cipher.getInstance(DES);
            SecretKey secretKey = getSecretKeyFactory();
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            return  new String(Base64.encodeBase64(cipher.doFinal(param.toString().getBytes())));
        }
    
        /**
         * 解密
         * @param value
         * @return
         * @throws Exception
         */
        public static final String decrypt(String value) throws Exception {
            Cipher cipher = Cipher.getInstance(DES);
            SecretKey secretKey = getSecretKeyFactory();
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            return new String(cipher.doFinal(Base64.decodeBase64(value.getBytes())));
        }
    
        /**
         测试
         */
        public static void main(String[] args) throws Exception {
            String key="123";
            System.out.println(" key="+key);
            //输出 key=123
            String value=DesUtils.encryption(key);
            System.out.println("encryption value="+value);
            //输出 encryption value=LDiFUdf0iew=
            System.out.println("decrypt key="+DesUtils.decrypt(value));
            //输出 decrypt key=123
    
        }
    }

    以上为des 加密算法

    以下为aes 可逆性加密算法

    package com.lock.demo.service;
    
    import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;
    
    import javax.crypto.Cipher;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
    
    /**
     * @author niunafei
     * @function
     * @email niunafei0315@163.com
     * @date 2018/12/12  下午2:32
     */
    public class AESUtils {
    
        private static final String AES="AES";
        private static final String CHAR_SET_NAME1="UTF-8";
        private static final String CHAR_SET_NAME2="ASCII";
        private static final String CIPHER_KEY="AES/CBC/PKCS5Padding";
    
        /**
         * 加密用的Key 可以用26个字母和数字组成 此处使用AES-128-CBC加密模式,key需要为16位。
         */
        private static final String IV_PARAMETER="a0.l954b_107x90l";
        /**
         * 可以用26个字母和数字组成 此处使用AES-128-CBC加密模式,需要为16位。
         */
        private static final String S_KEY="ax7x90.3k_10li5u";
    
    
        /**
         * 加密
         * @param param
         * @return
         * @throws Exception
         */
        public static String encryption(String param) throws Exception {
                Cipher cipher= Cipher.getInstance(CIPHER_KEY);
                SecretKeySpec skeySpec = new SecretKeySpec(S_KEY.getBytes(), AES);
                // 使用CBC模式,需要一个向量iv,可增加加密算法的强度
                IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes());
                cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
            // 此处使用BASE64做转码。
            return new BASE64Encoder().encode(cipher.doFinal(param.getBytes(CHAR_SET_NAME1)));
    
        }
    
        /**
         *  解密
         * @param value
         * @return
         * @throws Exception
         */
        public static String decrypt(String value) throws Exception {
                SecretKeySpec skeySpec = new SecretKeySpec(S_KEY.getBytes(CHAR_SET_NAME2), AES);
                Cipher cipher = Cipher.getInstance(CIPHER_KEY);
                IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes());
                cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
                // 先用base64解密
                return new String(cipher.doFinal(new BASE64Decoder().decodeBuffer(value)), CHAR_SET_NAME1);
        }
    
    
        /**
         测试
         */
        public static void main(String[] args) throws Exception {
            String key="123";
            System.out.println("key="+key);
            //输出 key=123
            String value=AESUtils.encryption(key);
            System.out.println("encryption value="+value);
            //输出 encryption value=OTslJ40Fa9a7ImOmCbmLPw==
            System.out.println("decrypt key="+AESUtils.decrypt(value));
            //输出 decrypt key=123
    
        }
    }
    

      

    加密结果适用于url参数。 请使用异或可逆性算法 或者使用

    URLEncoder.encode();
    URLDecoder.decode() 进行转码即可

    url出现了有+,空格,/,?,%,#,&,=等特殊符号的时候,可能在服务器端无法获得正确的参数值,如何是好?
    解决办法
    将这些字符转化成服务器可以识别的字符,对应关系如下:
    URL字符转义

    用其它字符替代吧,或用全角的。

    +    URL 中+号表示空格                            %2B   
    空格 URL中的空格可以用+号或者编码           %20 
    /     分隔目录和子目录                              %2F     
    ?     分隔实际的URL和参数                         %3F     
    %    指定特殊字符                                   %25     
    #    表示书签                                         %23     
    &    URL 中指定的参数间的分隔符                %26     
    =    URL 中指定参数的值                           %3D

  • 相关阅读:
    迁移
    zendframework 2 链接数据库
    zendframework 2
    merge sort and quick sort 自己去理解吧
    ubuntu git 使用
    resumablejs 分块上传 断点续传
    video.js html5 视频播放器
    swfupload 例子
    php 润年 星期 天数
    ubuntu 安装 axel
  • 原文地址:https://www.cnblogs.com/niunafei/p/10108180.html
Copyright © 2011-2022 走看看