js文件:

/** $Id: base64.js,v 2.15 2014/04/05 12:58:57 dankogai Exp dankogai $**  Licensed under the MIT license.*    http://opensource.org/licenses/mit-license**  References:*    http://en.wikipedia.org/wiki/Base64*/(function(global) {'use strict';// existing version for noConflict()var _Base64 = global.Base64;var version = "2.1.9";// if node.js, we use Buffervar buffer;if (typeof module !== 'undefined' && module.exports) {try {buffer = require('buffer').Buffer;} catch (err) {}}// constantsvar b64chars= 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';var b64tab = function(bin) {var t = {};for (var i = 0, l = bin.length; i < l; i++) t[bin.charAt(i)] = i;return t;}(b64chars);var fromCharCode = String.fromCharCode;// encoder stuffvar cb_utob = function(c) {if (c.length < 2) {var cc = c.charCodeAt(0);return cc < 0x80 ? c: cc < 0x800 ? (fromCharCode(0xc0 | (cc >>> 6))+ fromCharCode(0x80 | (cc & 0x3f))): (fromCharCode(0xe0 | ((cc >>> 12) & 0x0f))+ fromCharCode(0x80 | ((cc >>>  6) & 0x3f))+ fromCharCode(0x80 | ( cc         & 0x3f)));} else {var cc = 0x10000+ (c.charCodeAt(0) - 0xD800) * 0x400+ (c.charCodeAt(1) - 0xDC00);return (fromCharCode(0xf0 | ((cc >>> 18) & 0x07))+ fromCharCode(0x80 | ((cc >>> 12) & 0x3f))+ fromCharCode(0x80 | ((cc >>>  6) & 0x3f))+ fromCharCode(0x80 | ( cc         & 0x3f)));}};var re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g;var utob = function(u) {return u.replace(re_utob, cb_utob);};var cb_encode = function(ccc) {var padlen = [0, 2, 1][ccc.length % 3],ord = ccc.charCodeAt(0) << 16| ((ccc.length > 1 ? ccc.charCodeAt(1) : 0) << 8)| ((ccc.length > 2 ? ccc.charCodeAt(2) : 0)),chars = [b64chars.charAt( ord >>> 18),b64chars.charAt((ord >>> 12) & 63),padlen >= 2 ? '=' : b64chars.charAt((ord >>> 6) & 63),padlen >= 1 ? '=' : b64chars.charAt(ord & 63)];return chars.join('');};var btoa = global.btoa ? function(b) {return global.btoa(b);} : function(b) {return b.replace(/[\s\S]{1,3}/g, cb_encode);};var _encode = buffer ? function (u) {return (u.constructor === buffer.constructor ? u : new buffer(u)).toString('base64')}: function (u) { return btoa(utob(u)) };var encode = function(u, urisafe) {return !urisafe? _encode(String(u)): _encode(String(u)).replace(/[+\/]/g, function(m0) {return m0 == '+' ? '-' : '_';}).replace(/=/g, '');};var encodeURI = function(u) { return encode(u, true) };// decoder stuffvar re_btou = new RegExp(['[\xC0-\xDF][\x80-\xBF]','[\xE0-\xEF][\x80-\xBF]{2}','[\xF0-\xF7][\x80-\xBF]{3}'].join('|'), 'g');var cb_btou = function(cccc) {switch(cccc.length) {case 4:var cp = ((0x07 & cccc.charCodeAt(0)) << 18)|    ((0x3f & cccc.charCodeAt(1)) << 12)|    ((0x3f & cccc.charCodeAt(2)) <<  6)|     (0x3f & cccc.charCodeAt(3)),offset = cp - 0x10000;return (fromCharCode((offset  >>> 10) + 0xD800)+ fromCharCode((offset & 0x3FF) + 0xDC00));case 3:return fromCharCode(((0x0f & cccc.charCodeAt(0)) << 12)| ((0x3f & cccc.charCodeAt(1)) << 6)|  (0x3f & cccc.charCodeAt(2)));default:return  fromCharCode(((0x1f & cccc.charCodeAt(0)) << 6)|  (0x3f & cccc.charCodeAt(1)));}};var btou = function(b) {return b.replace(re_btou, cb_btou);};var cb_decode = function(cccc) {var len = cccc.length,padlen = len % 4,n = (len > 0 ? b64tab[cccc.charAt(0)] << 18 : 0)| (len > 1 ? b64tab[cccc.charAt(1)] << 12 : 0)| (len > 2 ? b64tab[cccc.charAt(2)] <<  6 : 0)| (len > 3 ? b64tab[cccc.charAt(3)]       : 0),chars = [fromCharCode( n >>> 16),fromCharCode((n >>>  8) & 0xff),fromCharCode( n         & 0xff)];chars.length -= [0, 0, 2, 1][padlen];return chars.join('');};var atob = global.atob ? function(a) {return global.atob(a);} : function(a){return a.replace(/[\s\S]{1,4}/g, cb_decode);};var _decode = buffer ? function(a) {return (a.constructor === buffer.constructor? a : new buffer(a, 'base64')).toString();}: function(a) { return btou(atob(a)) };var decode = function(a){return _decode(String(a).replace(/[-_]/g, function(m0) { return m0 == '-' ? '+' : '/' }).replace(/[^A-Za-z0-9\+\/]/g, ''));};var noConflict = function() {var Base64 = global.Base64;global.Base64 = _Base64;return Base64;};// export Base64global.Base64 = {VERSION: version,atob: atob,btoa: btoa,fromBase64: decode,toBase64: encode,utob: utob,encode: encode,encodeURI: encodeURI,btou: btou,decode: decode,noConflict: noConflict};// if ES5 is available, make Base64.extendString() availableif (typeof Object.defineProperty === 'function') {var noEnum = function(v){return {value:v,enumerable:false,writable:true,configurable:true};};global.Base64.extendString = function () {Object.defineProperty(String.prototype, 'fromBase64', noEnum(function () {return decode(this)}));Object.defineProperty(String.prototype, 'toBase64', noEnum(function (urisafe) {return encode(this, urisafe)}));Object.defineProperty(String.prototype, 'toBase64URI', noEnum(function () {return encode(this, true)}));};}// that's it!if (global['Meteor']) {Base64 = global.Base64; // for normal export in Meteor.js}
})(this);

