fix btoa decoded error All In One
fix btoa decoded error All In One
base64 encode / decode
Uncaught DOMException: Failed to execute 'atob' on 'Window': The string
to be decoded
contains characters
outside of the Latin1
range.
Uncaught DOMException: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.
error
let s = `此密码只会在此设备上解锁您的 MetaMask 钱包。MetaMask 无法恢复此密码。`;
atob(s)
// Uncaught DOMException: Failed to execute 'atob' on 'Window': The string to be decoded contains characters outside of the Latin1 range.
btoa(s)
// Uncaught DOMException: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.
solution
btoa(unescape(encodeURIComponent(s)));
// '5q2k5a+G56CB5Y+q5Lya5Zyo5q2k6K6+5aSH5LiK6Kej6ZSB5oKo55qEIE1ldGFNYXNrIOmSseWMheOAgk1ldGFNYXNrIOaXoOazleaBouWkjeatpOWvhueggeOAgg=='
atob(unescape(encodeURIComponent(s)));
// Uncaught DOMException: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
demos
function encodeBase64(str = '') {
// string 转成 base64
return window.btoa(unescape(encodeURIComponent(str)));
}
function decodeBase64(str = '') {
// base64 反转 string (逆序嵌套)
return decodeURIComponent(escape(window.atob(str)));
}
const text = 'Vue3 script setup pass props';
const binary = encodeBase64(text);
console.log(`binary =`, binary);
const string = decodeBase64(binary);
console.log(`string =`, string);
btoa()
The btoa() method creates a Base64
-encoded ASCII
string from a binary
string (i.e., a string in which each character in the string is treated as a byte of binary data).
https://developer.mozilla.org/en-US/docs/Web/API/btoa
atob()
The atob() function decodes a string of data which has been encoded using Base64 encoding. You can use the btoa() method to encode and transmit data which may otherwise cause communication problems, then transmit it and use the atob() method to decode the data again. For example, you can encode, transmit, and decode control characters such as ASCII values 0 through 31.
https://developer.mozilla.org/en-US/docs/Web/API/atob
refs
window.atob lost spaces bug All In One
转换成 Base64 字符串后,丢失空格 ❌
https://www.cnblogs.com/xgqfrms/p/16034913.html
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17246070.html
未经授权禁止转载,违者必究!