Base64 encoding encryption and decryption

From , 4 Years ago, written in JavaScript, viewed 63 times.
URL https://pastebin.vip/view/703957b6
  1. <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="content-type" content="text/html; charset=gb2312">
  5. <title>base64编码加密解密</title>
  6. <script language="javascript">
  7.     var base64encodechars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  8.     var base64decodechars = new Array(
  9.     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  10.     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  11.     -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
  12.     52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
  13.     -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  14.     15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
  15.     -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  16.     41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);
  17.  
  18.     function base64encode(str) {
  19.         var out, i, len;
  20.         var c1, c2, c3;
  21.         len = str.length;
  22.         i = 0;
  23.         out = "";
  24.         while (i < len) {
  25.             c1 = str.charCodeAt(i++) & 0xff;
  26.             if (i == len) {
  27.                 out += base64encodechars.charAt(c1 >> 2);
  28.                 out += base64encodechars.charAt((c1 & 0x3) << 4);
  29.                 out += "==";
  30.                 break;
  31.             }
  32.             c2 = str.charCodeAt(i++);
  33.             if (i == len) {
  34.                 out += base64encodechars.charAt(c1 >> 2);
  35.                 out += base64encodechars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xf0) >> 4));
  36.                 out += base64encodechars.charAt((c2 & 0xf) << 2);
  37.                 out += "=";
  38.                 break;
  39.             }
  40.             c3 = str.charCodeAt(i++);
  41.             out += base64encodechars.charAt(c1 >> 2);
  42.             out += base64encodechars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xf0) >> 4));
  43.             out += base64encodechars.charAt(((c2 & 0xf) << 2) | ((c3 & 0xc0) >> 6));
  44.             out += base64encodechars.charAt(c3 & 0x3f);
  45.         }
  46.         return out;
  47.     }
  48.     function base64decode(str) {
  49.         var c1, c2, c3, c4;
  50.         var i, len, out;
  51.  
  52.         len = str.length;
  53.  
  54.         i = 0;
  55.         out = "";
  56.         while (i < len) {
  57.            
  58.             do {
  59.                 c1 = base64decodechars[str.charCodeAt(i++) & 0xff];
  60.             } while (i < len && c1 == -1);
  61.             if (c1 == -1)
  62.                 break;
  63.  
  64.            
  65.             do {
  66.                 c2 = base64decodechars[str.charCodeAt(i++) & 0xff];
  67.             } while (i < len && c2 == -1);
  68.             if (c2 == -1)
  69.                 break;
  70.  
  71.             out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
  72.  
  73.            
  74.             do {
  75.                 c3 = str.charCodeAt(i++) & 0xff;
  76.                 if (c3 == 61)
  77.                     return out;
  78.                 c3 = base64decodechars[c3];
  79.             } while (i < len && c3 == -1);
  80.             if (c3 == -1)
  81.                 break;
  82.  
  83.             out += String.fromCharCode(((c2 & 0xf) << 4) | ((c3 & 0x3c) >> 2));
  84.  
  85.            
  86.             do {
  87.                 c4 = str.charCodeAt(i++) & 0xff;
  88.                 if (c4 == 61)
  89.                     return out;
  90.                 c4 = base64decodechars[c4];
  91.             } while (i < len && c4 == -1);
  92.             if (c4 == -1)
  93.                 break;
  94.             out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
  95.         }
  96.         return out;
  97.     }
  98.  
  99.     function utf16to8(str) {
  100.         var out, i, len, c;
  101.         out = "";
  102.         len = str.length;
  103.         for (i = 0; i < len; i++) {
  104.             c = str.charCodeAt(i);
  105.             if ((c >= 0x0001) && (c <= 0x007f)) {
  106.                 out += str.charAt(i);
  107.             } else if (c > 0x07ff) {
  108.                 out += String.fromCharCode(0xe0 | ((c >> 12) & 0x0f));
  109.                 out += String.fromCharCode(0x80 | ((c >> 6) & 0x3f));
  110.                 out += String.fromCharCode(0x80 | ((c >> 0) & 0x3f));
  111.             } else {
  112.                 out += String.fromCharCode(0xc0 | ((c >> 6) & 0x1f));
  113.                 out += String.fromCharCode(0x80 | ((c >> 0) & 0x3f));
  114.             }
  115.         }
  116.         return out;
  117.     }
  118.  
  119.     function utf8to16(str) {
  120.         var out, i, len, c;
  121.         var char2, char3;
  122.  
  123.         out = "";
  124.         len = str.length;
  125.         i = 0;
  126.         while (i < len) {
  127.             c = str.charCodeAt(i++);
  128.             switch (c >> 4) {
  129.                 case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
  130.                     // 0xxxxxxx
  131.                     out += str.charAt(i - 1);
  132.                     break;
  133.                 case 12: case 13:
  134.                     // 110x xxxx   10xx xxxx
  135.                     char2 = str.charCodeAt(i++);
  136.                     out += String.fromCharCode(((c & 0x1f) << 6) | (char2 & 0x3f));
  137.                     break;
  138.                 case 14:
  139.                     // 1110 xxxx  10xx xxxx  10xx xxxx
  140.                     char2 = str.charCodeAt(i++);
  141.                     char3 = str.charCodeAt(i++);
  142.                     out += String.fromCharCode(((c & 0x0f) << 12) |
  143.                        ((char2 & 0x3f) << 6) |
  144.                        ((char3 & 0x3f) << 0));
  145.                     break;
  146.             }
  147.         }
  148.  
  149.         return out;
  150.     }
  151.  
  152.     function doit() {
  153.         var f = document.f
  154.         f.output.value = base64encode(utf16to8(f.source.value))
  155.         f.decode.value = utf8to16(base64decode(f.output.value))
  156.     }
  157. </script>
  158. </head>
  159. <body style="padding-left:50px;">
  160. <h1 align="center">base64编码加密解密</h1>
  161. <form name="f">
  162.   原码<br>
  163.   <textarea name="source" rows=4 cols=60 wrap="soft"></textarea>
  164.   <br>
  165.   <br>
  166.   base64 encode<br>
  167.   <textarea name="output" rows=4 cols=60 wrap="soft"></textarea>
  168.   <br>
  169.   <br>
  170.   base64 decode<br>
  171.   <textarea name="decode" rows=4 cols=60 wrap="soft"></textarea>
  172.   <br>
  173.   <br>
  174.   <input type=button value="转换" onClick="doit()">
  175. </form>
  176. </body>
  177. </html>
  178.  

Reply to "Base64 encoding encryption and decryption"

Here you can reply to the paste above



base64 encode


base64 decode


captcha

https://burned.cc - Burn After Reading Website