JS implementation Base64_encode and Base64_decode (instance code) _javascript skills

Source: Internet
Author: User
Tags 0xc0 base64 base64 encode printable characters

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. Three bytes have 24 bits, corresponding to 4 Base64 units, that is, 3 bytes need to be represented by 4 printable characters. It can be used as a transport encoding for e-mail messages. printable characters in Base64 include letters A-Z, a-Z, number 0-9, 62 characters in total, and two printable symbols in different systems (this class library uses ' + ', '/' these two characters). Some other coding methods, such as Uuencode, and later versions of BinHex use different 64 character sets to represent 6 binary digits, but they are not called Base64.
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.
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.

Implementation code One, test can be used

var base64encodechars =  "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/"; Var base64decodechars = new array ( -1, -1, -1, -1, -1, -1, -1 ,  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,  62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58,  59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 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, -1,  -1, -1, -1,  -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35,  36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
 48, 49, 50, 51, -1, -1, -1, -1, -1);
/**  * base64 Code  *  @param  {object} str  */function base64encode (str) {
    var out, i, len;
    var c1, c2, c3;
    len = str.length;
    i = 0;
    out =  "";     while  (I < len)  {       
 c1 = str.charcodeat (i++)  & 0xff;         if  (I == len)  {             out +=&nbsP;base64encodechars.charat (C1 >> 2);             out += 
Base64encodechars.charat ((c1 & 0x3)  << 4);
            out +=  "= =";
            break;         }         c2 =
 str.charcodeat (i++);         if  (I == len)  {             out += base64encodechars.charat (c1 >>
 2);             out +=  Base64encodechars.charat ((c1 & 0x3)  << 4)  |  ((c2 & 0xf0
) ( >> 4));     &nbsP;       out += base64encodechars.charat ((C2 & 0xF)
 << 2);
            out +=  "=";
            break;         }         c3 =
 str.charcodeat (i++);         out += base64encodechars.charat (c1 >>
 2);         out += base64encodechars.charat ((c1 &
 0X3)  << 4)  |  ((c2 & 0xf0)  >> 4));         out += base64encodechars.charat ((c2 &
 0XF)  << 2)  |  ((c3 & 0xc0)  >> 6));          out += base64encodechars.charat (c3 & 0x3f);
    }     return out;
/**  * base64 decoding  *  @param  {object} str  */function base64decode (str) {
    var c1, c2, c3, c4;
    var i, len, out;
    len = str.length;
    i = 0;
    out =  "";     while  (I < len)  {         /* c1 */        do {             c1 = base64decodechars[str.charcodeat (i++)  
& 0xff];        &NBSP}         while  (i  < len &&&nbsP;C1 == -1);         if  (c1 == -1)       
        break;         /* c2 */         do {            c2 = 
Base64decodechars[str.charcodeat (i++)  & 0xff];        &NBSP}         while  (i
 < LEN && C2 == -1);         if  (c2 == -1)       
        break;         out += string.fromcharcode ((C1 << 2)
 |  ((c2 & 0x30)  >> 4));         /* c3 */        do {   
         c3 = str.charcodeat (i++)  & 0xff;
            if  (c3 == 61)                   return 
Out
            c3 = base64DecodeChars[c3];        &NBSP}         while  (i
 < LEN && C3 == -1);         if  (c3 == -1)       
        break;         out += string.fromcharcode ((C2 & 0XF) &nbsP;<< 4)  |  ((c3 & 0x3c)  >> 2));         /* c4 */         do {            c4 = 
Str.charcodeat (i++)  & 0xff;
            if  (c4 == 61)                   return 
Out
            c4 = base64DecodeChars[c4];        &NBSP}         while  (i
 < LEN && C4 == -1);         if  (c4 == -1)                break;
        out += string.fromcharcode ((c3 & 0x03
) ( << 6)  | c4);
    }     return out;
/**  * utf16 ext. UTF8  *  @param  {object} str  */function utf16to8 (str) {
    var out, 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 >> 12)  & 0x0F));                 out +=
 string.fromcharcode (0x80 |  (c >> 6)  & 0x3f));                 out +=
 string.fromcharcode (0x80 |  (c >> 0)  & 0x3f));             }              else {                 out +=&nBsp
String.fromCharCode (0xc0 |  (c >> 6)  & 0x1f));                 out +=
 string.fromcharcode (0x80 |  (c >> 0)  & 0x3f));             }        
  return out;
/**  * utf8 ext. utf16  *  @param  {object} str  */function utf8to16 (str) {
    var out, i, len, c;
    var char2, char3;
    out =  "";
    len = str.length;
    i = 0;     while  (I < len)  {       
 c = str.charcodeat (i++);         switch&nBSP; (c >> 4)  {             Case 0:             case 1:              CASE 2:              case 3:           
  case 4:             case 5:             case 6:              case 7:                  // 0xxxxxxx        
         out += str.charat (i - 1);                 break;             CASE 12:              CASE 13:                  // 110x xxxx 10xx xxxx                  char2 = str.charcodeat
(i++);                 out +=  string.fromcharcode ((c & 0x1f)  << 6)  |  (char2 & 
0x3F));
                break;             CASE 14:             &Nbsp;    // 1110 xxxx10xx xxxx10xx xxxx      
           char2 = str.charcodeat (i++);                 char3 =
 str.charcodeat (i++);                 out +=  string.fromcharcode ((c & 0x0f)  << 12)  |  ((char2 &
 0x3f)  << 6)  |  ((char3 & 0x3f)  << 0));
                break;         }          return 
