javascript实现base64 md5 sha1 密码加密


Posted in Javascript onSeptember 09, 2015

1、base64加密

在页面中引入base64.js文件,调用方法为:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>base64加密</title>
<script type="text/javascript" src="base64.js"></script>
<script type="text/javascript"> 
 var b = new Base64(); 
 var str = b.encode("admin:admin"); 
 alert("base64 encode:" + str); 

 //解密
 str = b.decode(str); 
 alert("base64 decode:" + str); 
</script> 
</head>
<body>
</body>
</html>

2、md5加密

在页面中引用md5.js文件,调用方法为

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>md5加密</title>
<script type="text/ecmascript" src="md5.js"></script>
<script type="text/javascript"> 
 var hash = hex_md5("123dafd");
 alert(hash)
</script> 
</head>
<body>
</body>
</html>

3、sha1加密

据说这是最安全的加密

页面中引入sha1.js,调用方法为

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>sha1加密</title>
<script type="text/ecmascript" src="sha1.js"></script>
<script type="text/javascript">
 var sha = hex_sha1('mima123465')
 alert(sha) 
</script> 
</head>
<body>
</body>
</html>

文章所包含的加密下载地址:

下面给大家补充点知识:非常流行的javascript的md5加密

在网上看到的javascript的MD5加密,看比较好,就摘录了,供参考

<HTML>
 <HEAD>
 <META http-equiv='Content-Type' content='text/html; charset=gb'>
 <TITLE>非常流行的JS的md加密办法</TITLE>
 </HEAD>
 <BODY >
 <input id=test value=webasp>
 <input type=button value=md onclick="alert(hex_md(test.value))">
 <script>
 var hexcase = ; /* hex output format. - lowercase; - uppercase */
 var bpad = ""; /* base- pad character. "=" for strict RFC compliance */
 var chrsz = ; /* bits per input character. - ASCII; - Unicode */
 /*
 * These are the functions you'll usually want to call
 * They take string arguments and return either hex or base- encoded strings
 */
 function hex_md(s){ return binlhex(core_md(strbinl(s), s.length * chrsz));}
 function b_md(s){ return binlb(core_md(strbinl(s), s.length * chrsz));}
 function hex_hmac_md(key, data) { return binlhex(core_hmac_md(key, data)); }
 function b_hmac_md(key, data) { return binlb(core_hmac_md(key, data)); }
 /* Backwards compatibility - same as hex_md() */
 function calcMD(s){ return binlhex(core_md(strbinl(s), s.length * chrsz));}
 /* 
 * Perform a simple self-test to see if the VM is working 
 */
 function md_vm_test()
 {
 return hex_md("abc") == "cdfbdfdef";
 }
 /*
 * Calculate the MD of an array of little-endian words, and a bit length
 */
 function core_md(x, len)
 {
 /* append padding */
 x[len >> ] |= x << ((len) % );
 x[(((len + ) >>> ) << ) + ] = len;
 var a = ;
 var b = -;
 var c = -;
 var d = ;
 for(var i = ; i < x.length; i += )
 {
 var olda = a;
 var oldb = b;
 var oldc = c;
 var oldd = d;
 a = md_ff(a, b, c, d, x[i+ ], , -);
 d = md_ff(d, a, b, c, x[i+ ], , -);
 c = md_ff(c, d, a, b, x[i+ ], , );
 b = md_ff(b, c, d, a, x[i+ ], , -);
 a = md_ff(a, b, c, d, x[i+ ], , -);
 d = md_ff(d, a, b, c, x[i+ ], , );
 c = md_ff(c, d, a, b, x[i+ ], , -);
 b = md_ff(b, c, d, a, x[i+ ], , -);
 a = md_ff(a, b, c, d, x[i+ ], , );
 d = md_ff(d, a, b, c, x[i+ ], , -);
 c = md_ff(c, d, a, b, x[i+], , -);
 b = md_ff(b, c, d, a, x[i+], , -);
 a = md_ff(a, b, c, d, x[i+], , );
 d = md_ff(d, a, b, c, x[i+], , -);
 c = md_ff(c, d, a, b, x[i+], , -);
 b = md_ff(b, c, d, a, x[i+], , );
 a = md_gg(a, b, c, d, x[i+ ], , -);
 d = md_gg(d, a, b, c, x[i+ ], , -);
 c = md_gg(c, d, a, b, x[i+], , );
 b = md_gg(b, c, d, a, x[i+ ], , -);
 a = md_gg(a, b, c, d, x[i+ ], , -);
 d = md_gg(d, a, b, c, x[i+], , );
 c = md_gg(c, d, a, b, x[i+], , -);
 b = md_gg(b, c, d, a, x[i+ ], , -);
 a = md_gg(a, b, c, d, x[i+ ], , );
 d = md_gg(d, a, b, c, x[i+], , -);
 c = md_gg(c, d, a, b, x[i+ ], , -);
 b = md_gg(b, c, d, a, x[i+ ], , );
 a = md_gg(a, b, c, d, x[i+], , -);
 d = md_gg(d, a, b, c, x[i+ ], , -);
 c = md_gg(c, d, a, b, x[i+ ], , );
 b = md_gg(b, c, d, a, x[i+], , -);
 a = md_hh(a, b, c, d, x[i+ ], , -);
 d = md_hh(d, a, b, c, x[i+ ], , -);
 c = md_hh(c, d, a, b, x[i+], , );
 b = md_hh(b, c, d, a, x[i+], , -);
 a = md_hh(a, b, c, d, x[i+ ], , -);
 d = md_hh(d, a, b, c, x[i+ ], , );
 c = md_hh(c, d, a, b, x[i+ ], , -);
 b = md_hh(b, c, d, a, x[i+], , -);
 a = md_hh(a, b, c, d, x[i+], , );
 d = md_hh(d, a, b, c, x[i+ ], , -);
 c = md_hh(c, d, a, b, x[i+ ], , -);
 b = md_hh(b, c, d, a, x[i+ ], , );
 a = md_hh(a, b, c, d, x[i+ ], , -);
 d = md_hh(d, a, b, c, x[i+], , -);
 c = md_hh(c, d, a, b, x[i+], , );
 b = md_hh(b, c, d, a, x[i+ ], , -);
 a = md_ii(a, b, c, d, x[i+ ], , -);
 d = md_ii(d, a, b, c, x[i+ ], , );
 c = md_ii(c, d, a, b, x[i+], , -);
 b = md_ii(b, c, d, a, x[i+ ], , -);
 a = md_ii(a, b, c, d, x[i+], , );
 d = md_ii(d, a, b, c, x[i+ ], , -);
 c = md_ii(c, d, a, b, x[i+], , -);
 b = md_ii(b, c, d, a, x[i+ ], , -);
 a = md_ii(a, b, c, d, x[i+ ], , );
 d = md_ii(d, a, b, c, x[i+], , -);
 c = md_ii(c, d, a, b, x[i+ ], , -);
 b = md_ii(b, c, d, a, x[i+], , );
 a = md_ii(a, b, c, d, x[i+ ], , -);
 d = md_ii(d, a, b, c, x[i+], , -);
 c = md_ii(c, d, a, b, x[i+ ], , );
 b = md_ii(b, c, d, a, x[i+ ], , -);
 a = safe_add(a, olda);
 b = safe_add(b, oldb);
 c = safe_add(c, oldc);
 d = safe_add(d, oldd);
 }
 return Array(a, b, c, d);
}
/*
 * These functions implement the four basic operations the algorithm uses.
 */
