• AES加密解密


    转自:https://blog.csdn.net/qq_33317238/article/details/122108861?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~aggregatepage~first_rank_ecpm_v1~rank_v31_ecpm-1-122108861.pc_agg_new_rank&utm_term=springboot+%E5%BC%95%E5%85%A5aes%E5%8A%A0%E5%AF%86&spm=1000.2123.3001.4430

    转自:https://blog.csdn.net/qq_28205153/article/details/55798628

    转自: https://www.zhihu.com/question/20874499

    转自:https://blog.csdn.net/l18848956739/article/details/83184243

    package com.zhhs.app.utils;
    
    import org.apache.commons.codec.binary.Base64;
    import org.apache.commons.lang.StringUtils;
    
    import javax.crypto.Cipher;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;
    import java.nio.charset.StandardCharsets;
    
    public class AesUtil {
          /**
         * 密钥 (需要前端和后端保持一致)十六位作为密钥
         */
        private static final String KEY = "SwScqcJqO1NIKPwB";
        /**
         * 密钥偏移量 (需要前端和后端保持一致)十六位作为密钥偏移量
         */
        private static final String IV = "0FVo7C4JQrU8wuR4";
    
        /**
         * base 64 decode
         *
         * @param base64Code 待解码的base 64 code
         * @return 解码后的byte[]
         */
        public static byte[] base64Decode(String base64Code) {
            return StringUtils.isEmpty(base64Code) ? null : Base64.decodeBase64(base64Code.getBytes());
        }
    
        /**
         * AES解密
         *
         * @param encryptBytes 待解密的byte[]
         * @return 解密后的String
         * @throws Exception
         */
        public static String aesDecryptByBytes(byte[] encryptBytes) throws Exception {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    
            byte[] temp = IV.getBytes(StandardCharsets.US_ASCII);
            IvParameterSpec iv = new IvParameterSpec(temp);
    
            cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(KEY.getBytes(StandardCharsets.US_ASCII), "AES"), iv);
            byte[] decryptBytes = cipher.doFinal(encryptBytes);
    
            return new String(decryptBytes);
        }
    
        /**
         * 将base 64 code AES解密
         *
         * @param encryptStr 待解密的base 64 code
         * @return 解密后的string
         * @throws Exception
         */
        public static String aesDecrypt(String encryptStr) throws Exception {
            return StringUtils.isEmpty(encryptStr) ? null : aesDecryptByBytes(base64Decode(encryptStr));
        }
    
        //加密
        @SuppressWarnings("unused")
        public static String aesEncrypt(String str) {
            try {
                Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
                IvParameterSpec iv = new IvParameterSpec(IV.getBytes(StandardCharsets.US_ASCII));
                cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(KEY.getBytes(StandardCharsets.US_ASCII), "AES"), iv);
                byte[] encryptBytes = cipher.doFinal(str.getBytes(StandardCharsets.UTF_8));
    
                return new String(Base64.encodeBase64(encryptBytes), StandardCharsets.US_ASCII);
            } catch (Exception e) {
                e.printStackTrace();
                return "";
            }
        }
    
        /**
         * 生成一个可选长度的随机数列
         *
         * @param
         */
        public static String getRandomCode(int length) {
            String[] arr = new String[]{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G",
                    "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X",
                    "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
                    "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
            String str = "";
            for (int i = 0; i < length; i++) {
                int r = (int) (Math.random() * 61);
                str += arr[r];
            }
            return str;
        }
    }
  • 相关阅读:
    数据验证及文件操作
    Leecode336
    Leecode335
    正式回归
    自定义按钮的实现 windows phone
    asp.net中保存更改数据
    asp.net中可以这样序列化
    Asp.net mvc 3 实现进度条上传思路[转]
    server.MapPath
    Path.Combine (合并两个路径字符串)方法的一些使用细节
  • 原文地址:https://www.cnblogs.com/person008/p/16083059.html
Copyright © 2020-2023  润新知