base64与Uin8Array互转实现
发布日期:2021-05-10 04:47:45 浏览次数:24 分类:精选文章

本文共 3692 字,大约阅读时间需要 12 分钟。

Base64是一种将二进制数据转换为可传输的ASCII 字符的方法,主要用于解决不同平台和协议之间的数据传输问题。Base64并不像人们常说的加密技术,它并没有加密数据的安全作用,但它在数据传输中非常有用。

Base64的核心思想是将3字节的二进制数据(24位)转换为4个Base64字符(32位),这样可以确保数据在传输过程中不会因为平台或系统的差异而产生问题。虽然用JavaScript的 toString方法可以实现Base64转换,但了解其原理仍然很重要。

下面是一个用Node.js实现的Base64转换例子:

const fs = require('fs');
const base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
class Base64Helper {
static bufToBase64(buf) {
let encoded_str = '';
const mod = buf.length % 3;
const sum = Math.floor(buf.length / 3);
for (let i = 0; i < sum * 3; i += 3) {
const byte1 = buf[i] >> 2;
const byte2 = ((buf[i] & 0x03) << 4) | ((buf[i + 1] >> 4));
const byte3 = ((buf[i + 1] & 0x0F) << 2) | buf[i + 2];
const byte4 = buf[i + 2] & 0x3F;
encoded_str += base[byte1] + base[byte2] + base[byte3] + base[byte4];
}
if (mod === 1) {
encoded_str += '====';
} else if (mod === 2) {
encoded_str += '==';
}
return encoded_str;
}
static base64ToBuf(base64Data) {
let equalCount = base64Data.match(/=/g) || [];
base64Data = base64Data.replace(/=/g, '');
const len = base64Data.length;
const mod = len % 4;
const sum = Math.floor(len / 4);
const buf = new Uint8Array(sum * 3 + (equalCount.length ? mod === 1 ? 2 : equalCount.length > 1 ? 1 : 0 : 0));
for (let i = 0; i < sum * 4; i += 4) {
const idx0 = base.indexOf(base64Data[i]);
const idx1 = base.indexOf(base64Data[i + 1]);
const idx2 = base.indexOf(base64Data[i + 2]);
const idx3 = base.indexOf(base64Data[i + 3]);
if (idx0 === -1 || idx1 === -1 || idx2 === -1 || idx3 === -1) continue;
buf[i / 4] = (idx0 & 0x00 ? (idx0 > 0 ? (idx0 < 2 ? idx0 : idx0 - 2) : 0) : (idx0 >= 2 ? idx0 - 2 : idx0))
| ((idx1 & 0x00 ? (idx1 > 0 ? (idx1 < 4 ? idx1 : idx1 - 4) : 0) : (idx1 >= 4 ? idx1 - 4 : idx1))
| ((idx2 & 0x00 ? (idx2 > 0 ? (idx2 < 6 ? idx2 : idx2 - 6) : 0) : (idx2 >= 6 ? idx2 - 6 : idx2))
| (idx3 & 0x3F);
}
if (equalCount.length && equalCount[0]) {
const idx0 = base.indexOf(base64Data[base64Data.length - (equalCount.length > 1 ? 2 : 3)]);
const idx1 = base.indexOf(base64Data[base64Data.length - (equalCount.length > 1 ? 1 : 2)]);
buf[buf.length - equalCount.length] = (idx0 & 0x00 ? (idx0 > 0 ? (idx0 < 2 ? idx0 : idx0 - 2) : 0) : (idx0 >= 2 ? idx0 - 2 : idx0))
| ((idx1 & 0x00 ? (idx1 > 0 ? (idx1 < 4 ? idx1 : idx1 - 4) : 0) : (idx1 >= 4 ? idx1 - 4 : idx1));
}
if (equalCount.length && equalCount.length > 0) {
const idx0 = base.indexOf(base64Data[base64Data.length - 2]);
const idx1 = base.indexOf(base64Data[base64Data.length - 1]);
buf[buf.length - 1] |= (idx0 & 0x00 ? (idx0 > 0 ? (idx0 < 2 ? idx0 : idx0 - 2) : 0) : (idx0 >= 2 ? idx0 - 2 : idx0))
| (idx1 & 0x00 ? (idx1 > 0 ? (idx1 < 4 ? idx1 : idx1 - 4) : 0) : (idx1 >= 4 ? idx1 - 4 : idx1));
}
return Buffer.from(buf, 'utf8');
}
}
const buf = fs.readFileSync('test.txt', 'utf8').buffer;
const originalStr = "这串文字会转成base64,然后再转成buffer,转成string显示出来。abcde";
const toStringRes = buf.toString('base64');
const base64HeplerRes = Base64Helper.bufToBase64(buf);
console.log('toString结果', toStringRes);
console.log('base64Hepler结果', base64HeplerRes);
console.log('还原文字', Base64Helper.base64ToBuf(base64HeplerRes).toString());

Base64并不是加密技术,它的主要作用是确保数据在传输过程中保持完整性。虽然 Base64 转换可以通过 toString 方法实现,但了解其原始逻辑对理解数据传输过程非常有帮助。

上一篇:debian网络和源设置(新手)
下一篇:爬取体彩历史数据过程

发表评论

最新留言

关注你微信了!
[***.104.42.241]2025年05月02日 17时19分05秒