首页 > 其他分享 >base64.js

base64.js

时间:2022-08-14 14:45:44浏览次数:55  
标签:function String cc base64 js var byte

--

/*!
 * jquery.base64.js 0.1 - https://github.com/yckart/jquery.base64.js
 * Makes Base64 en & -decoding simpler as it is.
 *
 * Based upon: https://gist.github.com/Yaffle/1284012
 *
 * Copyright (c) 2012 Yannick Albert (http://yckart.com)
 * Licensed under the MIT license (http://www.opensource.org/licenses/mit-license.php).
 * 2013/02/10
 **/
;(function() {
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
        a256 = '',
        r64 = [256],
        r256 = [256],
        i = 0;
    var UTF8 = {
        /**
         * Encode multi-byte Unicode string into utf-8 multiple single-byte characters
         * (BMP / basic multilingual plane only)
         *
         * Chars in range U+0080 - U+07FF are encoded in 2 chars, U+0800 - U+FFFF in 3 chars
         *
         * @param {String} strUni Unicode string to be encoded as UTF-8
         * @returns {String} encoded string
         */
        encode: function(strUni) {
            // use regular expressions & String.replace callback function for better efficiency
            // than procedural approaches
            var strUtf = strUni.replace(/[\u0080-\u07ff]/g, // U+0080 - U+07FF => 2 bytes 110yyyyy, 10zzzzzz
            function(c) {
                var cc = c.charCodeAt(0);
                return String.fromCharCode(0xc0 | cc >> 6, 0x80 | cc & 0x3f);
            })
            .replace(/[\u0800-\uffff]/g, // U+0800 - U+FFFF => 3 bytes 1110xxxx, 10yyyyyy, 10zzzzzz
            function(c) {
                var cc = c.charCodeAt(0);
                return String.fromCharCode(0xe0 | cc >> 12, 0x80 | cc >> 6 & 0x3F, 0x80 | cc & 0x3f);
            });
            return strUtf;
        },
 
        /**
         * Decode utf-8 encoded string back into multi-byte Unicode characters
         *
         * @param {String} strUtf UTF-8 string to be decoded back to Unicode
         * @returns {String} decoded string
         */
        decode: function(strUtf) {
            // note: decode 3-byte chars first as decoded 2-byte strings could appear to be 3-byte char!
            var strUni = strUtf.replace(/[\u00e0-\u00ef][\u0080-\u00bf][\u0080-\u00bf]/g, // 3-byte chars
            function(c) { // (note parentheses for precence)
                var cc = ((c.charCodeAt(0) & 0x0f) << 12) | ((c.charCodeAt(1) & 0x3f) << 6) | (c.charCodeAt(2) & 0x3f);
                return String.fromCharCode(cc);
            })
            .replace(/[\u00c0-\u00df][\u0080-\u00bf]/g, // 2-byte chars
            function(c) { // (note parentheses for precence)
                var cc = (c.charCodeAt(0) & 0x1f) << 6 | c.charCodeAt(1) & 0x3f;
                return String.fromCharCode(cc);
            });
            return strUni;
        }
    };
 
    while(i < 256) {
        var c = String.fromCharCode(i);
        a256 += c;
        r256[i] = i;
        r64[i] = b64.indexOf(c);
        ++i;
    }
 
    function code(s, discard, alpha, beta, w1, w2) {
        s = String(s);
        var buffer = 0,
            i = 0,
            length = s.length,
            result = '',
            bitsInBuffer = 0;
 
        while(i < length) {
            var c = s.charCodeAt(i);
            c = c < 256 ? alpha[c] : -1;
 
            buffer = (buffer << w1) + c;
            bitsInBuffer += w1;
 
            while(bitsInBuffer >= w2) {
                bitsInBuffer -= w2;
                var tmp = buffer >> bitsInBuffer;
                result += beta.charAt(tmp);
                buffer ^= tmp << bitsInBuffer;
            }
            ++i;
        }
        if(!discard && bitsInBuffer > 0) result += beta.charAt(buffer << (w2 - bitsInBuffer));
        return result;
    }
 
    var Plugin = window.base64 = function(dir, input, encode) {
            return input ? Plugin[dir](input, encode) : dir ? null : this;
        };
 
    Plugin.btoa = Plugin.encode = function(plain, utf8encode) {
        plain = Plugin.raw === false || Plugin.utf8encode || utf8encode ? UTF8.encode(plain) : plain;
        plain = code(plain, false, r256, b64, 8, 6);
        return plain + '===='.slice((plain.length % 4) || 4);
    };
 
    Plugin.atob = Plugin.decode = function(coded, utf8decode) {
        coded = String(coded).split('=');
        var i = coded.length;
        do {--i;
            coded[i] = code(coded[i], true, r64, a256, 6, 8);
        } while (i > 0);
        coded = coded.join('');
        return Plugin.raw === false || Plugin.utf8decode || utf8decode ? UTF8.decode(coded) : coded;
    };
}());

 

//对 123321 进行加密
var str = base64.encode('123321');
console.log(str,'+');

//对 str 进行解密
var dstr = base64.decode(str);
console.log(dstr,'-')

标签:function,String,cc,base64,js,var,byte
From: https://www.cnblogs.com/7qin/p/16585399.html

相关文章

  • 【前端】Windows升级nodejs到最新版本
    1、在GitHub官网,搜索gnvm,选择如下图所示的第一个,下载【gnvm.exe】可执行文件  2、将这个可执行文件放到自己安装的nodejs的根目录下,如C:\ProgramFiles\nodejs ......
  • 2022.8.17 vscode与nodejs
    大前端01、概述和前端工具vscode安装1.1、下载安装VScode下载地址:https://code.visualstudio.com/   1:双击打开vscode安装包2:指定安装目录     ......
  • ExtJS - ExtJS最佳实践
    更新记录转载请注明出处:https://www.cnblogs.com/cqpanda/p/16583543.html2022年8月14日发布。2022年8月13日从笔记迁移到博客。ExtJS教程汇总:https://www.cnblogs......
  • ExtJS - Sencha Inspector
    更新记录转载请注明出处:https://www.cnblogs.com/cqpanda/p/16583533.html2022年8月14日发布。2022年8月13日从笔记迁移到博客。ExtJS教程汇总:https://www.cnblogs......
  • three.js
    import*asTHREEfrom'three';//导入轨道控制器库import{OrbitControls}from"three/examples/jsm/controls/OrbitControls";//导入gltf模型载入库import{......
  • json
    json也称为javascriptobjectnotation格式如下所示:>>>my_json={'name':'Python','address':{'porvince':'吉林','city':['长春','吉林']}}>>>my_json{'name':'Py......
  • js | 防抖&&节流
    正常:事件触发非常频繁,每一次触发,回调函数都会执行,如果时间很短,回调函数中有计算,有可能会出现浏览器卡顿防抖:前面所有的触发都会取消,最后一次的执行在规定的时间......
  • JS之事件下
    event(事件源)在事件中,当前操作的那个元素就是事件源,比如网页元素中input有onclick事件,当点击input发送onclic事件时,事件源就是input。事件源是作为event对象的属性存在的......
  • js中es5及es6部分内容
    ES5及ES6es表示ECMASCript,他是从es3,es5,es6,es5是2009.12月发布的,es6是2015.6月发布的。vue2完全支持es5的(vue3完全支持es6的),react完全支持es6es5的新特性严格模式(对应的......
  • nodemon 调试nodeJS的使用方法
    首先你需要全局安装nodemon ,然后要做一个launch.json的配置:{"version":"0.2.0","configurations":[{"name":"Launchserver.js......