coding++:js实现基于Base64的编码及解码
1):引入 jquery.cookie.js
/*!
* jQuery Cookie Plugin v1.4.1
* https://github.com/carhartl/jquery-cookie
*
* Copyright 2006, 2014 Klaus Hartl
* Released under the MIT license
*/
(function(factory) {
if (typeof define === 'function' && define.amd) {
// AMD (Register as an anonymous module)
define([ 'jquery' ], factory);
} else if (typeof exports === 'object') {
// Node/CommonJS
module.exports = factory(require('jquery'));
} else {
// Browser globals
factory(jQuery);
}
}
(function($) { var pluses = /\+/g; function encode(s) {
return config.raw ? s : encodeURIComponent(s);
} function decode(s) {
return config.raw ? s : decodeURIComponent(s);
} function stringifyCookieValue(value) {
return encode(config.json ? JSON.stringify(value)
: String(value));
} function parseCookieValue(s) {
if (s.indexOf('"') === 0) {
// This is a quoted cookie as according to RFC2068,
// unescape...
s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g,
'\\');
} try {
// Replace server-side written pluses with spaces.
// If we can't decode the cookie, ignore it, it's unusable.
// If we can't parse the cookie, ignore it, it's unusable.
s = decodeURIComponent(s.replace(pluses, ' '));
return config.json ? JSON.parse(s) : s;
} catch (e) {
}
} function read(s, converter) {
var value = config.raw ? s : parseCookieValue(s);
return $.isFunction(converter) ? converter(value) : value;
} var config = $.cookie = function(key, value, options) { // Write if (arguments.length > 1 && !$.isFunction(value)) {
options = $.extend({}, config.defaults, options); if (typeof options.expires === 'number') {
var days = options.expires, t = options.expires = new Date();
t.setMilliseconds(t.getMilliseconds() + days * 864e+5);
} return (document.cookie = [
encode(key),
'=',
stringifyCookieValue(value),
options.expires ? '; expires='
+ options.expires.toUTCString() : '', // use
// expires
// attribute,
// max-age
// is
// not
// supported
// by
// IE
options.path ? '; path=' + options.path : '',
options.domain ? '; domain=' + options.domain : '',
options.secure ? '; secure' : '' ].join(''));
} // Read var result = key ? undefined : {},
// To prevent the for loop in the first place assign an empty
// array
// in case there are no cookies at all. Also prevents odd result
// when
// calling $.cookie().
cookies = document.cookie ? document.cookie.split('; ') : [], i = 0, l = cookies.length; for (; i < l; i++) {
var parts = cookies[i].split('='), name = decode(parts
.shift()), cookie = parts.join('='); if (key === name) {
// If second argument (value) is a function it's a
// converter...
result = read(cookie, value);
break;
} // Prevent storing a cookie that we couldn't decode.
if (!key && (cookie = read(cookie)) !== undefined) {
result[name] = cookie;
}
} return result;
}; config.defaults = {}; $.removeCookie = function(key, options) {
// Must not alter options, thus extending a fresh object...
$.cookie(key, '', $.extend({}, options, {
expires : -1
}));
return !$.cookie(key);
}; }));
2):引入 jquery.base64.js (这是自己写的 js )
(function ($) { var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; $.extend({
"Base64encode": function (e) {
var t = "";
var n, r, i, s, o, u, a;
var f = 0;
e = $.Base64_utf8_encode(e);
while (f < e.length) {
n = e.charCodeAt(f++);
r = e.charCodeAt(f++);
i = e.charCodeAt(f++);
s = n >> 2;
o = (n & 3) << 4 | r >> 4;
u = (r & 15) << 2 | i >> 6;
a = i & 63;
if (isNaN(r)) {
u = a = 64
} else if (isNaN(i)) {
a = 64
}
t = t + _keyStr.charAt(s) + _keyStr.charAt(o) + _keyStr.charAt(u) + _keyStr.charAt(a)
}
return t
},
"Base64decode": function (e) {
var t = "";
var n, r, i;
var s, o, u, a;
var f = 0;
e = e.replace(/[^A-Za-z0-9+/=]/g, "");
while (f < e.length) {
s = _keyStr.indexOf(e.charAt(f++));
o = _keyStr.indexOf(e.charAt(f++));
u = _keyStr.indexOf(e.charAt(f++));
a = _keyStr.indexOf(e.charAt(f++));
n = s << 2 | o >> 4;
r = (o & 15) << 4 | u >> 2;
i = (u & 3) << 6 | a;
t = t + String.fromCharCode(n);
if (u != 64) {
t = t + String.fromCharCode(r)
}
if (a != 64) {
t = t + String.fromCharCode(i)
}
}
t = $.Base64_utf8_decode(t);
return t
},
"Base64_utf8_encode": function (e) {
e = e.replace(/rn/g, "n");
var t = "";
for (var n = 0; n < e.length; n++) {
var r = e.charCodeAt(n);
if (r < 128) {
t += String.fromCharCode(r)
} else if (r > 127 && r < 2048) {
t += String.fromCharCode(r >> 6 | 192);
t += String.fromCharCode(r & 63 | 128)
} else {
t += String.fromCharCode(r >> 12 | 224);
t += String.fromCharCode(r >> 6 & 63 | 128);
t += String.fromCharCode(r & 63 | 128)
}
}
return t
},
"Base64_utf8_decode": function (e) {
var t = "";
var n = 0;
var r = c1 = c2 = 0;
while (n < e.length) {
r = e.charCodeAt(n);
if (r < 128) {
t += String.fromCharCode(r);
n++
} else if (r > 191 && r < 224) {
c2 = e.charCodeAt(n + 1);
t += String.fromCharCode((r & 31) << 6 | c2 & 63);
n += 2
} else {
c2 = e.charCodeAt(n + 1);
c3 = e.charCodeAt(n + 2);
t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
n += 3
}
}
return t
}
});
})(jQuery);
3):cookie_0.1.js 封装 (这是自己写的 js)
/**
* 全局cookies操作
*/
(function ($) { /**
* 设置cookie
*/
$.setCookie = function (key, value, expires) {
/*var _key = $.base64.encode(encodeURIComponent(key));*/
if (value) {
if (typeof value == "object" || typeof value == "number") {
value = JSON.stringify(value);
}
value = $.Base64encode(value);
}
var options = {path: '/'};
if (expires) {
options.expires = expires;
}
return $.cookie(key, value, options);
}; /**
* 获取cookie
*/
$.getCookie = function (key) {
/*var _key = $.base64.encode(encodeURIComponent(key));*/
var value = $.cookie(key);
if (value) {
value = $.Base64decode(value);
value = decodeURIComponent(value);
try {
value = JSON.parse(value);
} catch (e) {
}
}
return value;
}; /**
* 清理cookie
*/
$.cleanCookie = function (key) {
$.removeCookie(key, {path: '/'});
return !$.cookie(key);
}; }(jQuery));
4):使用
CookieUtil:在 本博客 java 分类中
@RequestMapping("add")
public String add(HttpServletResponse response) {
CookieUtil.addCookie(response, "string", "string", 0);
Map<String, String> map = new HashMap<String, String>() {{
put("a", "a");
}};
CookieUtil.addCookie(response, "map", JSON.toJSONString(map), 0);
return "OK";
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CookiePage</title>
<script src="/js/jquery-3.2.0.min.js"></script>
<script type="text/javascript" src="/js/cookie/jquery.cookie.js"></script>
<script src="/js/cookie/jquery.base64.js"></script>
<script type="text/javascript" src="/js/cookie/cookie_0.1.js"></script>
</head>
<body>
<script type="text/javascript"> $(function () { var _string = $.getCookie("string");
var _map = $.getCookie("map"); alert(_string + "--" + _map["a"]);
}); </script>
</body>
</html>
通过上述编写 可读|可写|可删
coding++:js实现基于Base64的编码及解码的更多相关文章
- 在LoadRunner中进行Base64的编码和解码
<Base64 Encode/Decode for LoadRunner>这篇文章介绍了如何在LoadRunner中对字符串进行Base64的编码和解码: http://ptfrontli ...
- 利用openssl进行base64的编码与解码
openssl可以直接使用命令对文件件进行base64的编码与解码,利用openssl提供的API同样可以做到这一点. 废话不多说,直接上代码了.需要注意的是通过base64编码后的字符每64个字节都 ...
- javascript中的Base64.UTF8编码与解码详解
javascript中的Base64.UTF8编码与解码详解 本文给大家介绍的是javascript中的Base64.UTF8编码与解码的函数源码分享以及使用范例,十分实用,推荐给小伙伴们,希望大家能 ...
- js和C#中的编码和解码
同一个字符串,用URL编码和HTML编码,结果是完全不同的. JS中的URL编码和解码.对 ASCII 字母和数字及以下特殊字符无效: - _ . ! ~ * ' ( ) ,/?:@&=+$# ...
- [Swift]扩展String类:Base64的编码和解码
扩展方式1: extension String { //Base64编码 func encodBase64() -> String? { if let data = self.data(usin ...
- 利用window对象自带atob和btoa方法进行base64的编码和解码
项目中一般需要将表单中的数据进行编码之后再进行传输到服务器,这个时候就需要base64编码 现在可以使用window自带的方法window.atob() 和 window.btoa() 方法进行 ...
- js与java encodeURI 进行编码与解码
JS escape()使用转义序列替换某些字符来对字符串进行编码 JavaScript 中国 编码后 JavaScript %u4E2D%u56FD unescape()对使用 encodeUR ...
- 在 Java 中如何进行 BASE64 编码和解码
BASE64 编码是一种常用的字符编码,在很多地方都会用到.JDK 中提供了非常方便的 BASE64Encoder 和 BASE64Decoder,用它们可以非常方便的完成基于 BASE64 的编码和 ...
- js实现base64编码与解码(原生js)
一直以来很多人使用到 JavaScript 进行 base64 编码解码时都是使用的 Base64.js,但事实上,浏览器很早就原生支持 base64 的编码与解码了 以前的方式 编码: <ja ...
随机推荐
- webpack中打包拷贝静态文件CopyWebpackPlugin插件
copyWebpackPlugin: 作用:用于webpack打包时拷贝文件的插件包 安装:npm install copyWebpackPlugin@版本号 使用:// copy custom st ...
- meterpreter会话渗透利用常用的32个命令归纳小结
仅作渗透测试技术实验之用,请勿针对任何未授权网络和设备. 1.background命令 返回,把meterpreter后台挂起 2.session命令 session 命令可以查看已经成功获取的会话 ...
- c语言之单向链表
0x00 什么是链表 链表可以说是一种最为基础的数据结构了,而单向链表更是基础中的基础.链表是由一组元素以特定的顺序组合或链接在一起的,不同元素之间在逻辑上相邻,但是在物理上并不一定相邻.在维护一组数 ...
- 7-40 jmu-python-统计成绩 (15 分)
输入一批学生成绩,计算平均成绩,并统计不及格学生人数. 输入格式: 每行输入一个数据,输入数据为负数结束输入 输出格式: 平均分=XX,不及格人数=XX,其中XX表示对应数据.如果没有学生数据,输出没 ...
- python Could not find a version that satisfies the requirement pymysql (from versions: none) ERROR: No matching distribution found for pymysql
使用pip安装pymysql出错;Could not find a version that satisfies the requirement cryptography (from pymysql) ...
- Ubuntu pppoe宽带拨号相关问题
因为可视化界面没有相关设置,因此采用终端命令的方法. 测试环境:Ubuntu 18.0.4 pppoe的配置:$ sudo pppoeconf 然后进入此界面进行一系列宽带拨号的设置. 联网:$ su ...
- 使用web写UI, 使用js对接C++项目, 提高开发效率
ppt资源下载地址https://www.slidestalk.com/s/webui_nodejs_cmdlrx
- psql的时间类型,通过时间查询
psql的时间类型,通过时间查询 psql有date/timestamp类型,date只显示年月日1999-01-08,而timestamp显示年月日时分秒 1999-01-08 09:54:03.2 ...
- 建议20:建议通过Function扩展类型
JavaScript允许为语言的基本数据类型定义方法.通过Object.prototype添加原型方法,该方法可被所有的对象,.这样的方法对函数,数组,字符串,数字,正则表达式和布尔值都适用.例如,通 ...
- Java 锁详解(转)
转自 https://www.cnblogs.com/jyroy/p/11365935.html Java提供了种类丰富的锁,每种锁因其特性的不同,在适当的场景下能够展现出非常高的效率.本文旨在对锁相 ...