<script src="../static/ajax/libs/captcha/crypto-js.min.js" th:src="@{/ajax/libs/captcha/crypto-js.min.js}"></script>
<script src="../static/ajax/libs/captcha/ase.min.js" th:src="@{/ajax/libs/captcha/ase.min.js}"></script>
4、在注册按钮的点击事件中,设置 key 值和 iv 值,这是加密的设置字段,定义要进行加密的密码 encryptedPwd
function register() {
var password = $.common.trim($("input[name='password']").val());
//AES加密
var key = "ABCDEFGHIJKL\_key";
var iv = "ABCDEFGHIJKLM\_iv";
var encryptedPwd = aesMinEncrypt(key,iv,password);
$.ajax({
type: "post",
url: ctx + "register",
data: {
"password": encryptedPwd,
...
...
}
success: function (res) {}
});
}
function aesMinEncrypt(key, iv, word){
var _word = CryptoJS.enc.Utf8.parse(word),
_key = CryptoJS.enc.Utf8.parse(key),
_iv = CryptoJS.enc.Utf8.parse(iv);
var encrypted = CryptoJS.AES.encrypt(_word, _key, {
iv: _iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
return encrypted.toString();
}
二、后端解密
后端解密需要用到一个工具类:AESUtils,里面是解密方法代码如下:
1、添加AESUtils工具类
package com.wuye.common.utils;
import cn.jpush.api.utils.StringUtils;
import sun.misc.BASE64Decoder;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/\*\*
\* TODO
\*
\* @author CSD
\* @date 2022-07-28 14:16
\*/
public class AESUtils {
//密钥 (需要前端和后端保持一致)十六位作为密钥
private static final String KEY = "ABCDEFGHIJKL\_key";
//密钥偏移量 (需要前端和后端保持一致)十六位作为密钥偏移量
private static final String IV = "ABCDEFGHIJKLM\_iv";
//算法
private static final String ALGORITHMSTR = "AES/CBC/PKCS5Padding";
/\*\*
\* base 64 decode
\* @param base64Code 待解码的base 64 code
\* @return 解码后的byte[]
\* @throws Exception
\*/
public static byte[] base64Decode(String base64Code) throws Exception{
return StringUtils.isEmpty(base64Code) ? null : new BASE64Decoder().decodeBuffer(base64Code);
}
/\*\*
\* AES解密
\* @param encryptBytes 待解密的byte[]
\* @return 解密后的String
\* @throws Exception
\*/
public static String aesDecryptByBytes(byte[] encryptBytes) throws Exception {
Cipher cipher = Cipher.getInstance(ALGORITHMSTR);
byte[] temp = IV.getBytes("UTF-8");
IvParameterSpec iv = new IvParameterSpec(temp);
cipher.init(Cipher.DECRYPT\_MODE, new SecretKeySpec(KEY.getBytes(), "AES"), iv);
byte[] decryptBytes = cipher.doFinal(encryptBytes);
### 最后
整理面试题,不是让大家去只刷面试题,而是熟悉目前实际面试中常见的考察方式和知识点,做到心中有数,也可以用来自查及完善知识体系。
**《前端基础面试题》,《前端校招面试题精编解析大全》,《前端面试题宝典》,《前端面试题:常用算法》**
![](https://img-blog.csdnimg.cn/img_convert/0eaa3f8d69d2e64abf8b0d694f6d248e.webp?x-oss-process=image/format,png)
![前端面试题宝典](https://img-blog.csdnimg.cn/img_convert/e584c0a43610d0a8958d4586354cc3d2.webp?x-oss-process=image/format,png)
![前端校招面试题详解](https://img-blog.csdnimg.cn/img_convert/b56a1898d6b62fb5e091a398b0354811.webp?x-oss-process=image/format,png)
链图片转存中...(img-V8eyXCNg-1718089033385)]
[外链图片转存中...(img-7Kv8z7Mq-1718089033386)]
[外链图片转存中...(img-qJ5QCaTJ-1718089033387)]