Native js encryption and decryption.

Native js encryption and decryption use atob and btoa methods.

1. btoa encoding

window.btoa(stringToEncode)

2.atob decoding

window.atob(encodedData);

(1) btoab refers to ordinary characters, a refers to Base64 characters, therefore, btoa means ordinary characters toBase64 characters, that is, Base64 encoding.

(2) atoba refers to Base64, and b refers to ordinary characters. Therefore, atob means Base64 characters to ordinary characters, that is, Base64 decoding.

for example:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<script>
    var s="abc121321dsadsa";
    console.log(window.btoa(s));//YWJjMTIxMzIxZHNhZHNh
    console.log(window.atob('YWJjMTIxMzIxZHNhZHNh'));

    var str="我是中文";
    console.log(window.escape(str));//%u6211%u662F%u4E2D%u6587
    console.log(window.unescape('%u6211%u662F%u4E2D%u6587'));

    var mm="123abc我是中文";
    console.log(window.encodeURIComponent(mm));//123abc%E6%88%91%E6%98%AF%E4%B8%AD%E6%96%87
    console.log(window.decodeURIComponent('123abc%E6%88%91%E6%98%AF%E4%B8%AD%E6%96%87'));

    var a={
    
    
        name:"张三你",
        age:20
    }
    console.log(btoa(a));//W29iamVjdCBPYmplY3Rd
    console.log(atob('W29iamVjdCBPYmplY3Rd'));

    console.log(escape(a));
    console.log(unescape("%5Bobject%20Object%5D"));

    console.log(encodeURIComponent(JSON.stringify(a)));
    console.log(decodeURIComponent('%7B%22name%22%3A%22%E5%BC%A0%E4%B8%89%E4%BD%A0%22%2C%22age%22%3A20%7D'));
</script>
</body>
</html>

おすすめ

転載: blog.csdn.net/weixin_46953330/article/details/116535001