javascript base64 encode decode 支持中文
* 字符编码
** 一定要知道数据的字符编码
** 使用utf-8字符编码存储数据
** 使用utf-8字符编码输出数据
* Crypto.js 支持中文
Base64编码说明
Base64编码要求把3个8位字节(3*8=24)转化为4个6位的字节(4*6=24),之后在6位的前面补两个0,形成8位一个字节的形式。
如果剩下的字符不足3个字节,则用0填充,输出字符使用'=',因此编码后输出的文本末尾可能会出现1或2个'='。
为了保证所输出的编码位可读字符,Base64制定了一个编码表,以便进行统一转换。编码表的大小为2^6=64,这也是Base64名称的由来。
* 运算符优先级
* /
%
+ -
<< >>
== ===
| &
|| &&
// http://tool.oschina.net/encrypt?type=3 var CryptoJS = CryptoJS || function(h, o) {
var f = {};
var j = f.lib = {};
var k = j.Base = function() {
function a() {}
return {
extend: function(b) {
a.prototype = this;
var c = new a;
b && c.mixIn(b);
c.$super = this;
return c
},
create: function() {
var a = this.extend();
a.init.apply(a, arguments);
return a
},
init: function() {},
mixIn: function(a) {
for (var c in a)
a.hasOwnProperty(c) && (this[c] = a[c]);
a.hasOwnProperty("toString") && (this.toString = a.toString)
},
clone: function() {
return this.$super.extend(this)
}
}
}();
var i = j.WordArray = k.extend({
init: function(a, b) {
a = this.words = a || [];
this.sigBytes = b != o ? b : 4 * a.length
},
toString: function(a) {
return (a || p).stringify(this)
},
concat: function(a) {
var b = this.words
, c = a.words
, d = this.sigBytes
, a = a.sigBytes;
this.clamp();
if (d % 4)
for (var e = 0; e < a; e++)
b[d + e >>> 2] |= (c[e >>> 2]
>>> 24 - 8 * (e % 4) & 255)
<< 24 - 8 * ((d + e) % 4);
else if (65535 < c.length)
for (e = 0; e < a; e += 4)
b[d + e >>> 2] = c[e >>> 2];
else
b.push.apply(b, c);
this.sigBytes += a;
return this
},
clamp: function() {
var a = this.words
, b = this.sigBytes;
a[b >>> 2] &= 4294967295 << 32 - 8 * (b % 4);
a.length = h.ceil(b / 4)
},
clone: function() {
var a = k.clone.call(this);
a.words = this.words.slice(0);
return a
},
random: function(a) {
for (var b = [], c = 0; c < a; c += 4)
b.push(4294967296 * h.random() | 0);
return i.create(b, a)
}
}), l = f.enc = {},
p = l.Hex = {
stringify: function(a) {
for (var b = a.words, a = a.sigBytes, c = [], d = 0; d < a; d++) {
var e = b[d >>> 2] >>> 24 - 8 * (d % 4) & 255;
c.push((e >>> 4).toString(16));
c.push((e & 15).toString(16))
}
return c.join("")
},
parse: function(a) {
for (var b = a.length, c = [], d = 0; d < b; d += 2)
c[d >>> 3] |= parseInt(a.substr(d, 2), 16) << 24 - 4 * (d % 8);
return i.create(c, b / 2)
}
}, n = l.Latin1 = {
stringify: function(a) {
for (var b = a.words, a = a.sigBytes, c = [], d = 0; d < a; d++)
c.push(String.fromCharCode(b[d >>> 2] >>> 24 - 8 * (d % 4) & 255));
return c.join("")
},
parse: function(a) {
for (var b = a.length, c = [], d = 0; d < b; d++)
c[d >>> 2] |= (a.charCodeAt(d) & 255) << 24 - 8 * (d % 4);
return i.create(c, b)
}
}, q = l.Utf8 = {
stringify: function(a) {
try {
return decodeURIComponent(escape(n.stringify(a)))
} catch (b) {
throw Error("Malformed UTF-8 data");
}
},
parse: function(a) {
return n.parse(unescape(encodeURIComponent(a)))
}
}, m = j.BufferedBlockAlgorithm = k.extend({
reset: function() {
this._data = i.create();
this._nDataBytes = 0
},
_append: function(a) {
"string" == typeof a && (a = q.parse(a));
this._data.concat(a);
this._nDataBytes += a.sigBytes
},
_process: function(a) {
var b = this._data
, c = b.words
, d = b.sigBytes
, e = this.blockSize
, f = d / (4 * e)
, f = a ? h.ceil(f) : h.max((f | 0) - this._minBufferSize, 0)
, a = f * e
, d = h.min(4 * a, d);
if (a) {
for (var g = 0; g < a; g += e)
this._doProcessBlock(c, g);
g = c.splice(0, a);
b.sigBytes -= d
}
return i.create(g, d)
},
clone: function() {
var a = k.clone.call(this);
a._data = this._data.clone();
return a
},
_minBufferSize: 0
});
j.Hasher = m.extend({
init: function() {
this.reset()
},
reset: function() {
m.reset.call(this);
this._doReset()
},
update: function(a) {
this._append(a);
this._process();
return this
},
finalize: function(a) {
a && this._append(a);
this._doFinalize();
return this._hash
},
clone: function() {
var a = m.clone.call(this);
a._hash = this._hash.clone();
return a
},
blockSize: 16,
_createHelper: function(a) {
return function(b, c) {
return a.create(c).finalize(b)
}
},
_createHmacHelper: function(a) {
return function(b, c) {
return r.HMAC.create(a, c).finalize(b)
}
}
});
var r = f.algo = {};
return f
}(Math); (function() {
var h = CryptoJS
, i = h.lib.WordArray;
h.enc.Base64 = {
stringify: function(b) {
var e = b.words
, f = b.sigBytes
, c = this._map;
b.clamp();
for (var b = [], a = 0; a < f; a += 3)
for (var d = (e[a >>> 2] >>> 24 - 8 * (a % 4) & 255) << 16
| (e[a + 1 >>> 2] >>> 24 - 8 * ((a + 1) % 4) & 255) << 8
| e[a + 2 >>> 2] >>> 24 - 8 * ((a + 2) % 4) & 255,
g = 0;
4 > g && a + 0.75 * g < f;
g++)
b.push(c.charAt(d >>> 6 * (3 - g) & 63));
if (e = c.charAt(64))
for (; b.length % 4; )
b.push(e);
return b.join("")
},
parse: function(b) {
var b = b.replace(/\s/g, "")
, e = b.length
, f = this._map
, c = f.charAt(64);
c && (c = b.indexOf(c),
-1 != c && (e = c));
for (var c = [], a = 0, d = 0; d < e; d++)
if (d % 4) {
var g = f.indexOf(b.charAt(d - 1)) << 2 * (d % 4)
, h = f.indexOf(b.charAt(d)) >>> 6 - 2 * (d % 4);
c[a >>> 2] |= (g | h) << 24 - 8 * (a % 4);
a++
}
return i.create(c, a)
},
_map: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
}
}
)();
test:
var str=CryptoJS.enc.Utf8.parse("这是一个测试用例");
var base64=CryptoJS.enc.Base64.stringify(str); console.log(base64); var words = CryptoJS.enc.Base64.parse(base64);
console.log(words.toString(CryptoJS.enc.Utf8));
* 仅支持ascii版本
https://blog.csdn.net/chenxiaohua/article/details/4084602
(function() {
var g_pCodes = (function() {
var s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz01234567890+/="
var a = new Array(s.length);
for (var i = 0, n = a.length; i < n; i++) {
a[i] = s.charCodeAt(i);
}
return a;
})(); // 256
var g_pMap = [
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255,
255, 254, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6,
7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255,
255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
49, 50, 51, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255
]; var encode = function(s) {
var pIn = s.split('').map(function(c) {
return c.charCodeAt(0);
}); var uInLen = s.length;
var pIndex = 0;
var len2 = Math.floor((uInLen+2)/3)<<2;
var pOut = [], p = 0;
var a, b, c; for (var i = 0, len = 3 * Math.floor(uInLen/3); i < len; i+=3) {
a = pIn[i], b = pIn[i+1], c = pIn[i+2];
pOut.push( g_pCodes[ a >> 2 ] );
pOut.push( g_pCodes[((a & 3) << 4) + (b>>4)] );
pOut.push( g_pCodes[((b & 0xf) << 2) + (c>>6)] );
pOut.push( g_pCodes[ c & 0x3f]);
} a = pIn[i], b = (i+1)<uInLen ? pIn[i+1]:0, c = 0;
if (i < uInLen) {
pOut.push( g_pCodes[a>>2]);
pOut.push( g_pCodes[ ((a & 3)<<4) + (b >> 4)] );
pOut.push( ((i+1)<uInLen) ?
g_pCodes[((b & 0xf) << 2)+(c>>6)] :
'='.charCodeAt(0));
pOut.push( '='.charCodeAt(0) );
} return pOut.map(function(c) {
return String.fromCharCode(c);
}).join('');
}; var decode = function(s) {
var x, y, z, t, c, g = 3;
var strIn = s.split('').map(function(c) {
return c.charCodeAt(0);
});
var out = []; for (x = y = z = t = 0; x < strIn.length; x++) {
c = g_pMap[strIn[x]];
if (c===255) {continue; }
if (c===254) {c = 0, g--;} t = (t << 6) | c; if (++y === 4) {
out.push( (t >> 16) & 255 ), z++;
if (g > 1) {
out.push((t >> 8) & 255), z++;
}
if (g > 2) {
out.push(t & 255), z++;
}
y = t = 0;
}
}
return out.map(function(c) {
return String.fromCharCode(c);
}).join('');
}; return base64 = {
encode: encode,
decode: decode
} })(); // test @website: http://tool.oschina.net/encrypt?type=3
var s = "this is a example";
var enc = base64.encode(s);
console.log(enc);
console.log(base64.decode(enc));
javascript base64 encode decode 支持中文的更多相关文章
- node_nibbler:自定义Base32/base64 encode/decode库
https://github.com/mattrobenolt/node_nibbler 可以将本源码复制到自己需要的JS文件中,比如下面这个文件,一个基于BASE64加密请求参数的REST工具: [ ...
- Base64 encode/decode large file
转载:http://www.cnblogs.com/jzywh/archive/2008/04/20/base64_encode_large_file.html The class System.Co ...
- BASE64 Encode Decode
package com.humi.encryption; import java.io.IOException; import java.io.UnsupportedEncodingException ...
- Javascript Base64编码与解码
原文:[转]Javascript Base64编码与解码 <html> <head> <META HTTP-EQUIV="MSThemeCompatible&q ...
- python中文处理之encode/decode函数
python中文处理相信迷惑过不少同学.下面说说python2/3的encode和decode函数. python2中,使用decode()和encode()来进行解码和编码,以unicode类型作为 ...
- javaScript生成二维码(支持中文,生成logo)
资料搜索 选择star最多的两个 第一个就是用的比较多的jquery.qrcode.js(但不支持中文,不能带logo)啦,第二个支持ie6+,支持中文,根据第二个源代码,使得,jquery.qrco ...
- javascript base64 编码,兼容ie6789
用Javascript进行base64编码,在高版本的IE浏览器(IE9以上版本),和firefox,chrome浏览器里是非常方便的.这些浏览器的window对象中内置了base64的编码和解码方法 ...
- Java前端Rsa公钥加密,后端Rsa私钥解密(目前还不支持中文加密解密,其他都行)
Base64工具类,可以让rsa编码的乱码变成一串字符序列 package com.utils; import java.io.ByteArrayInputStream; import java.io ...
- python编码encode decode(解惑)
关于python 字符串编码一直没有搞清楚,今天总结了一下. Python 字符串类型 Python有两种字符串类型:str 与 unicode. 字符串实例 # -*- coding: utf-8 ...
随机推荐
- 面对对象4 Mixins机制 内置方法 反射 异常
Mixins机制 为什么要有:子类继承父类的时候,可能会碰到需要继承多个父类的情况,那么继承关系也分主类和辅类,既保持主类的功能,也有辅类的功能. 命名方式,我们需要将主类和辅类区分开来,python ...
- Redis应用场景及缓存问题
1.应用场景 (1) 缓存 缓存机制几乎在所有的大型网站都有使用,合理地使用缓存不仅可以加快数据的访问速度,而且能够有效地降低后端数据源的压力.Redis 提供了键值过期时间设置,并且也提供了灵活 ...
- Blazor+Dapr+K8s微服务之开发环境调试
1 安装Dapr开发调试环境 1.1 Dapr 完整安装模式不支持开发调试 在上一篇随笔<Blazor+Dapr+K8s微服务之服务调用>中,我们通过为每个 ...
- NOIP 模拟 9 数颜色
题解 一道裸的数据结构题 正解是排序 \(+\) 二分,但是这怎么能有动态开点线段树好写呢? 于是我就打了暴力,骗了五十分. 对于每种颜色,我们在下标上开一颗线段树,对于交换若颜色相同则跳过,否则直接 ...
- 【软件工具】Git 使用总结
本地库就是由 对象 和 引用 构成的,或者叫 Repositories;下面是我整理的常用 Git 命令清单.几个专用名词的译名如下. Workspace:工作区 Index / Stage:暂存区 ...
- centos7-同步时间
yum install -y ntp ntpdate ntpdate -u cn.pool.ntp.org # 阿里云ntp ntpdate ntp1.aliyun.com 但这样的同步,只是强制性的 ...
- tomcat9配置https-pfx
下载tomcat9 wget https://mirrors.bfsu.edu.cn/apache/tomcat/tomcat-9/v9.0.37/bin/apache-tomcat-9.0.37.t ...
- 安装Ubuntu服务器版 + 远程连接ssh +更换阿里云源
安装Ubuntu服务器版 1.点击 "开启此虚拟机",开始安装. 2.默认选择English,英文版安装,直接按Enter键即可. 3.默认选择"Install Ubun ...
- SpringBoot以jar包部署需要注意的thymeleaf页面映射问题
关于themeleaf映射需要注意的: 1.页面映射 所有静态页面映射的时候,mapping后面要以/开头(最好),不以/开头也行 但是return 后面路径不能以/开头:IDE中正常,但是打jar包 ...
- docker安装与配置gitlab详细过程
docker安装与配置gitlab详细过程 1.打开网易镜像中心 https://c.163yun.com/hub#/m/home/ 2.搜索gitlab,获取下载地址.例如:docker pull ...