Example analysis of the encode64 encryption algorithm implemented by JavaScript and encode64 instance analysis

Source: Internet
Author: User

Example analysis of the encode64 encryption algorithm implemented by JavaScript and encode64 instance analysis

This example describes 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.