Vue?路由傳參加密的示例代碼
Vue 路由傳參加密
首先,創(chuàng)建一個base64.js
const Base64 = { //加密 encode(str) { return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function toSolidBytes(match, p1) { return String.fromCharCode('0x' + p1); })); }, //解密 decode(str) { // Going backwards: from bytestream, to percent-encoding, to original string. return decodeURIComponent(atob(str).split('').map(function (c) { return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2); }).join('')); } } export default Base64
在main.js里面引用
import Base64 from './assets/js/base64.js' Vue.prototype.$Base64 = Base64;
使用頁面:
this.$router.push({ path: "/user/Recommend", query:{ info:this.$Base64.encode(XXXXX)//this.$Base64.encode(JSON.stringify(row)) } });
接受參數(shù)頁面:
let params = JSON.parse(this.$Base64.decode(this.$route.query.info))
Vue項目:路由跳轉(zhuǎn)時中文傳參被URL編碼,怎么解決?用js封裝Base64編碼解碼加密解密
1.在utils中封裝js方法,代碼如下:
var Base64 = { ? // private property _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", //密鑰(不能修改) // public method for encoding encode: function(input) { var output = ""; var chr1, chr2, chr3, enc1, enc2, enc3, enc4; var i = 0; input = Base64._utf8_encode(input); while (i < input.length) { chr1 = input.charCodeAt(i++); chr2 = input.charCodeAt(i++); chr3 = input.charCodeAt(i++); enc1 = chr1 >> 2; enc2 = ((chr1 & 3) << 4) | (chr2 >> 4); enc3 = ((chr2 & 15) << 2) | (chr3 >> 6); enc4 = chr3 & 63; if (isNaN(chr2)) { enc3 = enc4 = 64; } else if (isNaN(chr3)) { enc4 = 64; } output = output + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4); } return output; }, // public method for decoding decode: function(input) { var output = ""; var chr1, chr2, chr3; var enc1, enc2, enc3, enc4; var i = 0; input = input.replace(/[^A-Za-z0-9+/=]/g, ""); while (i < input.length) { enc1 = this._keyStr.indexOf(input.charAt(i++)); enc2 = this._keyStr.indexOf(input.charAt(i++)); enc3 = this._keyStr.indexOf(input.charAt(i++)); enc4 = this._keyStr.indexOf(input.charAt(i++)); chr1 = (enc1 << 2) | (enc2 >> 4); chr2 = ((enc2 & 15) << 4) | (enc3 >> 2); chr3 = ((enc3 & 3) << 6) | enc4; output = output + String.fromCharCode(chr1); if (enc3 != 64) { output = output + String.fromCharCode(chr2); } if (enc4 != 64) { output = output + String.fromCharCode(chr3); } } output = Base64._utf8_decode(output); return output; }, // private method for UTF-8 encoding _utf8_encode: function(string) { string = string.replace(/ /g, " "); var utftext = ""; for (var n = 0; n < string.length; n++) { var c = string.charCodeAt(n); if (c < 128) { utftext += String.fromCharCode(c); } else if ((c > 127) && (c < 2048)) { utftext += String.fromCharCode((c >> 6) | 192); utftext += String.fromCharCode((c & 63) | 128); } else { utftext += String.fromCharCode((c >> 12) | 224); utftext += String.fromCharCode(((c >> 6) & 63) | 128); utftext += String.fromCharCode((c & 63) | 128); } } return utftext; }, // private method for UTF-8 decoding _utf8_decode: function(utftext) { var string = ""; var i = 0; var c,c1,c2,c3 = 0; while (i < utftext.length) { c = utftext.charCodeAt(i); if (c < 128) { string += String.fromCharCode(c); i++; } else if ((c > 191) && (c < 224)) { c2 = utftext.charCodeAt(i + 1); string += String.fromCharCode(((c & 31) << 6) | (c2 & 63)); i += 2; } else { c2 = utftext.charCodeAt(i + 1); c3 = utftext.charCodeAt(i + 2); string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); i += 3; } } return string; } } ? export default Base64
2.在main.js中引入并掛載到vue原型對象上
//Base64加密 import Base64 from '@/utils/base64.js' Vue.prototype.$Base64 = Base64
3.在vue文件中使用
this.$Base64.encode(str) //加密 this.$Base64.decode(str) //解密
拓展:編碼解碼的其他方式
1.md5
常用于加密,可以暴力反向破解但是難度很大,一般來說,對md5加密的要求是現(xiàn)代算力最高的計算機需要花百年才能進(jìn)行解密
2.window對象自帶的編碼解碼
window.btoa(str) //對str進(jìn)行編碼
binary to ascii,用于將ascii字符串
或二進(jìn)制數(shù)據(jù)
轉(zhuǎn)換成一個base64編碼過的字符串
表示,即Base64的編碼過程
,常用于編碼字符串
。btoa不能編輯Unicode字符
window.atob(str) //對str進(jìn)行解碼
ascii to binary,用于解碼一個已經(jīng)被base-64編碼過的數(shù)據(jù)
,即解碼Base64編碼的字符串
。
3.Unicode字符編碼
encodeURI(str) //將URL地址編碼轉(zhuǎn)變成ASCII字符序列 decodeURI(str) //將ASCII字符序列解碼轉(zhuǎn)變成UTL地址 encodeURIComponent(str) //將中文字符編碼轉(zhuǎn)變成ASCII字符序列 decodeURIComponent(str) //將ASCII字符序列解碼轉(zhuǎn)變成中文字符
4.使用JS函數(shù)的escape()和unescape()
var escape1 =escape("我的名字是:");//編碼 console.log(escape1); var unescape1 = unescape(escape1); //解碼 console.log(unescape1);
到此這篇關(guān)于Vue 路由傳參加密的文章就介紹到這了,更多相關(guān)Vue 路由傳參加密內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue3之Mixin的使用方式(全局,局部,setup內(nèi)部使用)
這篇文章主要介紹了Vue3之Mixin的使用方式(全局,局部,setup內(nèi)部使用),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10axios+Vue實現(xiàn)上傳文件顯示進(jìn)度功能
這篇文章主要介紹了axios+Vue上傳文件顯示進(jìn)度效果,本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04詳解Vue.js之視圖和數(shù)據(jù)的雙向綁定(v-model)
本篇文章主要介紹了Vue.js之視圖和數(shù)據(jù)的雙向綁定(v-model),使用v-model指令,使得視圖和數(shù)據(jù)實現(xiàn)雙向綁定,有興趣的可以了解一下2017-06-06vue2.0 下拉框默認(rèn)標(biāo)題設(shè)置方法
今天小編就為大家分享一篇vue2.0 下拉框默認(rèn)標(biāo)題設(shè)置方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08