1、Base64 编码
Base64 不是一种加密算法,而是一种编码方法,用于将二进制数据转换为基于 64 个可打印字符的文本字符串。它常用于在 URL、Cookie、网页中传输少量二进制数据,以及内嵌小图片以减少服务器访问次数。
Base64 编码简单,对性能影响不大,但会增加数据体积约 1/3,且无法缓存。
function toBase64(str) {
return btoa(unescape(encodeURIComponent(str)));
}
function fromBase64(str) {
return decodeURIComponent(escape(atob(str)));
}
// 使用示例
const originalText = "Hello, World!";
const encodedText = toBase64(originalText);
console.log(encodedText); // "SGVsbG8sIFdvcmxkIQ=="
const decodedText = fromBase64(encodedText);
console.log(decodedText); // "Hello, World!"
2、MD5 加密
MD5 是一种广泛使用的哈希函数,产生 128 位(16 字节)的哈希值。虽然 MD5 的计算速度快且效率高,但存在碰撞风险,且安全性较低,因此不推荐直接用于密码存储。
import CryptoJS from 'crypto-js';
function md5Encrypt(str) {
return CryptoJS.MD5(str).toString();
}
// 使用示例
originalText = ;
encryptedText = (originalText);
.(encryptedText);


