pc端ajax的header请求,CryptoJS加密

js部分:

var myAes={

Encrypt:function(word, keyStr){//加密

var srcs = CryptoJS.enc.Utf8.parse(word);

var key  = CryptoJS.enc.Utf8.parse(keyStr);

var encrypted = CryptoJS.AES.encrypt(srcs, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});

return encrypted.toString();

},

Decrypt:function(word, keyStr){//解密

//var srcs = CryptoJS.enc.Utf8.parse(word);

var key  = CryptoJS.enc.Utf8.parse(keyStr);

var decrypt = CryptoJS.AES.decrypt(word, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});

return decrypt.toString(CryptoJS.enc.Utf8);

},

getAesKey:function(len) {

len = len || 32;

var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';

var maxPos = $chars.length;

var keyStr = '';

for(var i = 0; i < len; i++) {

keyStr += $chars.charAt(Math.floor(Math.random() * maxPos));

}

return keyStr;

},

getNewSecret:function(sstr){

var newEncrypt = new JSEncrypt();

newEncrypt.setPublicKey(publicKeySer);

newSecret = newEncrypt.encrypt(sstr);

return newSecret;

},

returnSign:function(nowTime){

var sign='',data=[],system='systempc', mac='mac';

data.push(system);

data.push(mac);

data=data.sort() //sort字母排序

for(var i=0;i

if(i==(data.length-1)){sign += data[i];}else{sign += data[i]+'_';};

}

sign+=salt;

sign+=publicKeyUser;

sign+=nowTime;

return sign;

}

}

function newAjax(){

var nowTime =new Date().getTime();

    var newAesKey=myAes.getAesKey(16);

    var newSecret=myAes.getNewSecret(newAesKey);

    var newSign=myAes.Encrypt(myAes.returnSign(nowTime),newAesKey);

    var token=GLOBAL_G.readCookie('token');

  var ajaxData = {

url:arguments.length && arguments[0].url ? arguments[0].url : "",

data:arguments.length && arguments[0].data ? arguments[0].data : null,

beforeSend:arguments.length && arguments[0].beforeSend ? arguments[0].beforeSend : function(){},

success:arguments.length && arguments[0].success ? arguments[0].success : function(msg){},

error:arguments.length && arguments[0].error ? arguments[0].error : function(){}

  }

  var flag=ajaxData.beforeSend();

$.ajax({ //其他

url: ajaxData.url,

method: "POST",

data: ajaxData.data,

dataType: "json",

headers: {

    "sign":newSign,

"system":'pc',

"mac":'',

"secret":newSecret,

"time":nowTime

},

beforeSend:ajaxData.beforeSend,

success:ajaxData.success,

error:ajaxData.error

})

}

你可能感兴趣的:(pc端ajax的header请求,CryptoJS加密)