base64加密调整适配

This commit is contained in:
yy2205 2025-05-22 17:36:54 +08:00
parent ac2c457a8e
commit f8204dc2d8

View File

@ -5,8 +5,15 @@
 */
export const encodeBase64 = (str) => {
try {
// 使用 btoa 进行 base64 编码
return btoa(str);
// 使用 UTF-8 编码处理字符串
const utf8Encoder = new TextEncoder();
const bytes = utf8Encoder.encode(str);
// 将字节数组转换为 base64
let binary = '';
for (let i = 0; i < bytes.length; i++) {
binary += String.fromCharCode(bytes[i]);
}
return btoa(binary);
} catch (error) {
console.error('Base64 编码失败:', error);
return '';
@ -20,8 +27,15 @@ export const encodeBase64 = (str) => {
 */
export const decodeBase64 = (str) => {
try {
// 使用 atob 进行 base64 解码
return atob(str);
// 解码 base64
const binary = atob(str);
// 将二进制字符串转换为字节数组
const bytes = new Uint8Array(binary.length);
for (let i = 0; i < binary.length; i++) {
bytes[i] = binary.charCodeAt(i);
}
// 使用 UTF-8 解码
return new TextDecoder().decode(bytes);
} catch (error) {
console.error('Base64 解码失败:', error);
return '';