Sometimes it is necessary to use base64 encoding for encrypted transmission of strings during development. We can use base64
the encoding , which are btoa
encryption and atob
decryption ;
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)
But in the encrypted string, if there is Chinese, an error will be reported, and the error is as follows:
To solve this error, we can encodeURIComponent
escape , and then use decodeURIComponent
to get the string when decoding;
If you are interested, you can see the specific usage method here ;