Javascript base64编码实现代码


Posted in Javascript onDecember 02, 2011
/* 
* base64编码 
*/ 
var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 
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); 
function base64encode(str) { 

var out, i, len; 

var c1, c2, c3; 

len = str.length; 

i = 0; 

out = ""; 

while(i < len) { 
c1 = str.charCodeAt(i++) & 0xff; 
if(i == len) 
{ 
out += base64EncodeChars.charAt(c1 >> 2); 

 out += base64EncodeChars.charAt((c1 & 0x3) << 4); 

 out += "=="; 

 break; 
} 
c2 = str.charCodeAt(i++); 
if(i == len) 
{ 

 out += base64EncodeChars.charAt(c1 >> 2); 

 out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)); 

 out += base64EncodeChars.charAt((c2 & 0xF) << 2); 

 out += "="; 

 break; 
} 
c3 = str.charCodeAt(i++); 
out += base64EncodeChars.charAt(c1 >> 2); 
out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)); 
out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)); 
out += base64EncodeChars.charAt(c3 & 0x3F); 

} 

return out; 
} 
function base64decode(str) { 

var c1, c2, c3, c4; 

var i, len, out; 

len = str.length; 

i = 0; 

out = ""; 

while(i < len) { 
/* c1 */ 
do { 

 c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff]; 
} while(i < len && c1 == -1); 
if(c1 == -1) 

 break; 
/* c2 */ 
do { 

 c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff]; 
} while(i < len && c2 == -1); 
if(c2 == -1) 

 break; 
out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4)); 
/* c3 */ 
do { 

 c3 = str.charCodeAt(i++) & 0xff; 

 if(c3 == 61) 
  return out; 

 c3 = base64DecodeChars[c3]; 
} while(i < len && c3 == -1); 
if(c3 == -1) 

 break; 
out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2)); 
/* c4 */ 
do { 

 c4 = str.charCodeAt(i++) & 0xff; 

 if(c4 == 61) 
  return out; 

 c4 = base64DecodeChars[c4]; 
} while(i < len && c4 == -1); 
if(c4 == -1) 

 break; 
out += String.fromCharCode(((c3 & 0x03) << 6) | c4); 

 } 

return out; 
} 
function utf16to8(str) { 

var out, i, len, c; 

out = ""; 

len = str.length; 

for(i = 0; i < len; i++) { 
c = str.charCodeAt(i); 
if ((c >= 0x0001) && (c <= 0x007F)) { 

 out += str.charAt(i); 
} else if (c > 0x07FF) { 

 out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F)); 

 out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F)); 

 out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F)); 
} else { 

 out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F)); 

 out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F)); 
} 

} 

return out; 
} 
function utf8to16(str) { 

var out, i, len, c; 

var char2, char3; 

out = ""; 

len = str.length; 

i = 0; 

while(i < len) { 
c = str.charCodeAt(i++); 
switch(c >> 4) 
{ 
  case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: 

 // 0xxxxxxx 

 out += str.charAt(i-1); 

 break; 
  case 12: case 13: 

 // 110x xxxx  10xx xxxx 

 char2 = str.charCodeAt(i++); 

 out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F)); 

 break; 
  case 14: 

 // 1110 xxxx 10xx xxxx 10xx xxxx 

 char2 = str.charCodeAt(i++); 

 char3 = str.charCodeAt(i++); 

 out += String.fromCharCode(((c & 0x0F) << 12) | 


((char2 & 0x3F) << 6) | 


((char3 & 0x3F) << 0)); 

 break; 
} 
} 

return out; 
} 
/* 
* base64编码 End 
*/ 
var m = 'hello'; 
m = base64encode(m);

