最基础加密解密的btoa() atob()
//url传输数据时候 + / 符合转换成 - _ 原因是 +/ 会对url有影响
// Base64 加密函数
function base64Encode(str) {
let encoded = btoa(unescape(encodeURIComponent(str)));
return encoded.replace(/\+/g, '-').replace(/\//g, '_');
}
// Base64 解密函数
function base64Decode(encoded) {
let base64 = encoded.replace(/-/g, '+').replace(/_/g, '/');
return decodeURIComponent(escape(atob(base64)));
}
// 测试
let originalString = "Hello, World!";
let encodedString = base64Encode(originalString);
let decodedString = base64Decode(encodedString);
console.log("Original String:", originalString);
console.log("Encoded String:", encodedString);
console.log("Decoded String:", decodedString);
网友评论