This article mainly introduces the encode64 encryption algorithm implemented by JavaScript. The example analyzes the techniques of using javascript to process encode64 encoding for string encryption, which is very simple and practical, for more information, see the example in this article to describe the encode64 encryption algorithm implemented by JavaScript. Share it with you for your reference. The details are as follows:
This JavaScript code can implement the encode64 encryption algorithm, and the speed is quite good.
// Encode64 codec (function () {var codeChar = "PaAwO65goUf7IK2vi9-xq8cFTEXLCDY1Hd3tV0ryzjbpN_BlnSs4mGRkQWMZJeuh"; window. encode64 = function (str) {var s = ""; var a = strToBytes (str); // Obtain the byte array of the string. the array length is twice the length of the string. var res =. length % 3; // a group of three bytes for processing. The remaining special processing values are var I = 2, v; for (; I <. length; I + = 3) {// Each 3 bytes is represented by 4 characters, // equivalent to 3 characters (actually 6 bytes) encode with 8 characters (actually 16 bytes) // It seems that the capacity has expanded a lot, but when compression is enabled, these are offset by 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> 12) & 0x3f); s + = codeChar. charAt (v> 18);} if (res = 1) {// when the remaining byte is one, add 2 characters, 64*64> 256 v = a [I-2]; s + = codeChar. charAt (v & 0x3f); s + = codeChar. charAt (v> 6) & 0x3f);} when else if (res = 2) {// when the remaining two bytes exist, add three bytes, 64*64*64> 256*256, so it is feasible to v = a [I-2] + (a [I-1] <8); s + = codeChar. charAt (v & 0x3f); s + = codeChar. charAt (v> 6) & 0x3f); s + = codeChar. charAt (v> 12) & 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> 16) & 0xff);} if (res = 2) {// The correct number of bytes must be 2 or 3. if no value is 1, discard it. 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 );};})();
I hope this article will help you design javascript programs.