赫拉的苹果 發表於 2019-10-8 07:50:00

javascript实现base64编码、解码

<p>我们知道,浏览器的window对象提供有window.atob()和window.btoa()方法可以对字符串进行Base64编码和解码。</p>
<div class="cnblogs_code">
<pre>console.log(window.btoa(window.atob('yanggb')));</pre>
</div>
<p>但是有些运行环境,比如nuxt的服务端环境没法使用window对象(window对象只有在浏览器的运行环境中才存在),所以需要自己实现一个Base64的编码解码功能,下面是用原生JavaScript实现该功能,可以作为一个常用工具使用。</p>
<p><span style="background-color: rgba(204, 255, 204, 1)"><strong>UTF-8编码的函数</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> utf8_encode (string) {
    string </span>= string.replace(/\r\n/g, '\n'<span style="color: rgba(0, 0, 0, 1)">);
    let utfText </span>= ''<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">for</span> (let n = 0; n &lt; string.length; n++<span style="color: rgba(0, 0, 0, 1)">) {
      let c </span>=<span style="color: rgba(0, 0, 0, 1)"> string.charCodeAt(n);
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (c &lt; 128<span style="color: rgba(0, 0, 0, 1)">) {
            utfText </span>+=<span style="color: rgba(0, 0, 0, 1)"> String.fromCharCode(c);
      } </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span>((c &gt; 127) &amp;&amp; (c &lt; 2048<span style="color: rgba(0, 0, 0, 1)">)) {
            utfText </span>+= String.fromCharCode((c &gt;&gt; 6) | 192<span style="color: rgba(0, 0, 0, 1)">);
            utfText</span>+= String.fromCharCode((c &amp; 63) | 128<span style="color: rgba(0, 0, 0, 1)">);
      } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
            utfText </span>+= String.fromCharCode((c &gt;&gt; 12) | 224<span style="color: rgba(0, 0, 0, 1)">);
            utfText </span>+= String.fromCharCode(((c &gt;&gt; 6) &amp; 63) | 128<span style="color: rgba(0, 0, 0, 1)">);
            utfText </span>+= String.fromCharCode((c &amp; 63) | 128<span style="color: rgba(0, 0, 0, 1)">);
      }

    }
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> utfText;
}</span></pre>
</div>
<p><strong><span style="background-color: rgba(204, 255, 204, 1)">UTF-8解码的函数</span></strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> utf8_decode (utfText) {
    let string </span>= ""<span style="color: rgba(0, 0, 0, 1)">;
    let i </span>= 0<span style="color: rgba(0, 0, 0, 1)">;
    let c </span>= 0<span style="color: rgba(0, 0, 0, 1)">;
    let c1 </span>= 0<span style="color: rgba(0, 0, 0, 1)">;
    let c2 </span>= 0<span style="color: rgba(0, 0, 0, 1)">;
    let c3 </span>= 0<span style="color: rgba(0, 0, 0, 1)">;
    </span><span style="color: rgba(0, 0, 255, 1)">while</span> ( i &lt;<span style="color: rgba(0, 0, 0, 1)"> utfText.length ) {
      c </span>=<span style="color: rgba(0, 0, 0, 1)"> utfText.charCodeAt(i);
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (c &lt; 128<span style="color: rgba(0, 0, 0, 1)">) {
            string </span>+=<span style="color: rgba(0, 0, 0, 1)"> String.fromCharCode(c);
            i</span>++<span style="color: rgba(0, 0, 0, 1)">;
      } </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span>((c &gt; 191) &amp;&amp; (c &lt; 224<span style="color: rgba(0, 0, 0, 1)">)) {
            c2 </span>= utfText.charCodeAt(i+1<span style="color: rgba(0, 0, 0, 1)">);
            string </span>+= String.fromCharCode(((c &amp; 31) &lt;&lt; 6) | (c2 &amp; 63<span style="color: rgba(0, 0, 0, 1)">));
            i </span>+= 2<span style="color: rgba(0, 0, 0, 1)">;
      } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {
            c2 </span>= utfText.charCodeAt(i+1<span style="color: rgba(0, 0, 0, 1)">);
            c3 </span>= utfText.charCodeAt(i+2<span style="color: rgba(0, 0, 0, 1)">);
            string </span>+= String.fromCharCode(((c &amp; 15) &lt;&lt; 12) | ((c2 &amp; 63) &lt;&lt; 6) | (c3 &amp; 63<span style="color: rgba(0, 0, 0, 1)">));
            i </span>+= 3<span style="color: rgba(0, 0, 0, 1)">;
      }
    }
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> string;
}</span></pre>
</div>
<p><span style="background-color: rgba(204, 255, 204, 1)"><strong>Base64编码的函数</strong></span></p>
<div class="cnblogs_code">
<pre>export const encode = (input) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    let output </span>= ""<span style="color: rgba(0, 0, 0, 1)">;
    let chr1, chr2, chr3, enc1, enc2, enc3, enc4;
    let i </span>= 0<span style="color: rgba(0, 0, 0, 1)">;
    input </span>=<span style="color: rgba(0, 0, 0, 1)"> utf8_encode(input);
    </span><span style="color: rgba(0, 0, 255, 1)">while</span> (i &lt;<span style="color: rgba(0, 0, 0, 1)"> input.length) {
      chr1 </span>= input.charCodeAt(i++<span style="color: rgba(0, 0, 0, 1)">);
      chr2 </span>= input.charCodeAt(i++<span style="color: rgba(0, 0, 0, 1)">);
      chr3 </span>= input.charCodeAt(i++<span style="color: rgba(0, 0, 0, 1)">);
      enc1 </span>= chr1 &gt;&gt; 2<span style="color: rgba(0, 0, 0, 1)">;
      enc2 </span>= ((chr1 &amp; 3) &lt;&lt; 4) | (chr2 &gt;&gt; 4<span style="color: rgba(0, 0, 0, 1)">);
      enc3 </span>= ((chr2 &amp; 15) &lt;&lt; 2) | (chr3 &gt;&gt; 6<span style="color: rgba(0, 0, 0, 1)">);
      enc4 </span>= chr3 &amp; 63<span style="color: rgba(0, 0, 0, 1)">;
      </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (isNaN(chr2)) {
            enc3 </span>= enc4 = 64<span style="color: rgba(0, 0, 0, 1)">;
      } </span><span style="color: rgba(0, 0, 255, 1)">else</span> <span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (isNaN(chr3)) {
            enc4 </span>= 64<span style="color: rgba(0, 0, 0, 1)">;
      }
      output </span>= output +<span style="color: rgba(0, 0, 0, 1)">
      input.charAt(enc1) </span>+ input.charAt(enc2) +<span style="color: rgba(0, 0, 0, 1)">
      input.charAt(enc3) </span>+<span style="color: rgba(0, 0, 0, 1)"> input.charAt(enc4);
    }
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> output;
}</span></pre>
</div>
<p><span style="background-color: rgba(204, 255, 204, 1)"><strong>Base64解码的函数</strong></span></p>
<div class="cnblogs_code">
<pre>export const decode = (input) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    let output </span>= ""<span style="color: rgba(0, 0, 0, 1)">;
    let chr1, chr2, chr3;
    let enc1, enc2, enc3, enc4;
    let i </span>= 0<span style="color: rgba(0, 0, 0, 1)">;
    input </span>= input.replace(/[^A-Za-z0-9\+\/\=]/g, ""<span style="color: rgba(0, 0, 0, 1)">);
    </span><span style="color: rgba(0, 0, 255, 1)">while</span> (i &lt;<span style="color: rgba(0, 0, 0, 1)"> input.length) {
      enc1 </span>= input.indexOf(input.charAt(i++<span style="color: rgba(0, 0, 0, 1)">));
      enc2 </span>= input.indexOf(input.charAt(i++<span style="color: rgba(0, 0, 0, 1)">));
      enc3 </span>= input.indexOf(input.charAt(i++<span style="color: rgba(0, 0, 0, 1)">));
      enc4 </span>= input.indexOf(input.charAt(i++<span style="color: rgba(0, 0, 0, 1)">));
      chr1 </span>= (enc1 &lt;&lt; 2) | (enc2 &gt;&gt; 4<span style="color: rgba(0, 0, 0, 1)">);
      chr2 </span>= ((enc2 &amp; 15) &lt;&lt; 4) | (enc3 &gt;&gt; 2<span style="color: rgba(0, 0, 0, 1)">);
      chr3 </span>= ((enc3 &amp; 3) &lt;&lt; 6) |<span style="color: rgba(0, 0, 0, 1)"> enc4;
      output </span>= output +<span style="color: rgba(0, 0, 0, 1)"> String.fromCharCode(chr1);
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (enc3 != 64<span style="color: rgba(0, 0, 0, 1)">) {
            output </span>= output +<span style="color: rgba(0, 0, 0, 1)"> String.fromCharCode(chr2);
      }
      </span><span style="color: rgba(0, 0, 255, 1)">if</span> (enc4 != 64<span style="color: rgba(0, 0, 0, 1)">) {
            output </span>= output +<span style="color: rgba(0, 0, 0, 1)"> String.fromCharCode(chr3);
      }
    }
    output </span>=<span style="color: rgba(0, 0, 0, 1)"> utf8_decode(output);
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> output;
}</span></pre>
</div>
<p>&nbsp;</p>
<p>"我只是从来没有真正认识过你。"</p>

</div>
<div id="MySignature" role="contentinfo">
    你要去做一个大人,不要回头,不要难过。<br><br>
来源:https://www.cnblogs.com/yanggb/p/11536133.html
頁: [1]
查看完整版本: javascript实现base64编码、解码