AngularJS实现的base64编码与解码功能示例


Posted in Javascript onMay 17, 2018

本文实例讲述了AngularJS实现的base64编码与解码功能。分享给大家供大家参考,具体如下:

<!DOCTYPE HTML>
<html>
<head>
 <meta charset="utf-8">
 <title>base64加密</title>
 <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script>
 <script>
  function Base64() {
   // private property
   _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
   // public method for encoding
   this.encode = function (input) {
    var output = "";
    var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
    var i = 0;
    input = _utf8_encode(input);
    while (i < input.length) {
     chr1 = input.charCodeAt(i++);
     chr2 = input.charCodeAt(i++);
     chr3 = input.charCodeAt(i++);
     enc1 = chr1 >> 2;
     enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
     enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
     enc4 = chr3 & 63;
     if (isNaN(chr2)) {
      enc3 = enc4 = 64;
     } else if (isNaN(chr3)) {
      enc4 = 64;
     }
     output = output +
       _keyStr.charAt(enc1) + _keyStr.charAt(enc2) +
       _keyStr.charAt(enc3) + _keyStr.charAt(enc4);
    }
    return output;
   }
   // public method for decoding
   this.decode = function (input) {
    var output = "";
    var chr1, chr2, chr3;
    var enc1, enc2, enc3, enc4;
    var i = 0;
    input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
    while (i < input.length) {
     enc1 = _keyStr.indexOf(input.charAt(i++));
     enc2 = _keyStr.indexOf(input.charAt(i++));
     enc3 = _keyStr.indexOf(input.charAt(i++));
     enc4 = _keyStr.indexOf(input.charAt(i++));
     chr1 = (enc1 << 2) | (enc2 >> 4);
     chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
     chr3 = ((enc3 & 3) << 6) | enc4;
     output = output + String.fromCharCode(chr1);
     if (enc3 != 64) {
      output = output + String.fromCharCode(chr2);
     }
     if (enc4 != 64) {
      output = output + String.fromCharCode(chr3);
     }
    }
    output = _utf8_decode(output);
    return output;
   }
   // private method for UTF-8 encoding
   _utf8_encode = function (string) {
    string = string.replace(/\r\n/g, "\n");
    var utftext = "";
    for (var n = 0; n < string.length; n++) {
     var c = string.charCodeAt(n);
     if (c < 128) {
      utftext += String.fromCharCode(c);
     } else if ((c > 127) && (c < 2048)) {
      utftext += String.fromCharCode((c >> 6) | 192);
      utftext += String.fromCharCode((c & 63) | 128);
     } else {
      utftext += String.fromCharCode((c >> 12) | 224);
      utftext += String.fromCharCode(((c >> 6) & 63) | 128);
      utftext += String.fromCharCode((c & 63) | 128);
     }
    }
    return utftext;
   }
   // private method for UTF-8 decoding
   _utf8_decode = function (utftext) {
    var string = "";
    var i = 0;
    var c = c1 = c2 = 0;
    while (i < utftext.length) {
     c = utftext.charCodeAt(i);
     if (c < 128) {
      string += String.fromCharCode(c);
      i++;
     } else if ((c > 191) && (c < 224)) {
      c2 = utftext.charCodeAt(i + 1);
      string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
      i += 2;
     } else {
      c2 = utftext.charCodeAt(i + 1);
      c3 = utftext.charCodeAt(i + 2);
      string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
      i += 3;
     }
    }
    return string;
   }
  }
 </script>
</head>
<body>
<p id="p1">PHAgc3R5bGU9ImxpbmUtaGVpZ2h0OiAyZW07Ij48c3BhbiBzdHlsZT0iZm9udC1zaXplOiAxNnB4OyBmb250LWZhbWlseTog5b6u6L2v6ZuF6buRLCAnTWljcm9zb2Z0IFlhSGVpJzsiPu+7vzxpbWcgc3JjPSJodHRwOi8vd3d3LmpiNTEubmV0L2ltYWdlcy9sb2dvLmdpZiIgdGl0bGU9ImxvZ28uZ2lmIi8+PC9zcGFuPjwvcD48cCBzdHlsZT0idGV4dC1pbmRlbnQ6IDJlbTsgbGluZS1oZWlnaHQ6IDJlbTsiPjxzcGFuIHN0eWxlPSJmb250LXNpemU6IDE2cHg7IGZvbnQtZmFtaWx5Oi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4OyBmb250LWZhbWlseTog5b6u6L2v6ZuF6buRLCAnTWljcm9zb2Z0IFlhSGVpJzsiPui/memHjOi/mOaPkOS+m+Wkp+mHj+eahOa6kOeggeOAgeeUteWtkOS5puS4i+i9ve+8jOS7peWPiuWQhOenjeW4uOingeeahOWunueUqOWcqOe6v+W3peWFtzwvc3Bhbj48L3A+</p>
<script>
 var b = new Base64();
 str = document.getElementById("p1").innerHTML;
 //解密
 str = b.decode(str);
 document.getElementById("p1").innerHTML = str;
