AES前端加密后端解密详细步骤

3 篇文章 0 订阅

一、前端安装crypto-js,后端pom.xml中引入依赖

npm install crypto-js 
yarn add crypto-js 
<!--引入  AES/ECB/PKCS7Padding  支持-->
<dependency>
   <groupId>org.bouncycastle</groupId>
   <artifactId>bcprov-jdk16</artifactId>
   <version>1.46</version>
</dependency>

二、前端代码js

const CryptoJS = require('crypto-js');  //引用AES源码js
/**
 * AES加密
 */
export const AESEncrypt = (str, key) => {
  let _key = key || '5F6B2AK33DASD1235E74C231B47AC8F6'
  const __key = CryptoJS.enc.Utf8.parse(_key) //将秘钥转换成Utf8字节数组
  // const iv = CryptoJS.enc.Utf8.parse(_key.substr(0, 16))
  //加密
  const encrypt = CryptoJS.AES.encrypt(JSON.stringify(str), __key, {
    // iv: iv,
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  })
  return encrypt.toString()
}

/**
 * AES解密密
 */
export const AESDecrypt = (str, key) => {
  let _key = key || '5F6B2AK33DASD1235E74C231B47AC8F6'
  const __key = CryptoJS.enc.Utf8.parse(_key) //将秘钥转换成Utf8字节数组

  const decrypt = CryptoJS.AES.decrypt(str, __key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
  })
  const _decrypt = JSON.parse(decrypt.toString(CryptoJS.enc.Utf8)) //解密后的数据
  return _decrypt
}

三、前端调用

import { AESEncrypt } from "@/util/AES.js";

页面中直接使用
AESEncrypt(this.form.userName,null);

四、后端AESUtil类

package com.soft863.common.utils;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;
import org.apache.commons.codec.binary.Base64;
import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.Security;
public class AESUtil {
	/**
	 * AES加密
	 *
	 * @param content 明文
	 * @param key     秘钥
	 * @return
	 * @throws Exception
	 */
	public static String encrypt(String content, String key) throws Exception {
		key="5F6B2AK33DASD1235E74C231B47AC8F6";
		// 将返回的加密过的 byte[] 转换成Base64编码字符串 !!!!很关键
		return base64ToString(AES_ECB_Encrypt(content.getBytes(), key.getBytes()));
	}
	/**
	 * AES解密
	 *
	 * @param content Base64编码的密文
	 * @param key     秘钥
	 * @return
	 * @throws Exception
	 */
	public static String decrypt(String content, String key) {
		key="5F6B2AK33DASD1235E74C231B47AC8F6";
		// stringToBase64() 将 Base64编码的字符串转换成 byte[] !!!与base64ToString()配套使用
		try {
			byte[] base64 = stringToBase64(content);
			byte[] bytes = AES_ECB_Decrypt(base64, key.getBytes());
			String result = new String(bytes);
			return result.replaceAll("\"","");
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

	private static byte[] AES_ECB_Encrypt(byte[] content, byte[] keyBytes) {
		try {
			SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
			Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
			cipher.init(Cipher.ENCRYPT_MODE, key);
			byte[] result = cipher.doFinal(content);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private static byte[] AES_ECB_Decrypt(byte[] content, byte[] keyBytes) {
		try {
			SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
			Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
			Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding");
			cipher.init(Cipher.DECRYPT_MODE, key);
			byte[] result = cipher.doFinal(content);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 字符串装换成 Base64
	 */

	public static byte[] stringToBase64(String key) throws Exception {
		return Base64.decodeBase64(key.getBytes());
	}

	/**
	 * Base64装换成字符串
	 */
	public static String base64ToString(byte[] key) throws Exception {
		return new Base64().encodeToString(key);
	}

	public static void main(String[] args) throws Exception {
		String key = "5F6B2AK33DASD1235E74C231B47AC8F6";//加密密钥,很关键,不要对外泄露哦

		String content = "ashdghjadsw_2022";//明文

		String end = encrypt(content, key);
		System.out.println("加密:" + end);

		String decrypt = decrypt(encrypt(content, key), key);
		System.out.println("解密:"+decrypt);
	}
}

五、后端使用

//前端加密后传过来的值
String name="a9BN/Zu9fgRLK7631c6+2mwtWomnS9dyW0MYNVgXgjE=";
AESUtil.decrypt(name,"");
System.out.println("通过前端传来的加密值,解密出对应的明文值:"+AESUtil.decrypt(name,""));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值