demo代码地址:https://download.csdn.net/download/qq_33512843/10525116

一、后端java代码


package com.example.commons.util; import javax.crypto.Cipher; import
javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec;
importjava.security.Key; import java.security.spec.AlgorithmParameterSpec;
importjava.util.Base64; /** * 对称加密解密AES算法 * * @author zhaors */ public class
AESUtils {private static final String TRANSFORMATION = "AES/CBC/PKCS5Padding";
private static finalString ALGORITHM = "AES"; private static final String
CHARSET= "utf-8"; /** * 建议为16位或32位 */ private static final String KEY =
"A-16-Byte-keyValA-16-Byte-keyVal"; /** * 必须16位 *
初始化向量IV不可以为32位,否则异常java.security.InvalidAlgorithmParameterException: Wrong IV
length: must be 16 bytes long */ private static final String IV =
"A-16-Byte-String"; /** * 加密 * * @param context * @return */ public static
Stringencrypt(String context) { try { byte[] decode = context.getBytes(CHARSET);
byte[] bytes = createKeyAndIv(decode, Cipher.ENCRYPT_MODE); return Base64.
getEncoder().encodeToString(bytes); } catch (Exception e) { e.printStackTrace();
}return null; } /** * 解密 * * @param context * @return */ public static String
decrypt(String context) { try { Base64.Decoder decoder = Base64.getDecoder();
byte[] decode = decoder.decode(context); byte[] bytes = createKeyAndIv(decode,
Cipher.DECRYPT_MODE); return new String(bytes, CHARSET); } catch (Exception e)
{ e.printStackTrace(); } return null; } /** * 获取key & iv * * @param context *
@paramopmode * @return * @throws Exception */ public static byte[]
createKeyAndIv(byte[] context, int opmode) throws Exception { byte[] key = KEY
.getBytes(CHARSET); byte[] iv = IV.getBytes(CHARSET); return cipherFilter
(context, opmode, key, iv); } /** * 执行操作 * * @param context * @param opmode *
@paramkey * @param iv * @return * @throws Exception */ public static byte[]
cipherFilter(byte[] context, int opmode, byte[] key, byte[] iv) throws
Exception { Key secretKeySpec =new SecretKeySpec(key, ALGORITHM);
AlgorithmParameterSpec ivParameterSpec =new IvParameterSpec(iv); Cipher cipher
= Cipher.getInstance(TRANSFORMATION); cipher.init(opmode, secretKeySpec,
ivParameterSpec); return cipher.doFinal(context); } /** * 主方法测试 * * @param
args */ public static void main(String[] args) { String context = "zhaors";
System.out.println("元数据" + context); String encrypt = encrypt(context); System.
out.println("加密之后:" + encrypt); String decrypt = decrypt(encrypt); System.out
.println("解密之后:" + decrypt); } }

运行结果:




注意事项:

1.Java实现AES加密,异常java.security.InvalidKeyException: Illegal key size 的解决方案,

请参考:https://blog.csdn.net/wangjunjun2008/article/details/50847426

2.初始化向量(上文中的IV常量)不可以为32位,否则异常java.security.InvalidAlgorithmParameterException:
Wrong IV length: must be 16 bytes long

二、前端JS代码

crypto-js github地址:https://github.com/brix/crypto-js


1.导入所需要的js.


<script type="text/javascript" src="./crypto-js.js"></script>
2.核心js代码


var key = CryptoJS.enc.Utf8.parse("A-16-Byte-keyVal"); var iv =
CryptoJS.enc.Utf8.parse("A-16-Byte-String"); //aes加密 function encrypt(context) {
varencrypted = ''; if (typeof(context) == 'string') { }else if(typeof(context)
=='object'){ context = JSON.stringify(context); } var srcs =
CryptoJS.enc.Utf8.parse(context); encrypted = CryptoJS.AES.encrypt(srcs, key, {
iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); return
encrypted.toString(); } // aes解密 function decrypt(context) { var decrypt =
CryptoJS.AES.decrypt(context, key, { iv: iv, mode: CryptoJS.mode.CBC, padding:
CryptoJS.pad.Pkcs7 }); var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
returndecryptedStr.toString(); }


完整代码:


<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>crypto-js</title>
<scriptsrc="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<scripttype="text/javascript" src="./crypto-js.js"></script> <script type=
"text/javascript"> var key = CryptoJS.enc.Utf8.parse("A-16-Byte-keyVal"); var
iv = CryptoJS.enc.Utf8.parse("A-16-Byte-String"); //aes加密 function encrypt
(context) {var encrypted = ''; if (typeof(context) == 'string') { }else if(
typeof(context) == 'object'){ context = JSON.stringify(context); } var srcs =
CryptoJS.enc.Utf8.parse(context); encrypted = CryptoJS.AES.encrypt(srcs, key, {
iv: iv, mode: CryptoJS.mode.CBC, padding: CryptoJS.pad.Pkcs7 }); return
encrypted.toString(); } // aes解密 function decrypt(context) { var decrypt =
CryptoJS.AES.decrypt(context, key, { iv: iv, mode: CryptoJS.mode.CBC, padding:
CryptoJS.pad.Pkcs7 }); var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
returndecryptedStr.toString(); } function getAES(){ var jiamibefore = $(
"#jiamibefore").val(); var jiamiafter = encrypt(jiamibefore); $("#jiamiafter").
val(jiamiafter); } function getDAes(){ var jiemibefore = $("#jiemibefore").val()
; var jiemiafter = decrypt(jiemibefore); $("#jiemiafter").val(jiemiafter); }
</script></head> <body> <div> 加密前:<input type="text" id="jiamibefore"/> <input
type="button" name="" value="AES加密" onclick="getAES();"/> 加密后:<input type="text"
id="jiamiafter" readonly="readonly" /> <br> 解密前:<input type="text" id=
"jiemibefore"/> <input type="button" name="" value="AES解密" onclick="getDAes();"
/> 解密后:<input type="text" id="jiemiafter" readonly="readonly"/> </div> </body>
</html>
运行结果:






友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:[email protected]
QQ群:637538335
关注微信