</script>
</body>
</html>

运行效果如下:

AngularJS实现的base64编码与解码功能示例

Javascript 相关文章推荐
js触发select onchange事件的小技巧
Aug 05 Javascript
JavaScript中DOM详解
Apr 13 Javascript
js实现YouKu的漂亮搜索框效果
Aug 19 Javascript
jQuery ajaxForm()的应用
Oct 14 Javascript
jQuery 遍历map()方法详解
Nov 04 Javascript
80%应聘者都不及格的JS面试题
Mar 21 Javascript
jQuery操作之效果详解
May 19 jQuery
Vue自定义指令详解
Jul 28 Javascript
jquery实现垂直无限轮播的方法分析
Jul 16 jQuery
vue keep-alive列表页缓存 详情页返回上一页不刷新,定位到之前位置
Nov 26 Javascript
如何通过简单的代码描述Angular父组件、子组件传值
Apr 07 Javascript
vue封装数字翻牌器
Apr 20 Vue.js
解决修复npm安装全局模块权限的问题
May 17 #Javascript
npm 下载指定版本的组件方法
May 17 #Javascript
ES6之模版字符串的具体使用
May 17 #Javascript
解决淘宝cnpm 安装后cnpm不是内部或外部命令的问题
May 17 #Javascript
使用淘宝镜像cnpm安装Vue.js的图文教程
May 17 #Javascript
解决Mac安装thrift因bison报错的问题
May 17 #Javascript
Vue中的字符串模板的使用
May 17 #Javascript
You might like
PHP5.3安装Zend Guard Loader图文教程
2014/09/29 PHP
thinkphp中memcache的用法实例
2014/11/29 PHP
PHP7.0安装笔记整理
2015/08/28 PHP
php反射类ReflectionClass用法分析
2016/05/12 PHP
PHP XML和数组互相转换详解
2016/10/26 PHP
浅谈PHP的$_SERVER[SERVER_NAME]
2017/02/04 PHP
thinkPHP5.0框架URL访问方法详解
2017/03/18 PHP
php+ajax实现无刷新文件上传功能(ajaxuploadfile)
2018/02/11 PHP
常见的5个PHP编码小陋习以及优化实例讲解
2021/02/27 PHP
解决FLASH需要点击激活的代码
2006/12/20 Javascript
javascript结合html5 canvas实现(可调画笔颜色/粗细/橡皮)的涂鸦板
2013/04/27 Javascript
jQuery替换字符串(实例代码)
2013/11/13 Javascript
JavaScript不刷新实现浏览器的前进后退功能
2014/11/05 Javascript
JS运动框架之分享侧边栏动画实例
2015/03/03 Javascript
JavaScript中Date.toSource()方法的使用教程
2015/06/12 Javascript
js鼠标点击图片切换效果实现代码
2015/11/19 Javascript
JS判断字符串字节数并截取长度的方法
2016/03/05 Javascript
jquery实现图片切换代码
2016/10/13 Javascript
使用 jQuery.ajax 上传带文件的表单遇到的问题
2016/10/31 Javascript
基于iscroll.js实现下拉刷新和上拉加载效果
2016/11/28 Javascript
工厂模式在JS中的实践
2017/01/18 Javascript
Jquery EasyUI $.Parser
2017/06/02 jQuery
详解node Async/Await 更好的异步编程解决方案
2018/05/10 Javascript
axios简单实现小程序延时loading指示
2018/07/30 Javascript
Vue刷新修改页面中数据的方法
2018/09/16 Javascript
微信小程序实现的绘制table表格功能示例
2019/04/26 Javascript
Python实现模拟浏览器请求及会话保持操作示例
2018/07/30 Python
Python实现的ftp服务器功能详解【附源码下载】
2019/06/26 Python
在linux下实现 python 监控usb设备信号
2019/07/03 Python
python list多级排序知识点总结
2019/10/23 Python
荷兰和比利时时尚鞋店:Van Dalen
2018/04/23 全球购物
英国DVD和蓝光碟片购买网站:Zoom.co.uk(电影和电视)
2019/09/23 全球购物
质量工程师岗位职责
2013/11/16 职场文书
群众对十八届四中全会的期盼
2014/10/17 职场文书
工商局个人工作总结
2015/03/03 职场文书
导游经典开场白——导游词
2019/04/17 职场文书