base64
防止不可见的控制字符(0~31以及127),比如回车,换行等在编码是发生错误,产生了base编码
用64个可打印字符来表示二进制数据
为什么是64
3字节转化为4字节
编码表
范围:[A-Za-z0-9+/]+
- 将待转换的字符串每三个字节分为一组,每个字节占8bit,那么共有24个二进制位。
- 将上面的24个二进制位每6个一组,共分为4组。
- 在每组前面添加两个0,每组由6个变为8个二进制位,总共32个二进制位,即四个字节。
- 根据Base64编码对照表(见下图)获得对应的值。
0 A 17 R 34 i 51 z
1 B 18 S 35 j 52 0
2 C 19 T 36 k 53 1
3 D 20 U 37 l 54 2
4 E 21 V 38 m 55 3
5 F 22 W 39 n 56 4
6 G 23 X 40 o 57 5
7 H 24 Y 41 p 58 6
8 I 25 Z 42 q 59 7
9 J 26 a 43 r 60 8
10 K 27 b 44 s 61 9
11 L 28 c 45 t 62 +
12 M 29 d 46 u 63 /
13 N 30 e 47 v
14 O 31 f 48 w
15 P 32 g 49 x
16 Q 33 h 50 y
举例
文本 | M | a | n |
---|---|---|---|
ascii | 77 | 97 | 110 |
二进制位 | 01001101 | 0110001 | 01101110 |
划分为4字节 | 010011 | 010110 | 000101 | 101110 |
---|---|---|---|---|
3个字节24位,划分为4个字节,32位,不够8位向前补0,6bit补为8bit
补0 | 00010011 | 00010110 | 00000101 | 00101110 |
---|---|---|---|---|
索引 | 19 | 22 | 5 | 46 |
---|---|---|---|---|
base64 | T | W | F | U |
= 的出现
如果不够三字节,用0填到三字节,同时补充了多少个字节的0,末尾就添加就几个=
js实现base64(参考)
js-base的github:
https://github.com/mathiasbynens/base64
//加密、解密算法封装:
function Base64() {
// private property
let _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
// public method for encoding
this.encode = function(input) {
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
input = _checkChar(input)
input = _utf8_encode(input);
// console.log(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output +
_keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
_keyStr.charAt(enc3) + _keyStr.charAt(enc4);
};
output = output.replace('+','%2B');// 这个后台识别不了+,所以替换了一下,这个也可以不用替换,视实际情况定
return output;
};
// public method for decoding
this.decode = function(input) {
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
if (typeof(input) == String) {
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
}else{
return input
}
while (i < input.length) {
enc1 = _keyStr.indexOf(input.charAt(i++));
enc2 = _keyStr.indexOf(input.charAt(i++));
enc3 = _keyStr.indexOf(input.charAt(i++));
enc4 = _keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}
}
output = _utf8_decode(output);
output = _returnJson(output);
return output;
}
// private method for UTF-8 encoding
let _utf8_encode = function(string) {
string = string.replace(/\r\n/g, "\n");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
} else if ((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
} else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
// console.log(utftext);
return utftext;
}
// private method for UTF-8 decoding
let _utf8_decode = function(utftext) {
var string = "";
var i = 0;
var c1, c2, c3;
var c = c1 = c2 = 0;
while (i < utftext.length) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
} else if ((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i + 1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
} else {
c2 = utftext.charCodeAt(i + 1);
c3 = utftext.charCodeAt(i + 2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}
let _checkChar = function(textJson) {
var pattern = new RegExp("[\u4E00-\u9FA5]+");
for (let item in textJson) {
if (pattern.test(textJson[item])) {
textJson[item] = encodeURIComponent(textJson[item]);
}
}
return JSON.stringify(textJson);
}
let _returnJson = function(textString) {
let res = JSON.parse(textString);
for (let item in res) {
res[item] = decodeURIComponent(res[item]);
}
return res;
}
}
结束!