发新话题 回复该主题

base64encode JavaScript怎么操作加密 [复制链接]

1#
  1. /**
  2. *
  3. *  Base64 encode / decode
  4. *  http://www.webtoolkit.info/
  5. *
  6. **/
  7. var Base64 = {

  8. // private property
  9. _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

  10. // public method for encoding
  11. encode : function (input) {
  12.     var output = "";
  13.     var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  14.     var i = 0;

  15.     input = Base64._utf8_encode(input);

  16.     while (i < input.length) {

  17.         chr1 = input.charCodeAt(i++);
  18.         chr2 = input.charCodeAt(i++);
  19.         chr3 = input.charCodeAt(i++);

  20.         enc1 = chr1 >> 2;
  21.         enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  22.         enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  23.         enc4 = chr3 & 63;

  24.         if (isNaN(chr2)) {
  25.             enc3 = enc4 = 64;
  26.         } else if (isNaN(chr3)) {
  27.             enc4 = 64;
  28.         }

  29.         output = output +
  30.         this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
  31.         this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

  32.     }

  33.     return output;
  34. },

  35. // public method for decoding
  36. decode : function (input) {
  37.     var output = "";
  38.     var chr1, chr2, chr3;
  39.     var enc1, enc2, enc3, enc4;
  40.     var i = 0;

  41.     input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

  42.     while (i < input.length) {

  43.         enc1 = this._keyStr.indexOf(input.charAt(i++));
  44.         enc2 = this._keyStr.indexOf(input.charAt(i++));
  45.         enc3 = this._keyStr.indexOf(input.charAt(i++));
  46.         enc4 = this._keyStr.indexOf(input.charAt(i++));

  47.         chr1 = (enc1 << 2) | (enc2 >> 4);
  48.         chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  49.         chr3 = ((enc3 & 3) << 6) | enc4;

  50.         output = output + String.fromCharCode(chr1);

  51.         if (enc3 != 64) {
  52.             output = output + String.fromCharCode(chr2);
  53.         }
  54.         if (enc4 != 64) {
  55.             output = output + String.fromCharCode(chr3);
  56.         }

  57.     }

  58.     output = Base64._utf8_decode(output);

  59.     return output;

  60. },

  61. // private method for UTF-8 encoding
  62. _utf8_encode : function (string) {
  63.     string = string.replace(/\r\n/g,"\n");
  64.     var utftext = "";

  65.     for (var n = 0; n < string.length; n++) {

  66.         var c = string.charCodeAt(n);

  67.         if (c < 128) {
  68.             utftext += String.fromCharCode(c);
  69.         }
  70.         else if((c > 127) && (c < 2048)) {
  71.             utftext += String.fromCharCode((c >> 6) | 192);
  72.             utftext += String.fromCharCode((c & 63) | 128);
  73.         }
  74.         else {
  75.             utftext += String.fromCharCode((c >> 12) | 224);
  76.             utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  77.             utftext += String.fromCharCode((c & 63) | 128);
  78.         }

  79.     }

  80.     return utftext;
  81. },

  82. // private method for UTF-8 decoding
  83. _utf8_decode : function (utftext) {
  84.     var string = "";
  85.     var i = 0;
  86.     var c = c1 = c2 = 0;

  87.     while ( i < utftext.length ) {

  88.         c = utftext.charCodeAt(i);

  89.         if (c < 128) {
  90.             string += String.fromCharCode(c);
  91.             i++;
  92.         }
  93.         else if((c > 191) && (c < 224)) {
  94.             c2 = utftext.charCodeAt(i+1);
  95.             string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
  96.             i += 2;
  97.         }
  98.         else {
  99.             c2 = utftext.charCodeAt(i+1);
  100.             c3 = utftext.charCodeAt(i+2);
  101.             string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
  102.             i += 3;
  103.         }

  104.     }

  105.     return string;
  106. }

  107. }
复制代码



怎么使用这个加密?
最后编辑xunwai 最后编辑于 2018-02-22 20:10:18
分享 转发
TOP
2#

js写完函数,要做函数外执行一下,比如在后面一行加上text();,请参数红色示例
另外在变量加工里内置有base64加密解密功能,不需要调用js


TOP
发新话题 回复该主题