function md_cmn(q, a, b, x, s, t)
{
 return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
}
function md_ff(a, b, c, d, x, s, t)
{
 return md_cmn((b & c) | ((~b) & d), a, b, x, s, t);
}
function md_gg(a, b, c, d, x, s, t)
{
 return md_cmn((b & d) | (c & (~d)), a, b, x, s, t);
}
function md_hh(a, b, c, d, x, s, t)
{
 return md_cmn(b ^ c ^ d, a, b, x, s, t);
}
function md_ii(a, b, c, d, x, s, t)
{
 return md_cmn(c ^ (b | (~d)), a, b, x, s, t);
}
/*
 * Calculate the HMAC-MD, of a key and some data
 */
function core_hmac_md(key, data)
{
 var bkey = strbinl(key);
 if(bkey.length > ) bkey = core_md(bkey, key.length * chrsz);
 var ipad = Array(), opad = Array();
 for(var i = ; i < ; i++) 
 {
 ipad[i] = bkey[i] ^ x;
 opad[i] = bkey[i] ^ xCCCC;
 }
 var hash = core_md(ipad.concat(strbinl(data)), + data.length * chrsz);
 return core_md(opad.concat(hash), + );
}
/*
 * Add integers, wrapping at ^. This uses -bit operations internally
 * to work around bugs in some JS interpreters.
 */
function safe_add(x, y)
{
 var lsw = (x & xFFFF) + (y & xFFFF);
 var msw = (x >> ) + (y >> ) + (lsw >> );
 return (msw << ) | (lsw & xFFFF);
}
/*
 * Bitwise rotate a -bit number to the left.
 */
function bit_rol(num, cnt)
{
 return (num << cnt) | (num >>> ( - cnt));
}
/*
 * Convert a string to an array of little-endian words
 * If chrsz is ASCII, characters > have their hi-byte silently ignored.
 */
function strbinl(str)
{
 var bin = Array();
 var mask = ( << chrsz) - ;
 for(var i = ; i < str.length * chrsz; i += chrsz)
 bin[i>>] |= (str.charCodeAt(i / chrsz) & mask) << (i%);
 return bin;
}
/*
 * Convert an array of little-endian words to a hex string.
 */
function binlhex(binarray)
{
 var hex_tab = hexcase ? "ABCDEF" : "abcdef";
 var str = "";
 for(var i = ; i < binarray.length * ; i++)
 {
 str += hex_tab.charAt((binarray[i>>] >> ((i%)*+)) & xF) +
 hex_tab.charAt((binarray[i>>] >> ((i%)* )) & xF);
 }
 return str;
}
/*
 * Convert an array of little-endian words to a base- string
 */
