jquery.base64.js支持中文
jquery.base64.js支持中文
夏碌冬藏 发表于8个月前
jquery.base64.js支持中文
  • 发表于 8个月前
  • 阅读 35
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

摘要: jquery支持中文的base64插件

@阿阮 在https://my.oschina.net/aruan/blog/418980写道支持中文,但是缺少了东西“"use strict";”在测试时,发现有很多关于jquery.base64.js的库,最后发现@阿阮 兄是从https://github.com/carlo/jquery-base64这里修改来的,现在放出可用的源码。

    使用方法:

<html>

<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script language="javascript" src="js/jquery-1.11.3.min.js"></script>
    <script language="javascript" src="js/jquery.base64.cn.js"></script>
</head>

<body>
    <input id="path" name="path" type="hidden" value="haha"></input>
    <input id="putcardno01" name="putcardno01" type="text" size="65" value=""></input>
    <br>
    <input onclick="subfunc();" class="btn1" value="提交加密" type="button"></input>
    <br> 加密后:
    <input id="putcardno02" name="putcardno02" type="text" size="65" value=""></input>
    <br>
    <input onclick="subfunc02();" class="btn1" value="提交解密" type="button"></input>
    <br>
    <br>
    <hr>
    <input onclick="subfunc03();" class="btn1" value="提交N次加密" type="button"></input>
    <br> 加密后:
    <input id="putcardno03" name="putcardno03" type="text" size="65" value=""></input>
    <br>
    <input onclick="subfunc04();" class="btn1" value="提交N次解密" type="button"></input>
    <br>
    <br>
    <input onclick="clearrr();" class="btn1" value="清除" type="button"></input>
    <br>
    <textarea id='txt' cols="75" rows="19"></textarea>
</body>
<script language="javascript">
var path = document.getElementById("path").value;

function app(info) {
    $("#txt").val($("#txt").val() + '\n' + info);
}



function subfunc() {
    var put1 = $.trim($("#putcardno01").val());
    // var estxt=$.base64.encode(put1);
    //var estxt=$.base64.btoa(put1);

    var estxt = encodeBase64(put1);
    $("#putcardno02").val(estxt);
    app("加密后[" + estxt + "]");
}



function subfunc02() {
    var put1 = $.trim($("#putcardno02").val());
    //var estxt=$.base64.decode(put1);
    //var estxt=$.base64.atob(put1);

    var estxt = decodeBase64(put1);
    app("解密后[" + estxt + "]");
}


//////////////////////////////////////////
var numTimes = 5;

function subfunc03() {
    var put1 = $.trim($("#putcardno01").val());
    // var estxt=$.base64.encode(put1);
    //var estxt=$.base64.btoa(put1);
    //estxt=$.base64.btoa(estxt);

    estxt = encodeBase64(put1, numTimes);

    $("#putcardno03").val(estxt);
    app(numTimes + "次加密后[" + estxt + "]");
}



function subfunc04() {
    var put1 = $.trim($("#putcardno03").val());
    //var estxt=$.base64.decode(put1);
    //var estxt=$.base64.atob(put1);
    //estxt=$.base64.atob(estxt);


    estxt = decodeBase64(put1, numTimes);
    app(numTimes + "次解密后[" + estxt + "]");
}


function clearrr() {
    $("#putcardno02").val("");
    $("#putcardno03").val("");
    $("#putcardno04").val("");
    $("#txt").val("");
}













//加密方法。没有过滤首尾空格,即没有trim.
//加密可以加密N次,对应解密N次就可以获取明文
function encodeBase64(mingwen, times) {
    var code = "";
    var num = 1;
    if (typeof times == 'undefined' || times == null || times == "") {
        num = 1;
    } else {
        var vt = times + "";
        num = parseInt(vt);
    }

    if (typeof mingwen == 'undefined' || mingwen == null || mingwen == "") {

    } else {
        code = mingwen;
        for (var i = 0; i < num; i++) {
            code = $.base64.encode(code);
            // code = $.base64({data:code,type:0});
        }
    }
    return code;
}


//解密方法。没有过滤首尾空格,即没有trim
//加密可以加密N次,对应解密N次就可以获取明文
function decodeBase64(mi, times) {
    var mingwen = "";
    var num = 1;
    if (typeof times == 'undefined' || times == null || times == "") {
        num = 1;
    } else {
        var vt = times + "";
        num = parseInt(vt);
    }


    if (typeof mi == 'undefined' || mi == null || mi == "") {

    } else {
        mingwen = mi;
        for (var i = 0; i < num; i++) {
            mingwen = $.base64.decode(mingwen);
            // mingwen = $.base64({data:mingwen,type:1,unicode:false});
        }
    }
    return mingwen;
}


/*
测试
输入 suolong2014version


加密后[c3VvbG9uZzIwMTR2ZXJzaW9u]
解密后[suolong2014version]
5次加密后[VjFod1QxWXlVblJUYTJoUVYwWmFhRnBYZEhOTk1WSlhWV3hPVG1KSVFscFZNalYzWVVaYU5tSkVSVDA9]
5次解密后[suolong2014version]
*/
</script>

    源码:

