An example analysis of ENCODE64 encryption algorithm implemented by JavaScript

Source: Internet
Author: User

This JavaScript code can implement the ENCODE64 encryption algorithm, the speed is quite good.

 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61-62 //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) {//per 3 bytes in 4 characters,//equivalent to 3 characters (actually 6 bytes) encoded in 8 characters (actually 16 bytes)//Looks like a lot of volume expansion, but when compression is enabled, these are offset 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); If else if (res = = 2) {//Word surplus 2 bits, complement 3 bytes, 64*64*64>256*256, so it 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 + = d Ic[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 definitely 2 or 3, there is no 1 case, if present, discard. V = Dic[codestr.charat (i-3)]; v + + Dic[codestr.charat (i-2)] <& Lt 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); }; })();
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.