js 摘要算法 base64加密解密 unescape()和escape()對字符串進行編碼 encodeURI()和decodeURI()編碼 encodeURIComponent()和decodeURIComponent()編碼
base64:雙向加密方式。
1.GitHub中下載base64
https://github.com/dankogai/js-base64
2.對應的HTML中調用
//加密
var encodeStr = Base64.encode("abcd1234");
console.log(encodeStr);
//解密
var decodeStr = Base64.decode(encodeStr);
console.log(decodeStr);
3.顯示結果
YWJjZDEyMzQ=
abcd1234
4.對于數據加密來講,base64只是達到了讓人看到數據,不知道數據的內容,而無法做到真正的安全,所以base64另外一個用途是用來進行數據的傳輸.
md5摘要算法:它是單向的。
1.原理:
MD5是一個安全的散列算法,有兩個特點:
1、輸入兩個不同的字符串不會得到相同的輸出值 。
2、無法從算法入手還原出MD5算法處理前的結果,即過程不可逆。
2.實用demo
$("#btn").click(function () {
var password = $("#pwd").val();
var passwd = md5(password);
console.log("====>", password)
console.log("$$$$$", passwd)
})
unescape()和escape()對字符串進行編碼
escape() 函數可對字符串進行編碼,這樣就可以在所有的計算機上讀取該字符串。
1.語法:
escape(string)
返回值:
已編碼的 string 的副本。其中某些字符被替換成了十六進制的轉義序列。
2.實例
document.write(escape("Visit W3School!") + "
")
document.write(escape("?!=()#%&"))
輸出:
Visit%20W3School%21
%3F%21%3D%28%29%23%25%26
3.說明
該方法不會對 ASCII 字母和數字進行編碼,也不會對下面這些 ASCII 標點符號進行編碼: * @ - _ + . / 。其他所有的字符 都會被轉義序列替換。
4.提示
提示:可以使用 unescape() 對 escape() 編碼的字符串進行解碼。
decodeURI() 和 decodeURIComponent()
1.定義
encodeURI() 函數可把字符串作為 URI 進行編碼。
2.語法
encodeURI(_URIstring_)
3.說明
該方法不會對 ASCII 字母和數字進行編碼,也不會對這些 ASCII 標點符號進行編碼: - _ . ! ~ * ' ( ) 。
該方法的目的是對 URI 進行完整的編碼,因此對以下在 URI 中具有特殊含義的 ASCII 標點符號,encodeURI() 函數是不會進行轉義的:;/?:@&=+$,#
如果 URI 組件中含有分隔符,比如 ? 和 #,則應當使用 encodeURIComponent() 方法分別對各組件進行編碼。
4.實例
document.write(`encodeURI("http://www.w3school.com.cn")`\+ "
")
document.write(`encodeURI("http://www.w3school.com.cn/My first/")`)
document.write(`encodeURI(",/?:@&=+$#")`)
輸出:
http://www.w3school.com.cn
http://www.w3school.com.cn/My%20first/
,/?:@&=+$#
encodeURIComponent()和decodeURIComponent()編碼
1.定義
encodeURIComponent() 函數可把字符串作為 URI 組件進行編碼。
2.語法
encodeURIComponent(URIstring)
3.說明
該方法不會對 ASCII 字母和數字進行編碼,也不會對這些 ASCII 標點符號進行編碼: - _ . ! ~ * ' ( ) 。
其他字符(比如 :;/?:@&=+$,# 這些用于分隔 URI 組件的標點符號),都是由一個或多個十六進制的轉義序列替換的。
請注意 encodeURIComponent() 函數 與 encodeURI() 函數的區別之處,前者假定它的參數是 URI 的一部分(比如協議、主機名、路徑或查詢字符串)。因此 encodeURIComponent() 函數將轉義用于分隔 URI 各個部分的標點符號。
4.實例
document.write(encodeURIComponent("http://www.w3school.com.cn"))
document.write("
")
document.write(encodeURIComponent("http://www.w3school.com.cn/p 1/"))
document.write("
")
document.write(encodeURIComponent(",/?:@&=+$#"))
輸出:
http%3A%2F%2Fwww.w3school.com.cn
http%3A%2F%2Fwww.w3school.com.cn%2Fp%201%2F
%2C%2F%3F%3A%40%26%3D%2B%24%23