var XHCJ;
(function(XHCJ) {
var HandShake = (function() {
function HandShake() {
}
HandShake.val = function(x) {
var result = 5 * Math.pow(x, 4) + 4 * Math.pow(x, 3) + 3 * Math.pow(x, 2) + 2 * Math.pow(x, 1) + "";
return CryptoJS.MD5(result).toString().toUpperCase();
};
HandShake.execute = function(times, value, successfn, errorfn) {
// console.info(XHCJ.HandShake.val(value));
XHCJ.Ajax.post("/api/v1/client/handshake", {
value: XHCJ.HandShake.val(value),
times: times
}, successfn, errorfn);
};
return HandShake;
}());
XHCJ.HandShake = HandShake;
})(XHCJ || (XHCJ = {}));
XHCJ;
(function(XHCJ) {
var Ajax = (function() {
function Ajax() {
}
Ajax.host = "握手地址";
// Ajax.host=localStorage.getItem('url');
console.log('通讯握手请求的地址:'+Ajax.host);
Ajax.post = function(route, data, successfn, errorfn) {
mui.ajax(XHCJ.Ajax.host + route, {
data: JSON.stringify(data),
xhrFields: {
withCredentials: true
},
crossDomain: true,
dataType: 'json', //服务器返回json格式数据
type: 'POST', //HTTP请求类型
timeout: 10000, //超时时间设置为10秒;
headers: {
'Content-Type': 'application/json'
},
success: function(data) {
if(parseInt(data.status.code) == 0) {
successfn(data);
} else {
if(errorfn) {
errorfn()
} else {
alert(data.status.message)
};
plus.nativeUI.closeWaiting();
}
},
error: function(xhr, type, errorThrown) {
alert("通讯异常!");
plus.nativeUI.closeWaiting();
}
});
};
return Ajax;
}());
XHCJ.Ajax = Ajax;
})(XHCJ || (XHCJ = {}));
XHCJ.HandShake.execute(1, 11, function() {
XHCJ.HandShake.execute(2, 13, function() {
XHCJ.HandShake.execute(3, 17, function(data) {
//
var _sk=data.sk;
var _sn="xinhua";
console.log("_sk"+"-------"+_sk);
localStorage.setItem("secret_key",_sk);
// var i=localStorage.getItem("secret_key");
// ;
});
});
var secret_key = ''
var genSecretKey = function() {
secret_key = localStorage.getItem("secret_key");
console.log(secret_key)
if(!secret_key)
throw new Error("localStorage不存在secret_key");
return CryptoJS.enc.Utf8.parse(secret_key);
};
genSecretKey();
console.log(secret_key);
function Encrypt(word){
console.log(typeof(secret_key))
var key = CryptoJS.enc.Utf8.parse(secret_key);
console.log(key)
var srcs = CryptoJS.enc.Utf8.parse(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(secret_key);
var decrypt = CryptoJS.AES.decrypt(word, key, {mode:CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7});
return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}
console.log(Encrypt("我爱你"));
console.log(Decrypt(Encrypt("我爱你")))
过年后最后一发 AES对称加密
最新推荐文章于 2022-12-30 11:24:54 发布