java AES加密

1、引入maven

<!-- Base64编码需要  -->
        <dependency>
            <groupId>org.apache.directory.studio</groupId>
            <artifactId>org.apache.commons.codec</artifactId>
            <version>1.8</version>
        </dependency>
        <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk15on</artifactId>
            <version>1.56</version>
        </dependency>

 2、代码

  1 package com.hlt.ctms.org.utils;
  2 
  3 import org.apache.commons.codec.binary.Base64;
  4 import org.bouncycastle.jce.provider.BouncyCastleProvider;
  5 
  6 import javax.crypto.Cipher;
  7 import javax.crypto.spec.SecretKeySpec;
  8 import java.nio.charset.StandardCharsets;
  9 import java.security.SecureRandom;
 10 import java.util.Random;
 11 
 12 /**
 13  * AES加、解密算法工具类
 14  */
 15 public class AesUtil {
 16     /**
 17      * 加密算法AES
 18      */
 19     private static final String KEY_ALGORITHM = "AES";
 20 
 21     /**
 22      * key的长度,Wrong key size: must be equal to 128, 192 or 256
 23      * 传入时需要16、24、36
 24      */
 25     private static final Integer KEY_LENGTH = 16 * 8;
 26 
 27     /**
 28      * 算法名称/加密模式/数据填充方式
 29      * 默认:AES/ECB/PKCS5Padding
 30      */
 31     private static final String ALGORITHMS = "AES/ECB/PKCS5Padding";
 32 
 33     /**
 34      * 后端AES的key,由静态代码块赋值
 35      */
 36     public static String key;
 37 
 38     static {
 39         key = getKey();
 40     }
 41 
 42     /**
 43      * 获取key
 44      */
 45     public static String getKey() {
 46         StringBuilder uid = new StringBuilder();
 47         //产生16位的强随机数
 48         Random rd = new SecureRandom();
 49         for (int i = 0; i < KEY_LENGTH / 8; i++) {
 50             //产生0-2的3位随机数
 51             int type = rd.nextInt(3);
 52             switch (type) {
 53                 case 0:
 54                     //0-9的随机数
 55                     uid.append(rd.nextInt(10));
 56                     break;
 57                 case 1:
 58                     //ASCII在65-90之间为大写,获取大写随机
 59                     uid.append((char) (rd.nextInt(25) + 65));
 60                     break;
 61                 case 2:
 62                     //ASCII在97-122之间为小写,获取小写随机
 63                     uid.append((char) (rd.nextInt(25) + 97));
 64                     break;
 65                 default:
 66                     break;
 67             }
 68         }
 69         return uid.toString();
 70     }
 71 
 72     /**
 73      * 加密
 74      *
 75      * @param content    加密的字符串
 76      * @param encryptKey key值
 77      */
 78     public static String encrypt(String content, String encryptKey) throws Exception {
 79         //设置Cipher对象
 80         Cipher cipher = Cipher.getInstance(ALGORITHMS,new BouncyCastleProvider());
 81         cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(encryptKey.getBytes(), KEY_ALGORITHM));
 82 
 83         //调用doFinal
 84         byte[] b = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
 85 
 86         // 转base64
 87         return Base64.encodeBase64String(b);
 88 
 89     }
 90 
 91     /**
 92      * 解密
 93      *
 94      * @param encryptStr 解密的字符串
 95      * @param decryptKey 解密的key值
 96      */
 97     public static String decrypt(String encryptStr, String decryptKey) throws Exception {
 98         //base64格式的key字符串转byte
 99         byte[] decodeBase64 = Base64.decodeBase64(encryptStr);
100 
101         //设置Cipher对象
102         Cipher cipher = Cipher.getInstance(ALGORITHMS,new BouncyCastleProvider());
103         cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), KEY_ALGORITHM));
104 
105         //调用doFinal解密
106         byte[] decryptBytes = cipher.doFinal(decodeBase64);
107         return new String(decryptBytes);
108     }
109 }

3、测试

 1 //16位
 2         String key = "MIGfMA0GCSqGSIb3";
 3 
 4         //字符串
 5         String str = "测试下加密";
 6         try {
 7             //加密
 8             String encrypt = AesUtil.encrypt(str, key);
 9             //解密
10             String decrypt = AesUtil.decrypt(encrypt, key);
11 
12             System.out.println("加密前:" + str);
13             System.out.println("加密后:" + encrypt); //
14             System.out.println("解密后:" + decrypt);
15         } catch (Exception e) {
16             e.printStackTrace();
17         }

加密前:测试下加密
加密后:K50t0as9ReWNyzrH5MAiig==
解密后:测试下加密

原文地址:https://www.cnblogs.com/gide/p/14120245.html