Implementation of JavaScript BASE64 algorithm

Source: Internet
Author: User
Tags base64

JS version Base 64 algorithm Base64.js

varKeystr = "Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/=";//BASE64 encoding an ANSI-encoded stringfunctionencode64 (input) {varOutput = "";varCHR1, CHR2, CHR3 = "";varENC1, Enc2, enc3, Enc4 = "";vari = 0; Do{chr1= Input.charcodeat (i++); CHR2= Input.charcodeat (i++); Chr3= Input.charcodeat (i++); Enc1= Chr1 >> 2; Enc2= ((Chr1 & 3) << 4) | (CHR2 >> 4); Enc3= ((CHR2 &) << 2) | (CHR3 >> 6); Enc4= CHR3 & 63;if(IsNaN (CHR2)) {enc3= ENC4 = 64;} Else if(IsNaN (CHR3)) {Enc4= 64;} Output= output + Keystr.charat (ENC1) +Keystr.charat (ENC2)+ Keystr.charat (enc3) +Keystr.charat (ENC4); Chr1= CHR2 = CHR3 = ""; Enc1= ENC2 = Enc3 = Enc4 = "";}  while(I <input.length);returnoutput;}//converting an BASE64 encoded string to an ANSI-encoded stringfunctiondecode64 (input) {varOutput = "";varCHR1, CHR2, CHR3 = "";varENC1, Enc2, enc3, Enc4 = "";vari = 0; if(input.length% 4! = 0) {return"";}varBase64test =/[^a-za-z0-9\+\/\=]/G;if(base64test.exec (input)) {return"";} Do{enc1= Keystr.indexof (Input.charat (i++)); Enc2= Keystr.indexof (Input.charat (i++)); Enc3= Keystr.indexof (Input.charat (i++)); Enc4= Keystr.indexof (Input.charat (i++)); Chr1= (enc1 << 2) | (Enc2 >> 4); CHR2= ((Enc2 &) << 4) | (Enc3 >> 2); Chr3= ((enc3 & 3) << 6) |Enc4; Output= output +String.fromCharCode (CHR1);if(Enc3! = 64) {Output+=String.fromCharCode (CHR2);}if(Enc4! = 64) {Output+=String.fromCharCode (CHR3);} CHR1= CHR2 = CHR3 = ""; Enc1= ENC2 = Enc3 = Enc4 = "";}  while(I <input.length);returnoutput;} functionUtf16to8 (str) {varOut , I, Len, C; out= ""; Len=str.length;  for(i = 0; i < Len; i++) {C=str.charcodeat (i); if((c >= 0x0001) && (c <= 0x007F) ) { out+=Str.charat (i); } Else if(C > 0x07ff) { out+ = String.fromCharCode (0xE0 | ((c >> b) & 0x0F)); out+ = String.fromCharCode (0x80 | ((c >> 6) & 0x3F)); out+ = String.fromCharCode (0x80 | ((c >> 0) & 0x3F)); } Else{ out+ = String.fromCharCode (0xC0 | ((c >> 6) & 0x1F)); out+ = String.fromCharCode (0x80 | ((c >> 0) & 0x3F)); }  }  returnOut ;} functionutf8to16 (str) {varOut , I, Len, C; varChar2, Char3; out= ""; Len=str.length; I= 0;  while(I <Len) {C= Str.charcodeat (i++); Switch(C >> 4) {       Case0: Case1: Case2: Case3: Case4: Case5: Case6: Case7:        //0xxxxxxxOut + = Str.charat (i-1);  Break;  Case12: Case13:        //110x xxxx 10xx xxxxCHAR2 = Str.charcodeat (i++); out+ = String.fromCharCode (((C & 0x1F) << 6) | (Char2 & 0x3F));  Break;  Case14:        //1110 xxxx 10xx xxxx 10xx xxxxCHAR2 = Str.charcodeat (i++); Char3= Str.charcodeat (i++); out+ = String.fromCharCode (((C & 0x0F) << 12) |((Char2& 0x3F) << 6) |((Char3& 0x3F) << 0));  Break; }  }  returnOut ;}

Examples of parsing in JS:

Encryption

var testcode= "test88"; var resultcode= encode64 (Utf16to8 (Testcode)); Console.log (ResultCode);

Output

DGVzdDg4

Decrypt

var testcode= "DGVzdDg4"; ResultCode= utf8to16 (decode64 (Testcode)); Console.log (ResultCode );

Output

Test88

Implementation of JavaScript BASE64 algorithm

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.