• base64加密


    base64.js文件:

     1 function Base64() {
     2     var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
     3     this.encode = function(input) {
     4         var output = "";
     5         var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
     6         var i = 0;
     7         input = _utf8_encode(input);
     8         while (i < input.length) {
     9             chr1 = input.charCodeAt(i++);
    10             chr2 = input.charCodeAt(i++);
    11             chr3 = input.charCodeAt(i++);
    12             enc1 = chr1 >> 2;
    13             enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
    14             enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
    15             enc4 = chr3 & 63;
    16             if (isNaN(chr2)) {
    17                 enc3 = enc4 = 64;
    18             } else if (isNaN(chr3)) {
    19                 enc4 = 64;
    20             }
    21             output = output +
    22                 _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
    23                 _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
    24         }
    25         return output;
    26     }
    27     this.decode = function(input) {
    28         var output = "";
    29         var chr1, chr2, chr3;
    30         var enc1, enc2, enc3, enc4;
    31         var i = 0;
    32         input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
    33         while (i < input.length) {
    34             enc1 = _keyStr.indexOf(input.charAt(i++));
    35             enc2 = _keyStr.indexOf(input.charAt(i++));
    36             enc3 = _keyStr.indexOf(input.charAt(i++));
    37             enc4 = _keyStr.indexOf(input.charAt(i++));
    38             chr1 = (enc1 << 2) | (enc2 >> 4);
    39             chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
    40             chr3 = ((enc3 & 3) << 6) | enc4;
    41             output = output + String.fromCharCode(chr1);
    42             if (enc3 != 64) {
    43                 output = output + String.fromCharCode(chr2);
    44             }
    45             if (enc4 != 64) {
    46                 output = output + String.fromCharCode(chr3);
    47             }
    48         }
    49         output = _utf8_decode(output);
    50         return output;
    51     }
    52     var _utf8_encode = function(string) {
    53         string = string.replace(/\r\n/g, "\n");
    54         var utftext = "";
    55         for (var n = 0; n < string.length; n++) {
    56             var c = string.charCodeAt(n);
    57             if (c < 128) {
    58                 utftext += String.fromCharCode(c);
    59             } else if ((c > 127) && (c < 2048)) {
    60                 utftext += String.fromCharCode((c >> 6) | 192);
    61                 utftext += String.fromCharCode((c & 63) | 128);
    62             } else {
    63                 utftext += String.fromCharCode((c >> 12) | 224);
    64                 utftext += String.fromCharCode(((c >> 6) & 63) | 128);
    65                 utftext += String.fromCharCode((c & 63) | 128);
    66             }
    67         }
    68         return utftext;
    69     }
    70     var _utf8_decode = function(utftext) {
    71         var string = "";
    72         var i = 0;
    73         var c = c1 = c2 = 0;
    74         while (i < utftext.length) {
    75             c = utftext.charCodeAt(i);
    76             if (c < 128) {
    77                 string += String.fromCharCode(c);
    78                 i++;
    79             } else if ((c > 191) && (c < 224)) {
    80                 c2 = utftext.charCodeAt(i + 1);
    81                 string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
    82                 i += 2;
    83             } else {
    84                 c2 = utftext.charCodeAt(i + 1);
    85                 c3 = utftext.charCodeAt(i + 2);
    86                 string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
    87                 i += 3;
    88             }
    89         }
    90         return string;
    91     }
    92 }

    应用

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 
     4 <head>
     5     <meta charset="UTF-8">
     6     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     7     <title>加密</title>
     8     <script src="./base64.js"></script>
     9 </head>
    10 
    11 <body>
    12     <script>
    13         var passWord = "poi~~";
    14         let b = new Base64().encode(passWord);
    15         console.log(b)
    16     </script>
    17 </body>
    18 
    19 </html>

    打印结果:

    function Base64() {
        var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
        this.encode = function(input) {
            var output = "";
            var chr1chr2chr3enc1enc2enc3enc4;
            var i = 0;
            input = _utf8_encode(input);
            while (i < input.length) {
                chr1 = input.charCodeAt(i++);
                chr2 = input.charCodeAt(i++);
                chr3 = input.charCodeAt(i++);
                enc1 = chr1 >> 2;
                enc2 = ((chr1 & 3<< 4| (chr2 >> 4);
                enc3 = ((chr2 & 15<< 2| (chr3 >> 6);
                enc4 = chr3 & 63;
                if (isNaN(chr2)) {
                    enc3 = enc4 = 64;
                } else if (isNaN(chr3)) {
                    enc4 = 64;
                }
                output = output +
                    _keyStr.charAt(enc1+ _keyStr.charAt(enc2+
                    _keyStr.charAt(enc3+ _keyStr.charAt(enc4);
            }
            return output;
        }
        this.decode = function(input) {
            var output = "";
            var chr1chr2chr3;
            var enc1enc2enc3enc4;
            var i = 0;
            input = input.replace(/[^A-Za-z0-9\+\/\=]/g"");
            while (i < input.length) {
                enc1 = _keyStr.indexOf(input.charAt(i++));
                enc2 = _keyStr.indexOf(input.charAt(i++));
                enc3 = _keyStr.indexOf(input.charAt(i++));
                enc4 = _keyStr.indexOf(input.charAt(i++));
                chr1 = (enc1 << 2| (enc2 >> 4);
                chr2 = ((enc2 & 15<< 4| (enc3 >> 2);
                chr3 = ((enc3 & 3<< 6| enc4;
                output = output + String.fromCharCode(chr1);
                if (enc3 != 64) {
                    output = output + String.fromCharCode(chr2);
                }
                if (enc4 != 64) {
                    output = output + String.fromCharCode(chr3);
                }
            }
            output = _utf8_decode(output);
            return output;
        }
        var _utf8_encode = function(string) {
            string = string.replace(/\r\n/g"\n");
            var utftext = "";
            for (var n = 0n < string.lengthn++) {
                var c = string.charCodeAt(n);
                if (c < 128) {
                    utftext += String.fromCharCode(c);
                } else if ((c > 127&& (c < 2048)) {
                    utftext += String.fromCharCode((c >> 6| 192);
                    utftext += String.fromCharCode((c & 63| 128);
                } else {
                    utftext += String.fromCharCode((c >> 12| 224);
                    utftext += String.fromCharCode(((c >> 6& 63| 128);
                    utftext += String.fromCharCode((c & 63| 128);
                }
            }
            return utftext;
        }
        var _utf8_decode = function(utftext) {
            var string = "";
            var i = 0;
            var c = c1 = c2 = 0;
            while (i < utftext.length) {
                c = utftext.charCodeAt(i);
                if (c < 128) {
                    string += String.fromCharCode(c);
                    i++;
                } else if ((c > 191&& (c < 224)) {
                    c2 = utftext.charCodeAt(i + 1);
                    string += String.fromCharCode(((c & 31<< 6| (c2 & 63));
                    i += 2;
                } else {
                    c2 = utftext.charCodeAt(i + 1);
                    c3 = utftext.charCodeAt(i + 2);
                    string += String.fromCharCode(((c & 15<< 12| ((c2 & 63<< 6| (c3 & 63));
                    i += 3;
                }
            }
            return string;
        }
    }
  • 相关阅读:
    《Flutter实战入门》下拉刷新组件的使用方法
    百度HTTPS认证失败解决方法
    unity踩过的音频坑
    如何解决flutter中gradle慢的问题
    如何在ubuntu里面关掉后台的meteor
    ruby生成随机成绩
    Gemfile分平台加载gem
    sublime text2在windows中以命令行启动
    右键添加 CMD 命令提示符
    修复sublime text系统右键菜单
  • 原文地址:https://www.cnblogs.com/123poi/p/14553811.html
Copyright © 2020-2023  润新知