function binlb(binarray)
{
 var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
 var str = "";
 for(var i = ; i < binarray.length * ; i += )
 {
 var triplet = (((binarray[i >> ] >> * ( i %)) & xFF) << )
 | (((binarray[i+ >> ] >> * ((i+)%)) & xFF) << )
 | ((binarray[i+ >> ] >> * ((i+)%)) & xFF);
 for(var j = ; j < ; j++)
 {
 if(i * + j * > binarray.length * ) str += bpad;
 else str += tab.charAt((triplet >> *(-j)) & xF);
 }
 }
 return str;
}
</script> 
</BODY></HTML>

以上所述是小编给大家介绍的javascript实现base64 md5 sha1 密码加密,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Javascript 相关文章推荐
Javascript 圆角div的实现代码
Oct 15 Javascript
JavaScript Event学习第十章 一些可替换的事件对
Feb 10 Javascript
关于二级域名下使用一级域名下的COOKIE的问题
Nov 07 Javascript
JQuery $.each遍历JavaScript数组对象实例
Sep 01 Javascript
前端框架Vue.js构建大型应用浅析
Sep 12 Javascript
利用vue写todolist单页应用
Dec 15 Javascript
使用Angular CLI生成路由的方法
Mar 24 Javascript
微信小程序显示倒计时功能示例【测试可用】
Dec 03 Javascript
详解JavaScript中的函数、对象
Apr 01 Javascript
js实现点赞按钮功能的实例代码
Mar 06 Javascript
微信小程序实现打卡签到页面
Sep 21 Javascript
Openlayers学习之地图比例尺控件
Sep 28 Javascript
jQuery简单实现仿京东商城的左侧菜单效果代码
Sep 09 #Javascript
JavaScript中实现Map的示例代码
Sep 09 #Javascript
jQuery实现仿百度首页滑动伸缩展开的添加服务效果代码
Sep 09 #Javascript
javascript实现日期时间动态显示示例代码
Sep 08 #Javascript
使用JQuery在线制作ppt并在线演示源码特效
Sep 08 #Javascript
js带前后翻页的图片切换效果代码分享
Sep 08 #Javascript
jQuery实现带动画效果的多级下拉菜单代码
Sep 08 #Javascript
You might like
php文件下载处理方法分析
2015/04/22 PHP
windows下apache搭建php开发环境
2015/08/27 PHP
PHP常见错误提示含义解释(实用!值得收藏)
2016/04/25 PHP
一份老外写的XMLHttpRequest代码多浏览器支持兼容性
2007/01/11 Javascript
Mootools 1.2教程(3) 数组使用简介
2009/09/14 Javascript
打印json对象的内容及JSON.stringify函数应用
2013/03/29 Javascript
Javascript高级技巧分享
2014/02/25 Javascript
用jquery实现动画跳到顶部和底部(这个比较简单)
2014/09/01 Javascript
让angularjs支持浏览器自动填表
2014/11/10 Javascript
个人总结的一些JavaScript技巧、实用函数、简洁方法、编程细节
2015/06/10 Javascript
JS本地刷新返回上一页代码
2016/07/25 Javascript
搭建element-ui的Vue前端工程操作实例
2018/02/23 Javascript
浅谈vue首屏加载优化
2018/06/28 Javascript
js canvas实现写字动画效果
2018/11/30 Javascript
手写Vue源码之数据劫持示例详解
2021/01/04 Vue.js
Python和GO语言实现的消息摘要算法示例
2015/03/10 Python
分析并输出Python代码依赖的库的实现代码
2015/08/09 Python
解决python报错MemoryError的问题
2018/06/26 Python
解决Python print 输出文本显示 gbk 编码错误问题
2018/07/13 Python
python多线程并发让两个LED同时亮的方法
2019/02/18 Python
Python 3.6 中使用pdfminer解析pdf文件的实现
2019/09/25 Python
wxPython+Matplotlib绘制折线图表
2019/11/19 Python
python 操作hive pyhs2方式
2019/12/21 Python
tensorflow2.0保存和恢复模型3种方法
2020/02/03 Python
Python 随机生成测试数据的模块:faker基本使用方法详解
2020/04/09 Python
如何搭建pytorch环境的方法步骤
2020/05/06 Python
python如何遍历指定路径下所有文件(按按照时间区间检索)
2020/09/14 Python
UGG澳洲官网:UGG Australia
2018/04/26 全球购物
任课老师推荐信范文
2013/11/24 职场文书
应届生求职自荐信范文
2014/04/07 职场文书
房地产公司见习自我鉴定
2014/04/28 职场文书
水电维修专业推荐信
2014/09/06 职场文书
2014年社区矫正工作总结
2014/11/18 职场文书
幼儿园老师个人总结
2015/02/28 职场文书
如何写观后感
2015/06/19 职场文书
解决vue-router的beforeRouteUpdate不能触发
2022/04/14 Vue.js