应用场景:web页面间数据传递时出现中文乱码问题
一、加密btoa,encodeURIComponent
1、encodeURIComponent用于将字符转换为URIString类型,因为btoa加密字符的时候,将ascii字符串或二进制数据转换成一个base64编码过的字符串但不能直接作用于Unicode字符。
直接使用Unicode字符加密,会提示无效字符错误
>btoa('中国chinese')
InvalidCharacterError
配合encodeURIComponent使用
>btoa(encodeURIComponent('中国chinese'))
<"JUU0JUI4JUFEJUU1JTlCJUJEY2hpbmVzZQ=="
二、解密atob、decodeURIComponent
>atob('JUU0JUI4JUFEJUU1JTlCJUJEY2hpbmVzZQ==')
<"%E4%B8%AD%E5%9B%BDchinese"
>decodeURI('%E4%B8%AD%E5%9B%BDchinese')
<"中国chinese"