AES加密工具类

//后端
package
cn.keking.utils; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.nio.charset.StandardCharsets; import cn.keking.config.WebConfig; import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.binary.Base64; @Slf4j public class SecretUtils { /*** * key和iv值可以随机生成 */ private static String KEY; private static String IV; static { WebConfig.DesParams desParams = SpringContextUtil.getContext().getBean(WebConfig.DesParams.class); KEY = desParams.getKey(); IV = desParams.getIv(); } /*** * 加密 * @param data 要加密的数据 * @return encrypt */ public static String encrypt(String data){ return encrypt(data, KEY, IV); } /*** * param data 需要解密的数据 * 调用desEncrypt()方法 */ public static String decrypt(String data){ return decrypt(data, KEY, IV); } /** * 加密方法 * @param data 要加密的数据 * @param key 加密key * @param iv 加密iv * @return 加密的结果 */ private static String encrypt(String data, String key, String iv){ try { //"算法/模式/补码方式"NoPadding PkcsPadding Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); 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 keyspec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES"); IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8)); cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec); byte[] encrypted = cipher.doFinal(plaintext); return new Base64().encodeToString(encrypted); } catch (Exception e) { log.error(e.getMessage()); return null; } } /** * 解密方法 * @param data 要解密的数据 * @param key 解密key * @param iv 解密iv * @return 解密的结果 */ private static String decrypt(String data, String key, String iv){ try { data = data.replaceAll(" ", "+").replaceAll("\n", ""); byte[] encrypted1 = new Base64().decode(data); Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES"); IvParameterSpec ivSpec = new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8)); cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); byte[] original = cipher.doFinal(encrypted1); return new String(original, StandardCharsets.UTF_8).trim(); } catch (Exception e) { log.error(e.getMessage()); return null; } } }
//前端
<script src="js/crypto-js.js"></script> <script type="text/javascript"> /** * 对称加密 * @param {string} message 待加密的明文 * @param {string} key 加密所用的密钥 * @return {string} 返回密文字符串 */ // 默认的 KEY 与 iv 如果没有给 const KEY = CryptoJS.enc.Utf8.parse('T8lL1Nx8Z9X5MFEZ') const IV = CryptoJS.enc.Utf8.parse('yourivcodexxxxxx') function encrypt(message,key) { if (key==null||key==""){ key = KEY; } const srcs = CryptoJS.enc.Utf8.parse(message) const encrypted = CryptoJS.AES.encrypt(srcs, key, { iv: IV, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.ZeroPadding }) const res = CryptoJS.enc.Base64.stringify(encrypted.ciphertext); return res; } function decrypt(message, keyStr) { let key = KEY let iv = IV if (keyStr) { key = CryptoJS.enc.Utf8.parse(keyStr) } const base64 = CryptoJS.enc.Base64.parse(message) const src = CryptoJS.enc.Base64.stringify(base64) const decrypt = CryptoJS.AES.decrypt(src, key, { iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.ZeroPadding }) return CryptoJS.enc.Utf8.stringify(decrypt) } </script>

 

posted @ 2023-08-25 13:17  DreamCatt  阅读(153)  评论(0)    收藏  举报