前端:
js:https://files.cnblogs.com/files/blogs/681375/gitcrazy-crypto-js-master.zip下载
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title></title>
<script type="text/javascript" src="static/js/crypto-js.js"></script>
<script>
function AesTo(text){
var key = CryptoJS.enc.Utf8.parse("PB1234567890PB");
var iv = CryptoJS.enc.Utf8.parse("PB1234567890PB");
var srcs = CryptoJS.enc.Utf8.parse(encodeURI(text));
var context = CryptoJS.AES.encrypt(srcs,key,{
iv:iv,
mode:CryptoJS.mode.CBC,
padding:CryptoJS.pad.Pkcs7
});
$.ajax({
url:url,
type:"POST",
data:{
context:context.toString()
},
success:function(res){
console.log(res);
}
})
}
</script>
</head>
<body>
</body>
</html>
后端:
package com.cn.aes.util
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import com.alibaba.druid.util.StringUtils;
public class aesUtil{
//base64
private byte[] base64Decode(String base64Code) throws Exception{
return StringUtils.isEmpty(base64Code) ? null:new BASE64Decoder().decodeBuffer(base64Code);
}
//AES 解码
public String aesDecrypt(String context,String key,String iv) throws Exception{
return StringUtils.isEmpty(context) ? null : java.net.URLDecoder.decode(aesDecryptByBytes(base64Decode(context),key.getBytes(),iv.getBytes()),"UTF-8");
}
public String aesDecryptByBytes(byte[] context,byte[] key,byte[] iv) throws Exception{
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE,new SecretKeySpec(key,"AES"),new IvParameterSpec(iv));
byte[] result cipher.doFinal(context);
return new String(result);
}
}
------------------------------
后端通过方法调用 String contextNew = aesUtil.aesDecrypt(context,"PB1234567890PB","PB1234567890PB");