JS implementation of BASE64 encoding for mutual conversion

Source: Internet
Author: User

Simply point, Direct code

1. Code

functionBase64 () {//Private Property_keystr = "Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/="; //Public method for encoding     This. Encode =function(input) {varOutput = ""; varchr1, CHR2, CHR3, Enc1, Enc2, enc3, Enc4; vari = 0; Input=_utf8_encode (input);  while(I <input.length) {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); }          returnoutput; }         //Public method for decoding     This. decode =function(input) {varOutput = ""; varchr1, CHR2, CHR3; varenc1, Enc2, enc3, Enc4; vari = 0; Input= Input.replace (/[^a-za-z0-9\+\/\=]/g, "");  while(I <input.length) {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= output +String.fromCharCode (CHR2); }              if(Enc4! = 64) {Output= output +String.fromCharCode (CHR3); }} Output=_utf8_decode (output); returnoutput; }         //Private method for UTF-8 encoding_utf8_encode =function(String) {string= String.Replace (/\r\n/g, "\ n"); varUtftext = "";  for(varn = 0; n < string.length; n++) {              varc =string.charcodeat (n); if(C < 128) {Utftext+=String.fromCharCode (c); } Else if((C > 127) && (C < 2048) ) {Utftext+ = String.fromCharCode ((c >> 6) | 192); Utftext+ = String.fromCharCode ((C & 63) | 128); } Else{Utftext+ = String.fromCharCode ((c >> 12) | 224); Utftext+ = String.fromCharCode (((c >> 6) & 63) | 128); Utftext+ = String.fromCharCode ((C & 63) | 128); }             }          returnUtftext; }         //Private method for UTF-8 decoding_utf8_decode =function(utftext) {varString = ""; vari = 0; varc = C1 = C2 = 0;  while(I <utftext.length) {c=utftext.charcodeat (i); if(C < 128) {string+=String.fromCharCode (c); I++; } Else if((C > 191) && (C < 224) ) {C2= Utftext.charcodeat (i+1); String+ = String.fromCharCode (((C &) << 6) | (C2 & 63)); I+ = 2; } Else{C2= Utftext.charcodeat (i+1); C3= Utftext.charcodeat (i+2); String+ = String.fromCharCode (((C &) << 12) | ((C2 &) << 6) | (C3 & 63)); I+ = 3; }          }          returnstring; }  } 

2. Testing

// 1. Encryption   var str = ' 124 Chinese content ';   var New Base64 ();   var result = Base.encode (str);   // document.write (result);    // 2. Decryption   var result2 = Base.decode (result);  

JS implementation of BASE64 encoding for mutual conversion

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.