js密码64加密

可以在客户端对密码进行简单的64位加密,服务端对应使用64位解密即可。

  1 /**
  2 *
  3 *  Base64 encode / decode
  4 *
  5 *  @author 
  6 
  7 *  @date   
  8 
  9 *  @email  
 10 */
 11  
 12 function Base64() {
 13  
 14     // private property
 15     _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
 16  
 17     // public method for encoding
 18     this.encode = function (input) {
 19         var output = "";
 20         var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
 21         var i = 0;
 22         input = _utf8_encode(input);
 23         while (i < input.length) {
 24             chr1 = input.charCodeAt(i++);
 25             chr2 = input.charCodeAt(i++);
 26             chr3 = input.charCodeAt(i++);
 27             enc1 = chr1 >> 2;
 28             enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
 29             enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
 30             enc4 = chr3 & 63;
 31             if (isNaN(chr2)) {
 32                 enc3 = enc4 = 64;
 33             } else if (isNaN(chr3)) {
 34                 enc4 = 64;
 35             }
 36             output = output +
 37             _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
 38             _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
 39         }
 40         return output;
 41     }
 42  
 43 
 44  
 45     // private method for UTF-8 encoding
 46     _utf8_encode = function (string) {
 47         string = string.replace(/
/g,"
");
 48         var utftext = "";
 49         for (var n = 0; n < string.length; n++) {
 50             var c = string.charCodeAt(n);
 51             if (c < 128) {
 52                 utftext += String.fromCharCode(c);
 53             } else if((c > 127) && (c < 2048)) {
 54                 utftext += String.fromCharCode((c >> 6) | 192);
 55                 utftext += String.fromCharCode((c & 63) | 128);
 56             } else {
 57                 utftext += String.fromCharCode((c >> 12) | 224);
 58                 utftext += String.fromCharCode(((c >> 6) & 63) | 128);
 59                 utftext += String.fromCharCode((c & 63) | 128);
 60             }
 61         }
 62         return utftext;
 63 }
 64 
 65 //只使用加密方法,解密方法暂时注释,后期删除
 66 // public method for decoding
 67 this.decode = function (input) {
 68     var output = "";
 69     var chr1, chr2, chr3;
 70     var enc1, enc2, enc3, enc4;
 71     var i = 0;
 72     input = input.replace(/[^A-Za-z0-9+/=]/g, "");
 73     while (i < input.length) {
 74         enc1 = _keyStr.indexOf(input.charAt(i++));
 75         enc2 = _keyStr.indexOf(input.charAt(i++));
 76         enc3 = _keyStr.indexOf(input.charAt(i++));
 77         enc4 = _keyStr.indexOf(input.charAt(i++));
 78         chr1 = (enc1 << 2) | (enc2 >> 4);
 79         chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
 80         chr3 = ((enc3 & 3) << 6) | enc4;
 81         output = output + String.fromCharCode(chr1);
 82         if (enc3 != 64) {
 83             output = output + String.fromCharCode(chr2);
 84         }
 85         if (enc4 != 64) {
 86             output = output + String.fromCharCode(chr3);
 87         }
 88     }
 89     output = _utf8_decode(output);
 90     return output;
 91 }
 92  
 93     // private method for UTF-8 decoding
 94     _utf8_decode = function (utftext) {
 95         var string = "";
 96         var i = 0;
 97         var c = c1 = c2 = 0;
 98         while ( i < utftext.length ) {
 99             c = utftext.charCodeAt(i);
100             if (c < 128) {
101                 string += String.fromCharCode(c);
102                 i++;
103             } else if((c > 191) && (c < 224)) {
104                 c2 = utftext.charCodeAt(i+1);
105                 string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
106                 i += 2;
107             } else {
108                 c2 = utftext.charCodeAt(i+1);
109                 c3 = utftext.charCodeAt(i+2);
110                 string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
111                 i += 3;
112             }
113         }
114         return string;
115     }
116 }
View Code

使用方法:

var encoder = new Base64(); //64加密
userPwd = encoder.encode(userPwd);
原文地址:https://www.cnblogs.com/maomao999/p/3653993.html