一、内置函数
内置函数:btoa
let value = 'hello';
console.log(btoa(value));
内置函数:atob
let value = 'aGVsbG8=';
console.log(atob(value));
二、借助第三方库实现,例如CryptoJS
const CryptoJS = require("crypto-js");
let value = "hello";
let trans = CryptoJS.enc.Utf8.parse(value);
let encrypted = CryptoJS.enc.Base64.stringify(trans);
console.log(encrypted)
//自己编写一套Base64编码和解码算法
function Base64(){
this.encode = function(val){
//编码逻辑
return val
}
this.decode = function(val){
//解码逻辑
return val
}
}
encrypt = new Base64();
console.log(encrypt.encode("encode"))