Aes加密
-
需要引入的maven依赖如下:
<dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk15on</artifactId> <version>1.55</version> </dependency>
-
代码如下:
import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.util.encoders.Hex; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import java.security.AlgorithmParameters; import java.security.Key; import java.security.Security; /** * @author Jie.Lei * @date 2021/3/8 */ public class AesCbcPks7Utils { static { //导入支持AES/CBC/PKCS7Padding的Provider //解决 java.security.NoSuchProviderException: No such provider: BC Security.addProvider(new BouncyCastleProvider()); } /** * 固定的AES密钥KEY */ public static final String sessionKey = "业务平台随机生成16位的字符串key"; /** * 固定AES密钥的IV */ public static final String initVector = "业务平台随机生成16位的字符串初始向量"; /** * 加密字符串 * * @param ctx 需要加密的原始字符串 * @param aesKey aesKey * @param aesIv aesIv * @return 加密后的16字符串 * @throws Exception */ public static String encrypt(String ctx, String aesKey, String aesIv) { try { return Hex.toHexString(encrypt(ctx.getBytes(StandardCharsets.UTF_8), aesKey, aesIv)); } catch (Exception e) { e.printStackTrace(); } return null; } /** * 加密字节数组 * * @param data 需要加密的字节数组 * @param aesKey aesKey * @param aesIv aesIv * @return 加密后的字节数组 * @throws Exception 异常 */ public static byte[] encrypt(byte[] data, String aesKey, String aesIv) throws Exception { //指定算法,模式,填充方式,创建一个Cipher Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); //生成Key对象 Key sKeySpec = new SecretKeySpec(aesKey.getBytes(), "AES"); //把向量初始化到算法参数 AlgorithmParameters params = AlgorithmParameters.getInstance("AES"); params.init(new IvParameterSpec(aesIv.getBytes())); //指定用途,密钥,参数 初始化Cipher对象 cipher.init(Cipher.ENCRYPT_MODE, sKeySpec, params); //指定加密 return cipher.doFinal(data); } /** * 解密字节数组 * * @param encryptedData 加密的字节数组 * @param aesKey AesKey * @param aesIv aesIv * @return 解密后的字节数组 * @throws Exception 异常 */ public static byte[] decrypt(byte[] encryptedData, String aesKey, String aesIv) throws Exception { //指定算法,模式,填充方法 创建一个Cipher实例 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding", "BC"); //生成Key对象 Key sKeySpec = new SecretKeySpec(aesKey.getBytes(), "AES"); //把向量初始化到算法参数 AlgorithmParameters params = AlgorithmParameters.getInstance("AES"); params.init(new IvParameterSpec(aesIv.getBytes())); //指定用途,密钥,参数 初始化Cipher对象 cipher.init(Cipher.DECRYPT_MODE, sKeySpec, params); //执行解密 return cipher.doFinal(encryptedData); } /** * 解密字符串 * * @param ctx 需要解密的字符串 * @param aesKey aesKey * @param aesIv aesIv * @return 返回解密后的数据 * @throws Exception 异常 */ public static String decrypt(String ctx, String aesKey, String aesIv) throws Exception { return new String(decrypt(Hex.decode(ctx), aesKey, aesIv)); } public static void main(String[] args) throws Exception { System.out.println(AesCbcPks7Utils.encrypt("123", "aaaaaaaaaaaaaaaa", "bbbbbbbbbbbbbbbb")); }
- 输出
1f84d73bb413e1c2ac1d968d8c9205cf
- 输出