JS implementation BASE64 Coding and decoding examples _javascript skills

Source: Internet
Author: User

JS realizes Base64 encoding and decoding

Base64 is actually a simple method of permutation encryption, but the usefulness of BASE64 is often not to prevent information leakage, and in order to facilitate transmission, the BASE64 encoded information will be longer than the original information, about 4/3 times times.

Base64 is a representation that represents binary data based on 64 printable characters. Because 2 of the 6 times equals 64, each 6 bit is a unit, corresponding to a printable character.

Base64 is often used to represent, transmit, and store binary data in situations where text data is normally processed. Includes MIME-Email,email via MIME, which stores complex data in XML.

1. Encryption and decryption methods used:

1. Encrypt 
var str = ' 124 Chinese content '; 
var base = new Base64 (); 
var result = Base.encode (str); 
document.write (result); 
2. Decrypt 
var result2 = Base.decode (result); 
document.write (RESULT2); 

2. Encryption, decryption algorithm encapsulation:

/** * * * * * * * * * * @author haitao.tu * @date 2010-04-26 * @email tuhaitao@foxmail.com * * * * * * * funct 
 
 Ion Base64 () {//private property _keystr = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/="; 
  Public method for Encoding This.encode = function (input) {var output = ""; 
  var chr1, CHR2, CHR3, Enc1, Enc2, enc3, Enc4; 
  var i = 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); 
 return output; }//Public methOD for decoding this.decode = function (input) {var output = ""; 
  var chr1, CHR2, CHR3; 
  var enc1, Enc2, enc3, Enc4; 
  var i = 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!=) {output = output + String.fromCharCode (CHR2); 
   } if (Enc4!=) {output = output + String.fromCharCode (CHR3); 
  } output = _utf8_decode (output); 
 return output; 
  }//Private method for UTF-8 encoding _utf8_encode = function (string) {string = String.Replace (/\r\n/g, "\ n"); 
  var utftext = ""; for (var n = 0; n < String.Length; 
   n++) {var c = 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); 
 } return utftext; 
  }//Private method for UTF-8 decoding _utf8_decode = function (utftext) {var string = ""; 
  var i = 0; 
  var c = 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; 
 } return string; 
 } 
}

Thank you for reading, I hope to help you, thank you for your support for this site!

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.