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));
        }
    }
  • 相关阅读:
    Traefik-v2.x快速入门
    jenkins pipeline持续集成
    phpstorm 2017激活码(方法)
    PHP保留两位小数的几种方法
    php 数组排序 按照某字段
    sql大全
    解决jpgraph在php7.0版本下时,无法显示例子图表的问题
    Linux 定时任务crontab使用
    VIM命令操作
    wampserver变橙色,apache 服务无法启动!问题解决小记(安装失败亦可参考)
  • 原文地址:https://www.cnblogs.com/mikuriya/p/14447776.html
Copyright © 2011-2022 走看看