zoukankan      html  css  js  c++  java
  • Crypt 工具类

    import sun.misc.BASE64Decoder;
    import sun.misc.BASE64Encoder;
    
    import javax.crypto.Cipher;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
    
    public class CryptUtil {
        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 = "123456";
            String value = encryption(key);
            System.out.println("加密:" + encryption(key));
            System.out.println("解密:" + decrypt(value));
        }
    }
  • 相关阅读:
    @Autowired注解与@Qualifier注解搭配使用
    前端中具体实现
    js文件处理
    跨域解决方案
    WebSocket实现
    js触摸操作
    RPC框架gRPC
    RPC框架Thrift
    分布式RPC
    RPC基本原理
  • 原文地址:https://www.cnblogs.com/mikuriya/p/14447776.html
Copyright © 2011-2022 走看看