python兼容js base64_jquery.base64.js支持中文

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

使用方法:


加密后:



加密后:

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]

*/

源码:

//支持中文的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));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
127.0.0.1 - - [14/May/2023 00:58:30] "GET / HTTP/1.1" 200 - 127.0.0.1 - - [14/May/2023 00:58:30] "GET /static/sweetalert/dist/sweetalert.css HTTP/1.1" 404 - 127.0.0.1 - - [14/May/2023 00:58:30] "GET /static/sweetalert/dist/sweetalert.min.js HTTP/1.1" 404 - [2023-05-14 00:58:32,535] ERROR in app: Exception on /login [GET] Traceback (most recent call last): File "F:\python\lib\site-packages\flask\app.py", line 2311, in wsgi_app response = self.full_dispatch_request() File "F:\python\lib\site-packages\flask\app.py", line 1834, in full_dispatch_request rv = self.handle_user_exception(e) File "F:\python\lib\site-packages\flask\app.py", line 1737, in handle_user_exception reraise(exc_type, exc_value, tb) File "F:\python\lib\site-packages\flask\_compat.py", line 36, in reraise raise value File "F:\python\lib\site-packages\flask\app.py", line 1832, in full_dispatch_request rv = self.dispatch_request() File "F:\python\lib\site-packages\flask\app.py", line 1818, in dispatch_request return self.view_functions[rule.endpoint](**req.view_args) File "G:\python\course_select_system-master\app\view.py", line 50, in login return render_template('login.html') File "F:\python\lib\site-packages\flask\templating.py", line 135, in render_template context, ctx.app) File "F:\python\lib\site-packages\flask\templating.py", line 117, in _render rv = template.render(context) File "F:\python\lib\site-packages\jinja2\asyncsupport.py", line 76, in render return original_render(self, *args, **kwargs) File "F:\python\lib\site-packages\jinja2\environment.py", line 1008, in render return self.environment.handle_exception(exc_info, True) File "F:\python\lib\site-packages\jinja2\environment.py", line 780, in handle_exception reraise(exc_type, exc_value, tb) File "F:\python\lib\site-packages\jinja2\_compat.py", line 37, in reraise raise value.with_traceback(tb) File "G:\python\course_select_system-master\app\templates\login.html", line 1, in top-level template code {% extends 'bootstrap/base.html' %} File "F:\python\lib\site-packages\flask_bootstrap\templates\bootstrap\base.html", line 1, in top-level template code {% block doc -%} File "F:\python\lib\site-packages\flask_bootstrap\templates\bootstrap\base.html", line 4, in block "doc" {%- block html %} File "F:\python\lib\site-packages\flask_bootstrap\templates\bootstrap\base.html", line 20, in block "html" {% block body -%} File "F:\python\lib\site-packages\flask_bootstrap\templates\bootstrap\base.html", line 26, in block "body" {% block scripts %} File "F:\python\lib\site-packages\flask_bootstrap\templates\bootstrap\base.html", line 27, in block "scripts" <script src="{{bootstrap_find_resource('jquery.js', cdn='jquery')}}"></script> jinja2.exceptions.UndefinedError: 'bootstrap_find_resource' is undefined 127.0.0.1 - - [14/May/2023 00:58:32] "GET /login HTTP/1.1" 500 -
05-14

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值