这是一个例子:
<HTML> 
<HEAD> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
<TITLE>Javascript Base64 Demo</TITLE> 
<script language=javascript> 
/* 
* base64编码 
*/ 
var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 
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); 
function base64encode(str) { 

var out, i, len; 

var c1, c2, c3; 

len = str.length; 

i = 0; 

out = ""; 

while(i < len) { 
c1 = str.charCodeAt(i++) & 0xff; 
if(i == len) 
{ 
out += base64EncodeChars.charAt(c1 >> 2); 

 out += base64EncodeChars.charAt((c1 & 0x3) << 4); 

 out += "=="; 

 break; 
} 
c2 = str.charCodeAt(i++); 
if(i == len) 
{ 

 out += base64EncodeChars.charAt(c1 >> 2); 

 out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)); 

 out += base64EncodeChars.charAt((c2 & 0xF) << 2); 

 out += "="; 

 break; 
} 
c3 = str.charCodeAt(i++); 
out += base64EncodeChars.charAt(c1 >> 2); 
out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)); 
out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)); 
out += base64EncodeChars.charAt(c3 & 0x3F); 

} 

return out; 
} 
function base64decode(str) { 

var c1, c2, c3, c4; 

var i, len, out; 

len = str.length; 

i = 0; 

out = ""; 

while(i < len) { 
/* c1 */ 
do { 

 c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff]; 
} while(i < len && c1 == -1); 
if(c1 == -1) 

 break; 
/* c2 */ 
do { 

 c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff]; 
} while(i < len && c2 == -1); 
if(c2 == -1) 

 break; 
out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4)); 
/* c3 */ 
do { 

 c3 = str.charCodeAt(i++) & 0xff; 

 if(c3 == 61) 
  return out; 

 c3 = base64DecodeChars[c3]; 
} while(i < len && c3 == -1); 
if(c3 == -1) 

 break; 
out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2)); 
/* c4 */ 
do { 

 c4 = str.charCodeAt(i++) & 0xff; 

 if(c4 == 61) 
  return out; 

 c4 = base64DecodeChars[c4]; 
} while(i < len && c4 == -1); 
if(c4 == -1) 

 break; 
out += String.fromCharCode(((c3 & 0x03) << 6) | c4); 

 } 

return out; 
} 
function utf16to8(str) { 

var out, i, len, c; 

out = ""; 

len = str.length; 

for(i = 0; i < len; i++) { 
c = str.charCodeAt(i); 
if ((c >= 0x0001) && (c <= 0x007F)) { 

 out += str.charAt(i); 
} else if (c > 0x07FF) { 

 out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F)); 

 out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F)); 

 out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F)); 
} else { 

 out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F)); 

 out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F)); 
} 

} 

return out; 
} 
function utf8to16(str) { 

var out, i, len, c; 

var char2, char3; 

out = ""; 

len = str.length; 

i = 0; 

while(i < len) { 
c = str.charCodeAt(i++); 
switch(c >> 4) 
{ 
  case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: 

 // 0xxxxxxx 

 out += str.charAt(i-1); 

 break; 
  case 12: case 13: 

 // 110x xxxx  10xx xxxx 

 char2 = str.charCodeAt(i++); 

 out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F)); 

 break; 
  case 14: 

 // 1110 xxxx 10xx xxxx 10xx xxxx 

 char2 = str.charCodeAt(i++); 

 char3 = str.charCodeAt(i++); 

 out += String.fromCharCode(((c & 0x0F) << 12) | 


((char2 & 0x3F) << 6) | 


((char3 & 0x3F) << 0)); 

 break; 
} 
} 

