This paper illustrates the ENCODE64 encryption algorithm implemented by JavaScript. Share to everyone for your reference. Specifically as follows:
This JavaScript code can implement the ENCODE64 encryption algorithm, the speed is quite good.
Encode64 codec (function () {var Codechar = "Paawo65gouf7ik2vi9-xq8cftexlcdy1hd3tv0ryzjbpn_blnss4mgrkqwmzjeuh";
window.encode64 = function (str) {var s = "";
var a = strtobytes (str);
Gets the byte array of the string, which is twice times the length of the string.
var res = a.length% 3;
3 bytes A set of processing, the remaining special processing var i = 2, V; for (; i < a.length; i + 3) {//3 bytes per 4 characters,//equivalent to 3 characters (actually 6 bytes) encoded in 8 characters (actually 16 bytes)//Looks like a lot of capacity expansion, but in the case of compression enabled, this
Some have been cancelled. v = a[i-2] + (A[i-1] << 8) + (A[i] << 16);
S + + Codechar.charat (V & 0x3f);
S + + Codechar.charat ((v >> 6) & 0x3f);
S + + Codechar.charat ((v >>) & 0x3f);
S + + Codechar.charat ((v >> 18));
} if (res = = 1) {//Word savings one time, complement 2 characters, 64*64>256 v = a[i-2];
S + + Codechar.charat (V & 0x3f);
S + + Codechar.charat ((v >> 6) & 0x3f);
else if (res = = 2) {//Word surplus 2 bits, complement 3 bytes, 64*64*64>256*256, so is feasible v = a[i-2] + (a[i-1) << 8);
S + + Codechar.charat (V & 0x3f); S + + Codechar.charat((v >> 6) & 0x3f);
S + + Codechar.charat ((v >>) & 0x3f);
return s;
};
Window.decode64 = function (codestr) {var dic = [];
for (var i = 0; i < codechar.length i++) {Dic[codechar.charat (i)] = i;
var code = [];
var res = codestr.length% 4;
var i = 3, V;
for (; i < codestr.length i + 4) {v = Dic[codestr.charat (i-3)];
V + + Dic[codestr.charat (i-2)] << 6;
V + + Dic[codestr.charat (i-1)] << 12;
V + + Dic[codestr.charat (i)] << 18;
Code.push (V & 0xFF, (v >> 8) & 0xFF, (v >>) & 0xff);
} if (res = = 2) {//The correct number of bytes is certainly more than 2 or 3, there is no 1 case, if present, discard.
v = Dic[codestr.charat (i-3)];
V + + Dic[codestr.charat (i-2)] << 6;
Code.push (v & 0xff);
else if (res = = 3) {v = Dic[codestr.charat (i-3)];
V + + Dic[codestr.charat (i-2)] << 6;
V + + Dic[codestr.charat (i-1)] << 12;
Code.push (V & 0xFF, (v >> 8) & 0xff); Return StRfrombytes (code);
}; })();
The
wants this article to help you with your JavaScript programming.