JavaScript implemented Base64 encoding and decoding

zhaozj2021-02-16  62

Base64 </ Title></p> <p><script language = javaScript></p> <p>VAR Base64Encodechars = "Abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 /";</p> <p>VAR Base64Decodechars = new array (</p> <p>-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,</p> <p>-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,</p> <p>-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,</p> <p>52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,</p> <p>-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,</p> <p>15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,</p> <p>-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,</p> <p>41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);</p> <p>Function Base64encode (STR) {</p> <p>VAR OUT, I, LEN;</p> <p>VAR C1, C2, C3;</p> <p>Len = str.length;</p> <p>i = 0;</p> <p>OUT = "";</p> <p>While (i <len) {</p> <p>C1 = Str.Charcodeat (i ) & 0xff;</p> <p>IF (i == LEN)</p> <p>{</p> <p>Out = base64encodechars.Charat (C1 >> 2);</p> <p>OUT = Base64encodechars.Charat ((C1 & 0x3) << 4);</p> <p>OUT = "==";</p> <p>Break;</p> <p>}</p> <p>C2 = str.charcodeat (i );</p> <p>IF (i == LEN)</p> <p>{</p> <p>Out = base64encodechars.Charat (C1 >> 2);</p> <p>OUT = Base64Encodechars.Charat (((C1 & 0x3) << 4) | ((C2 & 0xF0) >> 4));</p> <p>OUT = Base64encodechars.Charat ((C2 & 0xF) << 2);</p> <p>OUT = "=";</p> <p>Break;</p> <p>}</p> <p>C3 = Str.Charcodeat (i );</p> <p>Out = base64encodechars.Charat (C1 >> 2);</p> <p>OUT = Base64Encodechars.Charat (((C1 & 0x3) << 4) | ((C2 & 0xF0) >> 4));</p> <p>OUT = Base64Encodechars.Charat (((C2 & 0xF) << 2) | ((C3 & 0xC0) >> 6); out = base64encodechars.charat (C3 & 0x3f);</p> <p>}</p> <p>Return Out;</p> <p>}</p> <p>Function Base64Decode (str) {</p> <p>VAR C1, C2, C3, C4;</p> <p>VAR I, LEN, OUT;</p> <p>Len = str.length;</p> <p>i = 0;</p> <p>OUT = "";</p> <p>While (i <len) {</p> <p>/ * c1 * /</p> <p>Do {</p> <p>C1 = base64decodechars [str.charcodeat (i ) & 0xff];</p> <p>WHILE (I <LEN && C1 == -1);</p> <p>IF (C1 == -1)</p> <p>Break;</p> <p>/ * C2 * /</p> <p>Do {</p> <p>C2 = Base64DecodeChars [str.charcodeat (i ) & 0xff];</p> <p>WHILE (I <LEN && C2 == -1);</p> <p>IF (C2 == -1)</p> <p>Break;</p> <p>OUT = String.Fromcharcode ((C1 << 2) | ((C2 & 0x30) >> 4));</p> <p>/ * C3 * /</p> <p>Do {</p> <p>C3 = Str.Charcodeat (i ) & 0xff;</p> <p>IF (C3 == 61)</p> <p>Return Out;</p> <p>C3 = base64decodechars [c3];</p> <p>WHILE (I <LEN && C3 == -1);</p> <p>IF (C3 == -1)</p> <p>Break;</p> <p>OUT = String.Fromcharcode (((C2 & 0xF) << 4) | ((C3 & 0x3c) >> 2));</p> <p>/ * C4 * /</p> <p>Do {</p> <p>C4 = Str.Charcodeat (i ) & 0xff;</p> <p>IF (C4 == 61)</p> <p>Return Out;</p> <p>C4 = base64decodechars [c4];</p> <p>WHILE (I <LEN && C4 == -1);</p> <p>IF (C4 == -1)</p> <p>Break;</p> <p>OUT = String.fromcharcode (((C3 & 0x03) << 6) | C4);</p> <p>}</p> <p>Return Out;</p> <p>}</p> <p>Function UTF16TO8 (STR) {</p> <p>VAR OUT, I, LEN, C;</p> <p>OUT = "";</p> <p>Len = str.length;</p> <p>For (i = 0; i <len; i ) {</p> <p>C = str.charcodeat (i);</p> <p>IF ((c> = 0x0001) && (c <= 0x007f)) {</p> <p>OUT = Str.Charat (i);</p> <p>} else IF (c> 0x07ff) {</p> <p>OUT = String.Fromcharcode (0xE0 | ((C >> 12) & 0x0f);</p> <p>Out = string.fromcharcode (0x80 | ((c >> 6) & 0x3f); out = string.fromcharcode (0x80 | (((c >> 0) & 0x3f));</p> <p>} else {</p> <p>Out = string.fromcharcode (0xc0 | ((c >> 6) & 0x1f);</p> <p>Out = string.fromcharcode (0x80 | (((c >> 0) & 0x3f);</p> <p>}</p> <p>}</p> <p>Return Out;</p> <p>}</p> <p>Function UTF8TO16 (STR) {</p> <p>VAR OUT, I, LEN, C;</p> <p>Var Char2, Char3;</p> <p>OUT = "";</p> <p>Len = str.length;</p> <p>i = 0;</p> <p>While (i <len) {</p> <p>C = str.charcodeat (i );</p> <p>Switch (c >> 4)</p> <p>{</p> <p>Case 0: Case 1: Case 2: Case 3: Case 4: Case 5: Case 6: Case 7:</p> <p>// 0xxxxxxx</p> <p>OUT = str.charat (i-1);</p> <p>Break;</p> <p>Case 12: Case 13:</p> <p>// 110x xxxx 10xx xxxx</p> <p>Char2 = str.charcodeat (i );</p> <p>Out = string.fromcharcode ((C & 0x1f) << 6) | (char2 & 0x3f));</p> <p>Break;</p> <p>Case 14:</p> <p>// 1110 xxxx 10xx xxxx 10xx xxxx</p> <p>Char2 = str.charcodeat (i );</p> <p>Char3 = str.charcodeat (i );</p> <p>Out = string.fromcharcode (((c & 0x0f) << 12) |</p> <p>((char2 & 0x3f) << 6) |</p> <p>((Char3 & 0x3f) << 0));</p> <p>Break;</p> <p>}</p> <p>}</p> <p>Return Out;</p> <p>}</p> <p>Function DOIT () {</p> <p>VAR f = Document.f</p> <p>f.output.value = base64encode (UTF16TO8 (F.Source.Value))</p> <p>f.Decode.Value = UTF8TO16 (Base64Decode (f.output.value))</p> <p>}</p> <p></ script></p> <p></ HEAD></p> <p><Body></p> <p><H1> Base64 </ h1></p> <p><Form name = "f"></p> <p>Original code <br></p> <p><TEXTAREA NAME = "source" ROWS = 4 COLS = 60 WRAP = "soft"> </ TEXTAREA> <BR> <BR></p> <p>Base64 encode <br></p> <p><TEXTAREA NAME = "output" ROWS = 4 COLS = 60 WRAP = "soft"> </ TEXTAREA> <BR> <BR></p> <p>Base64 Decode <br></p> <p><TEXTAREA NAME = "decode" ROWS = 4 COLS = 60 WRAP = "soft"> </ TEXTAREA> <BR> <BR> <INPUT TYPE = BUTTON VALUE = "conversion" ONCLICK = "doit ()"></p> <p></ Form></p> <p></ Body></p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-16531.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="16531" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.040</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'DhF3S5NGKAGLpOV_2Fe_2F1_2FceCVW2re1JGNzF2SHf81tevZdnbh9YMI30GGsQENA5VUHAnxx09ibO9CJWxU3rF6uQ_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>