jquery.base64.js支持中文

@阿阮 在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));

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章