Out }//demo//function doit () {//    var f = document.f;//    &nbsP;f.output.value = base64encode (Utf16to8 (F.source.value));
    f.decode.value = utf8to16 (Base64decode (F.output.value)); //}

Functional split version, that is, two JS functions are written out separately

JavaScript base64 cryptographic functions and Base64 decryption functions, imitation PHP base64_encode () and Base64_decode (). Attached examples. You can save the JS code to base64.js and call it when needed.
Note: Base64 encryption and decryption are solid, please do not change the following code, or you may cause a program error, or can not get the correct results.
To ensure the correctness of the code, please download the attachment base64.js (no instances) and do not copy the page code directly.

Base64 Encryption Code

    function Base64_encode (str) {var c1, C2, C3;        
        var base64encodechars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/";

        var i = 0, len= str.length, string = ';
            while (I < len) {c1 = Str.charcodeat (i++) & 0xFF;
                if (i = = len) {string + Base64encodechars.charat (C1 >> 2);
                string + = Base64encodechars.charat ((C1 & 0x3) << 4);
                string + = "= =";
            Break
            } C2 = Str.charcodeat (i++);
                if (i = = len) {string + Base64encodechars.charat (C1 >> 2); String + Base64encodechars.charat ((C1 & 0x3) << 4) |
                ((C2 & 0xF0) >> 4));
                string + = Base64encodechars.charat ((C2 & 0xF) << 2);
                string + = "=";
            Break
            } C3 = Str.charcodeat (i++); string + = Base64encodechArs.charat (C1 >> 2); String + Base64encodechars.charat ((C1 & 0x3) << 4) |
            ((C2 & 0xF0) >> 4)); String + Base64encodechars.charat ((C2 & 0xF) << 2) |
            ((C3 & 0xc0) >> 6)); string + = Base64encodechars.charat (C3 & 0x3F)} return string}

Base64 decryption, code

function Base64_decode (str) {var c1, C2, C3, C4;
            var base64decodechars = new Array (-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 1
            2,-1,-1,-1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,-1,-1,-1,-1,-1,-1, 1, 0, 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,-1,-1,-1,-1, 1, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 1,-1,-
        1,-1,-1);

        var i=0, Len = str.length, string = '; while (I < len) {do{C1 = base64decodechars[str.charcodeat (i++) & 0xFF]} w

            Hile (i < len && C1 = = 1);

        if (C1 = = 1) break;    do{C2 = base64decodechars[str.charcodeat (i++) & 0xFF]} while (i) ;

            Len && c2 =-1);

            if (C2 = = 1) break; String + string.fromcharcode (C1 << 2) |

            ((C2 & 0x30) >> 4));
                do{C3 = Str.charcodeat (i++) & 0xFF;

                if (C3 = =) return string;

            C3 = Base64decodechars[c3]} while (I < Len && C3 = = 1);

            if (C3 = = 1) break; String + String.fromCharCode ((C2 & 0XF) << 4) |

            ((C3 & 0x3c) >> 2));
                do{C4 = str.charcodeat (i++) & 0xFF;
                if (C4 = =) return string;

            C4 = base64decodechars[c4]} while (I < len && C4 = = 1);

            if (C4 = = 1) break; string + = String.fromCharCode ((C3 & 0x03) << 6) | c4)} return string; }

Test code for the code above

 document.write (Base64_encode (' www.jb51.net '));
    document.write (' <br/> ');
    

Implementation code two: for UTF8

&lt;script language= ' JavaScript ' &gt;/* utf.js-utf-8 &lt;=&gt; UTF-16 convertion * Copyright (C) 1999 Masanao & lt;iz@onicos.co.jp&gt; * version:1.0 * LASTMODIFIED:DEC 1999 * This library are free.
You can redistribute it and/or modify it.
* * * Interfaces: * UTF8 = Utf16to8 (UTF16);
* utf16 = Utf16to8 (UTF8);

  */function Utf16to8 (str) {var out, I, Len, C;
  out = "";
  len = str.length;
   for (i = 0; i &lt; len; i++) {c = str.charcodeat (i);
   if ((c &gt;= 0x0001) &amp;&amp; (c &lt;= 0x007f)) {out = = Str.charat (i); else if (C &gt; 0x07ff) {out + = String.fromCharCode (0xe0 |
    ((c &gt;&gt;) &amp; 0x0f)); Out + + string.fromcharcode (0x80 |
    ((c &gt;&gt; 6) &amp; 0x3F)); Out + + string.fromcharcode (0x80 |
   ((c &gt;&gt; 0) &amp; 0x3F)); else {out = = String.fromCharCode (0xc0 |
    ((c &gt;&gt; 6) &amp; 0x1F)); Out + + string.fromcharcode (0x80 |
   ((c &gt;&gt; 0) &amp; 0x3F));
} return out; function Utf8to16 (str) {var out, I, lEn, C;

  var char2, Char3;
  out = "";
  len = str.length;
  i = 0;
   while (I &lt; len) {c = str.charcodeat (i++); Switch (c &gt;&gt; 4) {case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7://0xxxxxxx out = = S
    Tr.charat (i-1);
   Break
    Case 12:case://110x xxxx 10xx xxxx char2 = str.charcodeat (i++); Out + + String.fromCharCode ((C &amp; 0x1F) &lt;&lt; 6) |
    (Char2 &amp; 0x3F));
   Break
    Case://1110 xxxx 10xx xxxx 10xx xxxx char2 = str.charcodeat (i++);
    CHAR3 = Str.charcodeat (i++);
           Out + + String.fromCharCode ((C &amp; 0x0f) &lt;&lt; 12) |
           ((Char2 &amp; 0x3F) &lt;&lt; 6) |
    ((Char3 &amp; 0x3F) &lt;&lt; 0));
   Break
} return out; }/* Copyright (C) 1999 Masanao Izumo &lt;iz@onicos.co.jp&gt; * version:1.0 * LASTMODIFIED:DEC 1999 * This library I s free.
You can redistribute it and/or modify it.
* * * Interfaces: * b64 = base64encode (data);
* data = Base64decode (B64);

*/
var base64encodechars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/";  var base64decodechars = new Array (-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 1,-1,-1,-1,-1,-1,-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 1,-1, 62,-1, 1, 1, 63, 52 4, 55, 56, 57, 58, 59, 60, 61,-1,-1,-1,-1,-1,-1,-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1
  7, 18, 19, 20, 21, 22, 23, 24, 25,-1,-1, 1,-1, 1, 1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,

41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,-1,-1,-1,-1,-1);
  function Base64Encode (str) {var out, I, Len;

  var C1, C2, C3;
  len = str.length;
  i = 0;
  out = "";
   while (I &lt; len) {c1 = Str.charcodeat (i++) &amp; 0xFF;
    if (i = = len) {out + = Base64encodechars.charat (C1 &gt;&gt; 2);
    Out + = Base64encodechars.charat ((C1 &amp; 0x3) &lt;&lt; 4);
    Out + = "= =";
   Break } c2 = str.charCodeAt (i++);
    if (i = = len) {out + = Base64encodechars.charat (C1 &gt;&gt; 2); Out + + Base64encodechars.charat ((C1 &amp; 0x3) &lt;&lt; 4) |
    ((C2 &amp; 0xF0) &gt;&gt; 4));
    Out + = Base64encodechars.charat ((C2 &amp; 0xF) &lt;&lt; 2);
    Out + = "=";
   Break
   } C3 = Str.charcodeat (i++);
   Out + = Base64encodechars.charat (C1 &gt;&gt; 2); Out + + Base64encodechars.charat ((C1 &amp; 0x3) &lt;&lt; 4) |
   ((C2 &amp; 0xF0) &gt;&gt; 4)); Out + + Base64encodechars.charat ((C2 &amp; 0xF) &lt;&lt; 2) |
   ((C3 &amp; 0xc0) &gt;&gt;6));
  Out + = Base64encodechars.charat (C3 &amp; 0x3F);
} return out;
  function Base64decode (str) {var c1, C2, C3, C4;

  var i, Len, out;
  len = str.length;
  i = 0;
  out = "";
   while (I &lt; len) {/* C1 */do {c1 = Base64decodechars[str.charcodeat (i++) &amp; 0xFF];
   while (I &lt; len &amp;&amp; C1 = = 1);

   if (C1 = = 1) break;
   /* C2/do {c2 = Base64decodechars[str.charcodeat (i++) &amp; 0xFF]; } while(I &lt; len &amp;&amp; C2 = = 1);

   if (C2 = = 1) break; Out + + string.fromcharcode (C1 &lt;&lt; 2) |

   ((C2 &amp; 0x30) &gt;&gt; 4));
    /* C3/Do {c3 = Str.charcodeat (i++) &amp; 0xFF;
    if (C3 = =) return out;
   C3 = Base64decodechars[c3];
   while (I &lt; Len &amp;&amp; C3 = = 1);

   if (C3 = = 1) break; Out + + String.fromCharCode ((C2 &amp; 0XF) &lt;&lt; 4) |

   ((C3 &amp; 0x3c) &gt;&gt; 2));
    /* C4/do {c4 = str.charcodeat (i++) &amp; 0xFF;
    if (C4 = =) return out;
   C4 = base64decodechars[c4];
   while (i &lt; len &amp;&amp; C4 = = 1);
   if (C4 = = 1) break;
  Out + = String.fromCharCode ((C3 &amp; 0x03) &lt;&lt; 6) | c4);
} return out;
 }//input base64 encode function Strdecode (str) {return utf8to16 (Base64decode (str));

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.