//支持中文的jquery.base64.js插件
"use strict";
jQuery.base64 = (function($) {

    var _PADCHAR = "=",
        _ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
        _VERSION = "1.1"; //Mr. Ruan fix to 1.1 to support asian char(utf8)


    function _getbyte64(s, i) {
        // This is oddly fast, except on Chrome/V8.
        // Minimal or no improvement in performance by using a
        // object with properties mapping chars to value (eg. 'A': 0)

        var idx = _ALPHA.indexOf(s.charAt(i));

        if (idx === -1) {
            throw "Cannot decode base64";
        }

        return idx;
    }

    function _decode_chars(y, x) {
        while (y.length > 0) {
            var ch = y[0];
            if (ch < 0x80) {
                y.shift();
                x.push(String.fromCharCode(ch));
            } else if ((ch & 0x80) == 0xc0) {
                if (y.length < 2) break;
                ch = y.shift();
                var ch1 = y.shift();
                x.push(String.fromCharCode(((ch & 0x1f) << 6) + (ch1 & 0x3f)));
            } else {
                if (y.length < 3) break;
                ch = y.shift();
                var ch1 = y.shift();
                var ch2 = y.shift();
                x.push(String.fromCharCode(((ch & 0x0f) << 12) + ((ch1 & 0x3f) << 6) + (ch2 & 0x3f)));
            }
        }
    }

    function _decode(s) {
        var pads = 0,
            i,
            b10,
            imax = s.length,
            x = [],
            y = [];

        s = String(s);

        if (imax === 0) {
            return s;
        }

        if (imax % 4 !== 0) {
            throw "Cannot decode base64";
        }

        if (s.charAt(imax - 1) === _PADCHAR) {
            pads = 1;

            if (s.charAt(imax - 2) === _PADCHAR) {
                pads = 2;
            }

            // either way, we want to ignore this last block
            imax -= 4;
        }

        for (i = 0; i < imax; i += 4) {
            var ch1 = _getbyte64(s, i);
            var ch2 = _getbyte64(s, i + 1);
            var ch3 = _getbyte64(s, i + 2);
            var ch4 = _getbyte64(s, i + 3);

            b10 = (_getbyte64(s, i) << 18) | (_getbyte64(s, i + 1) << 12) | (_getbyte64(s, i + 2) << 6) | _getbyte64(s, i + 3);
            y.push(b10 >> 16);
            y.push((b10 >> 8) & 0xff);
            y.push(b10 & 0xff);
            _decode_chars(y, x);
        }
        switch (pads) {
            case 1:
                b10 = (_getbyte64(s, i) << 18) | (_getbyte64(s, i + 1) << 12) | (_getbyte64(s, i + 2) << 6);
                y.push(b10 >> 16);
                y.push((b10 >> 8) & 0xff);
                break;

            case 2:
                b10 = (_getbyte64(s, i) << 18) | (_getbyte64(s, i + 1) << 12);
                y.push(b10 >> 16);
                break;
        }
        _decode_chars(y, x);
        if (y.length > 0) throw "Cannot decode base64";
        return x.join("");
    }


    function _get_chars(ch, y) {
        if (ch < 0x80) y.push(ch);
        else if (ch < 0x800) {
            y.push(0xc0 + ((ch >> 6) & 0x1f));
            y.push(0x80 + (ch & 0x3f));
        } else {
            y.push(0xe0 + ((ch >> 12) & 0xf));
            y.push(0x80 + ((ch >> 6) & 0x3f));
            y.push(0x80 + (ch & 0x3f));
        }
    }



    function _encode(s) {
        if (arguments.length !== 1) {
            throw "SyntaxError: exactly one argument required";
        }

        s = String(s);
        if (s.length === 0) {
            return s;
        }

        //s = _encode_utf8(s);
        var i,
            b10,
            y = [],
            x = [],
            len = s.length;
        i = 0;
        while (i < len) {
            _get_chars(s.charCodeAt(i), y);
            while (y.length >= 3) {
                var ch1 = y.shift();
                var ch2 = y.shift();
                var ch3 = y.shift();
                b10 = (ch1 << 16) | (ch2 << 8) | ch3;
                x.push(_ALPHA.charAt(b10 >> 18));
                x.push(_ALPHA.charAt((b10 >> 12) & 0x3F));
                x.push(_ALPHA.charAt((b10 >> 6) & 0x3f));
                x.push(_ALPHA.charAt(b10 & 0x3f));
            }
            i++;
        }


        switch (y.length) {
            case 1:
                var ch = y.shift();
                b10 = ch << 16;
                x.push(_ALPHA.charAt(b10 >> 18) + _ALPHA.charAt((b10 >> 12) & 0x3F) + _PADCHAR + _PADCHAR);
                break;

            case 2:
                var ch1 = y.shift();
                var ch2 = y.shift();
                b10 = (ch1 << 16) | (ch2 << 8);
                x.push(_ALPHA.charAt(b10 >> 18) + _ALPHA.charAt((b10 >> 12) & 0x3F) + _ALPHA.charAt((b10 >> 6) & 0x3f) + _PADCHAR);
                break;
        }

        return x.join("");
    }


    return {
        decode: _decode,
        encode: _encode,
        VERSION: _VERSION
    };
}(jQuery));

 

 

共有 人打赏支持
粉丝 8
博文 34
码字总数 8055
×
夏碌冬藏
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: