base64 encoding and decoding

Sometimes it is necessary to use base64 encoding for encrypted transmission of strings during development. We can use base64the encoding , which are btoaencryption and atobdecryption ;
btoa()the method can encode a binary string into a Base64-encoded ASCII string , and then use atob()the method to decode the data, as follows:

let data = 'test btoa & atob'
let btoaStr = btoa(data)
console.log(btoaStr)

let atobStr = atob(btoaStr)
console.log(atobStr)

insert image description here

But in the encrypted string, if there is Chinese, an error will be reported, and the error is as follows:

insert image description here

To solve this error, we can encodeURIComponentescape , and then use decodeURIComponentto get the string when decoding;

insert image description here

If you are interested, you can see the specific usage method here ;

Guess you like

Origin blog.csdn.net/Ljwen_/article/details/127007614