===================================================================

使用demo

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
</body>
<script src="base64.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">var common = common || {};/*** 加密方法*/
common.encrypt = function(key,value){localStorage.setItem(key,(Base64.encode(value)));}/*** 解密方法*/common.decipher = function(key){var rerunValue = localStorage.getItem(key);if(common.isNull(rerunValue)){rerunValue = null;}else{rerunValue = Base64.decode(rerunValue);}return rerunValue;}common.isNull = function(value){
return typeof value =='undefined' || value==="" ||value==null ||value==undefined;
}common.isNotNull = function (value){
return !common.isNull(value);
}common.encrypt('1',3);console.log(common.decipher('1'))
</script>
</html><!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
</body>
<script type="text/javascript">console.log(base64_encode('12345678'))
console.log(base64_decode('MTIzNDU2Nzg='))/** Javascript base64_encode() base64加密函数用于生成字符串对应的base64加密字符串* 吴先成  www.51-n.com ohcc@163.com QQ:229256237* @param string str 原始字符串* @return string 加密后的base64字符串*/function base64_encode(str){var c1, c2, c3;var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";                var i = 0, len= str.length, string = '';while (i < len){c1 = str.charCodeAt(i++) & 0xff;if (i == len){string += base64EncodeChars.charAt(c1 >> 2);string += base64EncodeChars.charAt((c1 & 0x3) << 4);string += "==";break;}c2 = str.charCodeAt(i++);if (i == len){string += base64EncodeChars.charAt(c1 >> 2);string += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));string += base64EncodeChars.charAt((c2 & 0xF) << 2);string += "=";break;}c3 = str.charCodeAt(i++);string += base64EncodeChars.charAt(c1 >> 2);string += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4));string += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6));string += base64EncodeChars.charAt(c3 & 0x3F)}return string}/** Javascript base64_decode() base64解密函数用于解密base64加密的字符串* 吴先成  www.51-n.com ohcc@163.com QQ:229256237* @param string str base64加密字符串* @return string 解密后的字符串*/function base64_decode(str){var c1, c2, c3, c4;var base64DecodeChars = new Array(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,-1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57,58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 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, -1, -1, -1, -1, -1, -1, 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, -1, -1, -1,-1, -1);var i=0, len = str.length, string = '';while (i < len){do{c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff]} while (i < len && c1 == -1);if (c1 == -1) break;do{c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff]} while (i < len && c2 == -1);if (c2 == -1) break;string += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));do{c3 = str.charCodeAt(i++) & 0xff;if (c3 == 61)return string;c3 = base64DecodeChars[c3]} while (i < len && c3 == -1);if (c3 == -1) break;string += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));do{c4 = str.charCodeAt(i++) & 0xff;if (c4 == 61) return string;c4 = base64DecodeChars[c4]} while (i < len && c4 == -1);if (c4 == -1) break;string += String.fromCharCode(((c3 & 0x03) << 6) | c4)}return string;}
</script>
</html>


Base64(本地存储加密解密)相关推荐

  1. base64,base32bit加密解密

    import base64 str='admin' str=str.encode('utf-8') #加密 bs64=base64.b64encode(str) #解密 debs64=base64.b ...

  2. Vue项目:路由跳转时中文传参被URL编码,怎么解决?用js封装Base64编码解码加密解密

    1.在utils中封装js方法,代码如下: var Base64 = { ​// private property_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZab ...

  3. [网络安全课程实验]:PGP加密解密

    目录 实验名称:  PGP 加密解密 一.实验目的 二.实验环境 三.实验内容与实验要求 四.实验过程与分析 加密文件 使用PGP磁盘加密磁盘 五.实验结果总结 实验名称: PGP 加密解密 一.实验 ...

  4. Java AES 加密解密工具类

    maven 引入一个依赖 <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec --> <depen ...

  5. 国密算法SM2加密解密

    一.依赖包 <!-- hutool的 SM2 加密--><dependency><groupId>org.bouncycastle</groupId>& ...

  6. 1、【java数据安全】数据安全之加密解密(base64、MD、SHA、DES、AES、IDEA、PBE、DH、RSA、EIGamal)、数字签名(DSA、ECDSA)和数字证书介绍、应用示例详细介绍

    java数据安全 系列文章 1.[java数据安全]数据安全之加密解密(base64.MD.SHA.DES.AES.IDEA.PBE.DH.RSA.EIGamal).数字签名(DSA.ECDSA)和数 ...

  7. Lua Base64加密解密

    Base64 加密解密方法相关 -- 加密 function encode(data)local b = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrst ...

  8. 与众不同 windows phone (28) - Feature(特性)之手机方向, 本地化, 应用程序的试用体验, 系统主题资源, 本地数据的加密解密...

    原文:与众不同 windows phone (28) - Feature(特性)之手机方向, 本地化, 应用程序的试用体验, 系统主题资源, 本地数据的加密解密 [索引页] [源码下载] 与众不同 w ...

  9. js_md5加密和base64的加密解密

    1.最近有些人在爬我们公司的数据,然有了这个md5加密的小需求.为什么叫小需求呢?嗯,之前没做过,会以为很复杂. 其实,是想多了. 2.前端md5加密,其实也并不是安全的,因为代码是可见的.也就是说, ...

最新文章

  1. 女生参加web前端培训压力大吗
  2. call,apply,bind,new实现原理
  3. C语言 解决4996警告
  4. Logback 配置文件例子
  5. php贝叶斯,php – 将单个概率与朴素贝叶斯垃圾邮件过滤相结合
  6. ”盒模型“之如何防止边框和内边距把元素撑开
  7. asp.net core合并压缩资源文件引发的学习之旅
  8. 浅谈项目开发现状(一)
  9. BugkuCTF-Reverse题Easy_vb多方法解决
  10. find和chmod结合来修改权限
  11. tf7: RNN—古诗词
  12. 合并两个有序表(C语言)
  13. kotlin的by lazy
  14. 移动通信网络规划:多址技术
  15. amd显卡风扇调节_amd显卡风扇速度设置linux版本
  16. Visual Studio 2008项目打包问题——项目太大C盘空间不够怎么办?
  17. 微信支付(公众号支付)微信公众平台开发教程(5)
  18. 手机软件自动化测试探索
  19. Pytorch内存泄漏Memory Leak
  20. 计算机处理器i5和i7,i5与i7有什么区别 酷睿i5和i7区别汇总【详细介绍】

热门文章

  1. OpenStack Swift Architecture
  2. 搭建个人的GPS定位系统
  3. excel将柱状图的柱宽减为直线的方法 作x轴垂线的方法
  4. UML/SysML和流浪地球的地球发动机
  5. mysql04:数据管理
  6. planet_Earth靶场渗透记录
  7. 百度推广图片库里面的图片可以删除吗?
  8. python pip在哪个文件夹运行_如何安装python与pip-百度经验
  9. 使用tensorflow进行简单的强化学习 1—Q-learning
  10. stata里php代码,stata字符型数据如何转数值型