AES加密及ajax的post方法封装
首先要引用ase的加密文件(见文章最后),然后在js中定义加密函数,在请求接口时对传递的参数进行加密。详细代码如下:
封装
//加密
function encrypt(word){
var key = CryptoJS.enc.Utf8.parse('1234');
var srcs = CryptoJS.enc.Utf8.parse(word);
if(typeof word == 'object'){
// 如果传递的参数为json对象,要将对象先转为字符串
srcs = JSON.stringify(word);
}
var encrypted = CryptoJS.AES.encrypt(srcs, key, {
mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
return encrypted.toString();
}
//由于该方法中未添加解密功能,所以解密函数未对数据进行处理
//解密
function decrypt(word){
var key = CryptoJS.enc.Utf8.parse("1234"); //‘1234’为约定好的加密key,由前后端约定
var decrypt = CryptoJS.AES.decrypt(word, key, {
mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}
//ajax请求
function RTS(url,data,sucfun,errfun){
let mes = encrypt(data); //加密
let baseUrl = '默认的前缀路径'+url;
$.ajax({
//此处仅对post方法进行封装,也可以用同样的方法对get方法进行封装
type:"post",
url:baseUrl,
data:{
data:mes},
dataType:"json",
async:true,
success:function(res){
sucfun(res);
},
error:function(err){
errfun(err);
}
});
}
使用
let data2 = {
occupForm:""};
RTS('occupForm/listOccup',data2,function(res){
//res为调用成功后的返回数据
console.log(res)
},function(err){
console.log('链接错误');
});
aes.min.js 文件
!function(t,n){
"object"==typeof exports?module.exports=exports=n():"function"==typeof define&&define.amd?define([],n):t.CryptoJS=n()}(this,function(){
var t=t||function(t,n){
var i=Object.create||function(){
function t(){
}return function(n){
var i;return t.prototype=n,i=new t,t.prototype=null,i}}(),e={
},r=e.lib={
},o=r.Base=function(){
return{
extend:function(t){
var n=i(this);return t&&n.mixIn(t),n.hasOwnProperty("init")&&this.init!==n.init||(n.init=function(){
n.$super.init.apply(this,arguments)}),n.init.prototype=n,n.$super=this,n},create:function(){
var t=this.extend();return t.init.apply(t,arguments),t},init:function(){
},mixIn:function(t){
for(var n in t)t.hasOwnProperty(n)&&(this[n]=t[n]);t.hasOwnProperty("toString")&&(this.toString=t.toString)},clone:function(){
return this.init.prototype.extend(this)}}}(),s=r.WordArray=o.extend({
init:function(t,i){
t=this.words=t||[],i!=n?this.sigBytes=i:this.sigBytes=4*t.length},toString:function(t){
return(t||c).stringify(this)},concat:function(t){
var n=this.words,i=t.words,e=this.sigBytes,r=t.sigBytes;if(this.clamp(),e%4)for(var o=0;o<r;o++){
var s=i[o>>>2]>>>24-o%4*8&255;n[e+o>>>2]|=s<<24-(e+o)%4*8}else for(var o=0;o<r;o+=4)n[e+o>>>2]=i[o>>>2];return this.sigBytes+=r,this},clamp:function(){
var n=this.words,i=this.sigBytes;n[i>>>2]&=4294967295<<32-i%4*8,n.length=t.ceil(i/4)},clone:function(){
var t=o.clone.call(this);return t.words=this.words.slice(0),t},random:function(n){
for(var i,e=[],r=function(n){
var n=n,i=987654321,e=4294967295;return function(){
i=36969*(65535&i)+(i>>16)&e,n=18e3*(65535&n)+(n>>16)&e;var r=(i<<16)+n&e;return r/=4294967296,r+=.5,r*(t.random()>.5?1:-1)}},o=0;o<n;o+=4){
var a=r(4294967296*(i||t.random()));i=987654071*a(),e.push(4294967296*a()|0)}return new s.init(e,n)}}),a=e.enc={
},c=a.Hex={
stringify:function(t){
for(var n=t.words,i=t.sigBytes,e=[],r=0;r<i;r++){
var o=n[r>>>2]>>>24-r%4*8&255;e.push((o>>>4).toString(16)),e.push((15&o).toString(16))}return e.join("")},parse:function(t){
for(var n=t.length,i=[],e=0;e<n;e+=2)i[e>>>3]|=parseInt(t.substr(e,2),16)<<24-e%8*4;return new s.init(i,n/2)}},u=a.Latin1={
stringify:function(t){
for(var n=t.words,i=t.sigBytes,e=[],r=0;r<i;r++){