(已使用)前端CryptoJS和后端crypto实现前后端密码加密解密功能

it2026-01-20  7

1.前端代码

// 1. vue package.json 引入crypto-js的依赖:"crypto-js": "^4.0.0", // 导入 import CryptoJS from 'crypto-js'; // 这两个必须是16位前后端一致 const key = CryptoJS.enc.Utf8.parse('FaceSunAweSome_K'); const iv = CryptoJS.enc.Utf8.parse('FaceSunAweSomeIV'); // 加密 const encrypt = (pass) => { const password = CryptoJS.enc.Utf8.parse(pass); return CryptoJS.AES.encrypt(password, key, { mode: CryptoJS.mode.CBC, iv: iv, padding: CryptoJS.pad.Pkcs7 }).toString(); }; // 解密 const decrypt = (pass) => { return CryptoJS.AES.decrypt(pass, key, { mode: CryptoJS.mode.CBC, iv: iv, padding: CryptoJS.pad.Pkcs7 }).toString(CryptoJS.enc.Utf8); }; export { encrypt, decrypt };

2.后端:

import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.lang3.StringUtils; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import java.nio.charset.StandardCharsets; /** * @author FaceSun * @title: AESUtil */ public class AESUtil { // 密钥 (需要前端和后端保持一致)十六位作为密钥 private static final String KEY = "FaceSunAweSome_K"; // 密钥偏移量 (需要前端和后端保持一致)十六位作为密钥偏移量 private static final String IV = "FaceSunAweSomeIV"; // 算法 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 encryptStr 待解密的 * @return 解密后的String * @throws Exception Exception */ public static String aesDecrypt(String encryptStr) throws Exception { if (StringUtils.isNotBlank(encryptStr)){ Cipher cipher = Cipher.getInstance(ALGORITHMSTR); byte[] temp = IV.getBytes(StandardCharsets.UTF_8); IvParameterSpec iv = new IvParameterSpec(temp); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(KEY.getBytes(), "AES"), iv); byte[] decryptBytes = cipher.doFinal(base64Decode(encryptStr)); return new String(decryptBytes); } return null; } /** * ==============================================AES加密 * @param password 待加密的 * @return 加密后的String * @throws Exception Exception */ public static String aesEncrypt(String password) throws Exception { if (StringUtils.isNotBlank(password)){ Cipher cipher = Cipher.getInstance(ALGORITHMSTR); byte[] temp = IV.getBytes(StandardCharsets.UTF_8); IvParameterSpec iv = new IvParameterSpec(temp); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(KEY.getBytes(), "AES"), iv); byte[] encryptBytes = cipher.doFinal(password.getBytes()); return base64Encoder(encryptBytes); } return null; } /** * base 64 加码 */ public static String base64Encoder(byte[] base64Code){ return new BASE64Encoder().encodeBuffer(base64Code); } }

 

最新回复(0)