2024年Web前端最新前后端(JAVA)实现AES对称加解密方式_前端后端对称加解密(2),2024年最新Web前端程序员面试必备的知识点

最后

小编综合了阿里的面试题做了一份前端面试题PDF文档,里面有面试题的详细解析

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

虽只说了一个公司的面试,但我们可以知道大厂关注的东西并举一反三,通过一个知识点延伸到另一个知识点,这是我们要掌握的学习方法,小伙伴们在这篇有学到的请评论点赞转发告诉小编哦,谢谢大家的支持!

import org.apache.commons.lang3.StringUtils;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
* @author
* @description AES加解密(ECB和CBC模式)
* @date 2023/12/8 15:08
*/
public class AESEncryptUtil {

/\*\*

* 加密模式
* ECB: AES/ECB/PKCS5Padding
* CBC: AES/CBC/NoPadding
*/
private static final String[] TRANSFORM_ALGORITHM = new String[]{“AES/ECB/PKCS5Padding”, “AES/CBC/NoPadding”};
/**
* 初始向量样式 IV
*/
private static String iv = “HBJNRU56MDk4NzK6”;
private static final String ALGORITHM = “AES”;
private static final String CHARSET_NAME = “UTF-8”;
/**
* AES加解key样式
*/
public static String ENCRYPT_KEY = “7CC408B24462ABD1”;

/\*\*

* AES的ECB模式加解
* @param data 待加密参数
* @param key 加密key
* @return
*/
public static String encryptECB(String data, String key) {
if (StringUtils.isEmpty(key)) {
throw new IllegalArgumentException(“加密失败,加密key为空”);
}
SecretKeySpec aesKey = new SecretKeySpec(key.getBytes(Charset.forName(CHARSET_NAME)), ALGORITHM);
try {
Cipher cipher = Cipher.getInstance(TRANSFORM_ALGORITHM[0]);
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encrypted = cipher.doFinal(data.getBytes(Charset.forName(CHARSET_NAME)));
// 使用Base64来包装是规避报错Input length must be multiple of 16 when decrypting with padded cipher
// 解密的字节数组必须是16的倍数
return Base64.getEncoder().encodeToString(encrypted);
} catch (Exception e) {
throw new IllegalArgumentException("加密失败: "+ e.getMessage());
}
}

/\*\*

* AES的ECB模式解密
* @param data 待解密参数
* @param key 解密key
* @return
*/
public static String decryptECB(String data, String key) {
if (StringUtils.isEmpty(key)) {
throw new IllegalArgumentException(“解密失败,解密key为空”);
}
byte[] decode = Base64.getDecoder().decode(data.getBytes(StandardCharsets.UTF_8));
SecretKeySpec aesKey = new SecretKeySpec(key.getBytes(Charset.forName(CHARSET_NAME)), ALGORITHM);
try {
Cipher cipher = Cipher.getInstance(TRANSFORM_ALGORITHM[0]);
cipher.init(Cipher.DECRYPT_MODE, aesKey);
return new String(cipher.doFinal(decode));
} catch (Exception e) {
throw new IllegalArgumentException("解密失败: "+ e.getMessage());
}
}

/\*\*

* AES的CBC模式加密
* @param data 要加密的数据
* @param key 加密key
* @return 加密的结果
*/
public static String encryptCBC(String data, String key) {
try {
// “算法/模式/补码方式”
Cipher cipher = Cipher.getInstance(TRANSFORM_ALGORITHM[1]);
int blockSize = cipher.getBlockSize();
byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8);
int plaintextLength = dataBytes.length;
if (plaintextLength % blockSize != 0) {
plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
}
byte[] plaintext = new byte[plaintextLength];
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
SecretKeySpec keyStr = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), “AES”);
IvParameterSpec ivStr = new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8));
cipher.init(Cipher.ENCRYPT_MODE, keyStr, ivStr);
byte[] encrypted = cipher.doFinal(plaintext);
return Base64.getEncoder().encodeToString(encrypted);
} catch (Exception e) {
throw new IllegalArgumentException("加密失败: "+ e.getMessage());
}
}

/\*\*

