base64加密工具类 es6写法
一个ES6写法的Base64加密工具类 [代码]class Base64 {[代码][代码] [代码][代码]constructor() {[代码] [代码] [代码][代码]}[代码][代码] [代码][代码]_keyStr = [代码][代码]"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="[代码][代码];[代码] [代码] [代码][代码]encode(input) {[代码][代码] [代码][代码]var[代码] [代码]output = [代码][代码]""[代码][代码];[代码][代码] [代码][代码]var[代码] [代码]chr1, chr2, chr3, enc1, enc2, enc3, enc4;[代码][代码] [代码][代码]var[代码] [代码]i = 0;[代码][代码] [代码][代码]input = [代码][代码]this[代码][代码]._utf8_encode(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 + [代码][代码]this[代码][代码]._keyStr.charAt(enc1) + [代码][代码]this[代码][代码]._keyStr.charAt(enc2) + [代码][代码]this[代码][代码]._keyStr.charAt(enc3) + [代码][代码]this[代码][代码]._keyStr.charAt(enc4);[代码] [代码] [代码][代码]}[代码] [代码] [代码][代码]return[代码] [代码]output;[代码][代码] [代码][代码]};[代码][代码] [代码][代码]// public method for decoding[代码][代码] [代码][代码]decode(input) {[代码][代码] [代码][代码]var[代码] [代码]output = [代码][代码]""[代码][代码];[代码][代码] [代码][代码]var[代码] [代码]chr1, chr2, chr3;[代码][代码] [代码][代码]var[代码] [代码]enc1, enc2, enc3, enc4;[代码][代码] [代码][代码]var[代码] [代码]i = 0;[代码] [代码] [代码][代码]input = input.replace(/[^A-Za-z0-9\+\/\=]/g, [代码][代码]""[代码][代码]);[代码] [代码] [代码][代码]while[代码] [代码](i < input.length) {[代码] [代码] [代码][代码]enc1 = [代码][代码]this[代码][代码]._keyStr.indexOf(input.charAt(i++));[代码][代码] [代码][代码]enc2 = [代码][代码]this[代码][代码]._keyStr.indexOf(input.charAt(i++));[代码][代码] [代码][代码]enc3 = [代码][代码]this[代码][代码]._keyStr.indexOf(input.charAt(i++));[代码][代码] [代码][代码]enc4 = [代码][代码]this[代码][代码]._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 = [代码][代码]this[代码][代码]._utf8_decode(output);[代码] [代码] [代码][代码]return[代码] [代码]output;[代码][代码] [代码][代码]};[代码] [代码] [代码][代码]// private method for UTF-8 encoding[代码][代码] [代码][代码]_utf8_encode(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);[代码][代码] [代码][代码]}[代码] [代码] [代码][代码]}[代码] [代码] [代码][代码]return[代码] [代码]utftext;[代码][代码] [代码][代码]};[代码] [代码] [代码][代码]// private method for UTF-8 decoding[代码][代码] [代码][代码]_utf8_decode(utftext) {[代码][代码] [代码][代码]var[代码] [代码]string = [代码][代码]""[代码][代码];[代码][代码] [代码][代码]var[代码] [代码]i = 0;[代码][代码] [代码][代码]var[代码] [代码]c = 0;[代码][代码] [代码][代码]var[代码] [代码]c1 = 0;[代码][代码] [代码][代码]var[代码] [代码]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;[代码][代码] [代码][代码]}[代码][代码]}[代码] [代码]export {[代码][代码] [代码][代码]Base64[代码][代码]}[代码] 使用的话在要使用的js中 [代码]import{[代码][代码] [代码][代码]Base64[代码][代码]} from [代码][代码]'utils/base64.js'[代码][代码];[代码][代码]const base64 = [代码][代码]new[代码] [代码]Base64();[代码]既可以