base64和hex
vue项目
//如下代码写为vue的全局函数,便于调用
var iv = crypto.enc.Utf8.parse('1234567887654321');
//ECB模式不需要iv
// //aes base64,url传递+的时候全部变成空格了,可以做全局替换,+替换成%2B
// //加密
// Vue.prototype.myEncode = function(word, keyStr){
// keyStr = keyStr ? keyStr : 'abcdefgabcdefg12';
// var key = crypto.enc.Utf8.parse(keyStr);
// //var srcs = crypto.enc.Utf8.parse(word);
// var srcs = word
// var encrypted = crypto.AES.encrypt(srcs, key, { mode: crypto.mode.ECB, padding: crypto.pad.Pkcs7 });
// var hexStr = encrypted.ciphertext.toString().toUpperCase();
// console.log('hexStr : ' + hexStr);
// //暂存hexStr,用以转base64
// var oldHexStr = crypto.enc.Hex.parse(hexStr);
// // 将密文转为base64的字符串
// var base64Str = crypto.enc.Base64.stringify(oldHexStr);
// console.log('base64Str : ' + base64Str);
// //自行决定返回 base64Str 或 hexStr(解密时有所不同)
// return base64Str;
// }
// //解密
// Vue.prototype.myDecode = function(word, keyStr){
// keyStr = keyStr ? keyStr : 'abcdefgabcdefg12';
// var key = crypto.enc.Utf8.parse(keyStr);
// console.log('word : ' + word);
// //处理加密返回的base64Str
// var srcs = word;
// //若上面加密返回的是hexStr,需要执行下面注释掉的,转换为可处理的密文,再进行解密
// //var encryptedHexStr = crypto.enc.Hex.parse(word);
// // var srcs = crypto.enc.Base64.stringify(encryptedHexStr);
// var decrypt = crypto.AES.decrypt(srcs, key, { mode: crypto.mode.ECB, padding: crypto.pad.Pkcs7 });
// var decryptedStr = decrypt.toString(crypto.enc.Utf8);
// var value = decryptedStr.toString();
// console.log('value->'+value);
// return value;
// }
//aes hexstr加密,hexstr中不会又空格
//加密
Vue.prototype.myEncode = function(word, keyStr){
keyStr = keyStr ? keyStr : 'abcdefgabcdefg12';
var key = crypto.enc.Utf8.parse(keyStr);//Latin1 w8m31+Yy/Nw6thPsMpO5fg==
var srcs = crypto.enc.Utf8.parse(word);
var encrypted = crypto.AES.encrypt(srcs, key, {mode:crypto.mode.ECB,padding: crypto.pad.Pkcs7});
return encrypted.ciphertext.toString().toUpperCase();
}
//解密
Vue.prototype.myDecode = function(word, keyStr){
keyStr = keyStr ? keyStr : 'abcdefgabcdefg12';
var key = crypto.enc.Utf8.parse(keyStr);//Latin1 w8m31+Yy/Nw6thPsMpO5fg==
var encryptedHexStr = crypto.enc.Hex.parse(word);
var srcs = crypto.enc.Base64.stringify(encryptedHexStr);
var decrypt = crypto.AES.decrypt(srcs, key, {mode:crypto.mode.ECB,padding: crypto.pad.Pkcs7});
decryptedStr = decrypt.toString(crypto.enc.Utf8);
return decryptedStr.toString()
}
//其他可能会用到的函数
//时间戳转可视化之间字符串
Vue.prototype.timestampToTimeString = function(timestamp){
var time = new Date(timestamp)
var y = time.getFullYear();
var m = time.getMonth()+1;
var d = time.getDate();
var h = time.getHours();
var mm = time.getMinutes();
var s = time.getSeconds();
return y + '/' + m + '/' + d + ' ' + h + ':' + mm + ':' + s
}
//字符串是否包含,不区分大小写,通过reg参数设置是否区分大小写
Vue.prototype.strFind = function(str, subStr){
var reg = eval("/"+subStr+"/ig");
var ret = reg.test(str);
//console.log(str + ' => ' + subStr + ' = ' + ret)
return ret
}
//位掩码判断是否mask中是否包含submask,最大值maxBit默认1024
Vue.prototype.maskMatch = function(mask, subMask, maxBit){
if (maxBit) {
}else{
maxBit = 1024
}
if ('string' == typeof mask) {
mask = parseInt(mask)
}
if ('string' == typeof subMask) {
subMask = parseInt(subMask)
}
var bits = []
var ret = false
var oldMask = mask
for (var i = maxBit; i >= 1; i /= 2) {
if (mask > i) {
bits.push[true]
mask -= i
if (i == subMask) {
ret = true
}
}else{
bits.push[false]
}
}
//console.log(oldMask + ' : ' + subMask + ' => ' + ret)
return ret
}