* AES的CBC模式解密
* @param data 要解密的数据
* @param key 解密key
* @return 解密的结果
*/
public static String decryptCBC(String data, String key) {
try {
byte[] encrypted1 = Base64.getDecoder().decode(data);
Cipher cipher = Cipher.getInstance(TRANSFORM_ALGORITHM[1]);
SecretKeySpec keyStr = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), “AES”);
IvParameterSpec ivStr = new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8));
cipher.init(Cipher.DECRYPT_MODE, keyStr, ivStr);
byte[] original = cipher.doFinal(encrypted1);
return new String(original, StandardCharsets.UTF_8);
} catch (Exception e) {
throw new IllegalArgumentException("解密失败: "+ e.getMessage());
}
}

public static void main(String[] args) throws Exception {
    String data = "hello Test symmetric encry";
    String keyStr = "7CC408B24462ABD1";
    String encryDataStr = encryptECB(data, keyStr);
    System.out.println("encryptECB = " + encryDataStr);
    System.out.println("decryptECB = " + decryptECB(encryDataStr, keyStr));
    encryDataStr = encryptCBC(data, keyStr);
    System.out.println("encryptCBC = " + encryDataStr);
    System.out.println("decryptCBC = " + decryptCBC(encryDataStr, keyStr));

}

}


### 4 前端(VUE)AES对称加解密(CBC模式)工具类


前端其他工程配置请看文章《java前后端参数和返回加密解密AES+CBC》


npm install crypto-js


#### 4.1 aseKeConfig.js AES+CBC配置



export const AES_KEY = ‘MTIzNDU2Nzg5MEFC’
export const AES_IV = ‘QUJDRURGMDk4NzY1’
// 参数是否进行加密设置,需要与后端配置保持一致
export const PARAM_ENCRYPT_ABLE = true
// 结果是否进行加密
export const RESULT_ENCRYPT_ABLE = true
// 需要排除的不进行加密的接口,正则匹配
export const EXCLUE_PATH = [‘.*/orc/.*’, ‘.*/fastdfs/.*’, ‘.*/eempFastdfs/.*’, ‘,.*/homepage/preview’, ‘.*oauth/getClickApplicationInfo’, ‘.*/common/defaultKaptcha.*’, ‘.*/autoKeywordKaTeX parse error: Undefined control sequence: \* at position 6: ', '.\̲*̲/getLayerCount’, ‘.*/getLayerInfoListByPageKaTeX parse error: Undefined control sequence: \* at position 8: ', '.\̲*̲/epUiImgSaveAnd…’, ‘/v7/weather’]


#### 4.2 加密解密工具类 aesSecretUtil.js



import CryptoJS from ‘crypto-js’
import {
AES_KEY,
AES_IV,
PARAM_ENCRYPT_ABLE,
EXCLUE_PATH,
RESULT_ENCRYPT_ABLE
}
from ‘…/config/aesKeyConfig.js’
const key = CryptoJS.enc.Utf8.parse(AES_KEY) // 16位
const iv = CryptoJS.enc.Utf8.parse(AES_IV)
const excluePath = EXCLUE_PATH
const paramEncryptAble = PARAM_ENCRYPT_ABLE
const resultEncryptAble = RESULT_ENCRYPT_ABLE
/**
*Description AES CBC BASE64加密解密
*@author
*@date 13:38 2022/3/31
*/
export default {
// aes加密
encrypt(word) {
let encrypted = ‘’
if (typeof word === ‘string’) {
const srcs = CryptoJS.enc.Utf8.parse(word)
encrypted = CryptoJS.AES.encrypt(srcs, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.ZeroPadding
})
} else if (typeof word === ‘object’) {
// 对象格式的转成json字符串
const data = JSON.stringify(word)
const srcs = CryptoJS.enc.Utf8.parse(data)
encrypted = CryptoJS.AES.encrypt(srcs, key, {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.ZeroPadding
})
}
return CryptoJS.enc.Base64.stringify(encrypted.ciphertext)
},
// aes解密
decrypt(word) {
if (word) {

总结

大厂面试问深度,小厂面试问广度,如果有同学想进大厂深造一定要有一个方向精通的惊艳到面试官,还要平时遇到问题后思考一下问题的本质,找方法解决是一个方面,看到问题本质是另一个方面。还有大家一定要有目标,我在很久之前就想着以后一定要去大厂,然后默默努力,每天看一些大佬们的文章,总是觉得只有再学深入一点才有机会,所以才有恒心一直学下去。

开源分享:【大厂前端面试题解析+核心总结学习笔记+真实项目实战+最新讲解视频】

  • 20
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值