// 使用 JavaScript 进行 Base64 加密和解密的示例
// Base64 加密
function encodeBase64(input) {
// 将输入字符串转换为 Base64 编码的字符串
return btoa(input);
}
// Base64 解密
function decodeBase64(input) {
// 将 Base64 编码的字符串解码为原始字符串
return atob(input);
}
// 示例用法
const originalString = "Hello, World!";
const encodedString = encodeBase64(originalString);
console.log("Encoded String:", encodedString); // 输出加密后的字符串
const decodedString = decodeBase64(encodedString);
console.log("Decoded String:", decodedString); // 输出解密后的字符串
btoa()
是浏览器内置的函数,用于将字符串编码为 Base64 格式。atob()
是浏览器内置的函数,用于将 Base64 编码的字符串解码回原始字符串。encodeBase64
函数封装了 btoa
,用于加密输入的字符串。decodeBase64
函数封装了 atob
,用于解密 Base64 编码的字符串。上一篇:js 获取用户ip
下一篇:js worker 多线程
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站