return out; 
} 
/* 
* base64编码 End 
*/ 
function doit() { 

var f = document.f 

f.output.value = base64encode(utf16to8(f.source.value)) 

f.decode.value = utf8to16(base64decode(f.output.value)) 
} 
</script> 
</HEAD> 
<BODY> 
<H1>Javascript Base64 Demo</H1> 
<FORM NAME="f"> 
原码<BR> 
<TEXTAREA NAME="source" ROWS=4 COLS=80 WRAP="soft"></TEXTAREA><BR><BR> 
Base64 encode<BR> 
<TEXTAREA NAME="output" ROWS=4 COLS=80 WRAP="soft"></TEXTAREA><BR><BR> 
Base64 decode<BR> 
<TEXTAREA NAME="decode" ROWS=4 COLS=80 WRAP="soft"></TEXTAREA><BR><BR> 
<INPUT TYPE=BUTTON VALUE="转换" ONCLICK="doit();"> 
</FORM> 
</BODY> 
</html>
Javascript 相关文章推荐
window.parent调用父框架时 ie跟火狐不兼容问题
Jul 30 Javascript
javascript parseInt 大改造
Sep 27 Javascript
简略说明Javascript中的= =(等于)与= = =(全等于)区别
Apr 16 Javascript
js实现跟随鼠标移动且带关闭功能的图片广告实例
Feb 26 Javascript
jquery验证手机号是否正确实例讲解
Nov 17 Javascript
深入学习JavaScript的AngularJS框架中指令的使用方法
Mar 05 Javascript
BootStrap实现带有增删改查功能的表格(DEMO详解)
Oct 26 Javascript
JavaScript实现经典排序算法之插入排序
Dec 28 Javascript
vue用递归组件写树形控件的实例代码
Jul 19 Javascript
超详细的5个Shell脚本实例分享(值得收藏)
Aug 15 Javascript
ES6 proxy和reflect的使用方法与应用实例分析
Feb 15 Javascript
在vue-cli3.0 中使用预处理器 (Sass/Less/Stylus) 配置全局变量操作
Aug 10 Javascript
JS获取页面窗口大小的代码解读
Dec 01 #Javascript
基于JQuery的抓取博客园首页RSS的代码
Dec 01 #Javascript
jQuery 中使用JSON的实现代码
Dec 01 #Javascript
jquery中dom操作和事件的实例学习 下拉框应用
Dec 01 #Javascript
JS代码放在head和body中的区别分析
Dec 01 #Javascript
js获取浏览器的可视区域尺寸的实现代码
Nov 30 #Javascript
js取滚动条的尺寸的函数代码
Nov 30 #Javascript
You might like
简单的用PHP编写的导航条程序
2006/10/09 PHP
PHP遍历目录并返回统计目录大小
2014/06/09 PHP
详解Yaf框架PHPUnit集成测试方法
2017/12/27 PHP
javascript页面加载完执行事件代码
2014/02/11 Javascript
浅谈Jquery为元素绑定事件
2015/04/27 Javascript
简介JavaScript中fixed()方法的使用
2015/06/08 Javascript
浅谈jQuery.easyui的datebox格式化时间
2015/06/25 Javascript
情人节单身的我是如何在敲完代码之后收到12束玫瑰的(javascript)
2015/08/21 Javascript
Easyui form combobox省市区三级联动
2016/01/13 Javascript
JS实现复制内容到剪贴板功能兼容所有浏览器(推荐)
2016/06/17 Javascript
利用iscroll4实现轮播图效果实例代码
2017/01/11 Javascript
关于vue.extend和vue.component的区别浅析
2017/08/16 Javascript
jQuery实现简单的Ajax调用功能示例
2019/02/15 jQuery
JS中min函数实例讲解
2019/02/18 Javascript
使用layui前端框架弹出form表单以及提交的示例
2019/10/25 Javascript
antd table按表格里的日期去排序操作
2020/11/17 Javascript
[02:25]DOTA2英雄基础教程 熊战士
2014/01/03 DOTA
python分割列表(list)的方法示例
2017/05/07 Python
Python 查看文件的编码格式方法
2017/12/21 Python
TensorFlow实现简单的CNN的方法
2019/07/18 Python
Python 词典(Dict) 加载与保存示例
2019/12/06 Python
pytorch 实现模型不同层设置不同的学习率方式
2020/01/06 Python
Django中使用Json返回数据的实现方法
2020/06/03 Python
python中upper是做什么用的
2020/07/20 Python
纯css3实现效果超级炫的checkbox复选框和radio单选框
2014/09/01 HTML / CSS
Canvas实现贝赛尔曲线轨迹动画的示例代码
2019/04/25 HTML / CSS
生物科学专业个人求职信范文
2013/12/07 职场文书
幼教毕业生自我鉴定
2014/01/12 职场文书
初中体育教学反思
2014/01/14 职场文书
酒店总经理助理岗位职责
2014/02/01 职场文书
员工合理化建议书
2014/05/19 职场文书
目标责任书格式
2014/07/28 职场文书
试用期辞职信范文
2015/03/02 职场文书
公司欠款证明
2015/06/24 职场文书
带你彻底理解JavaScript中的原型对象
2021/04/14 Javascript
关于redisson缓存序列化几枚大坑说明
2021/08/04 Redis