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 相关文章推荐
jquery easyui的tabs使用时的问题
Mar 23 Javascript
可选择和输入的下拉列表框示例
Nov 05 Javascript
jQuery中:last-child选择器用法实例
Dec 31 Javascript
JavaScript 事件绑定及深入
Apr 13 Javascript
轻松学习jQuery插件EasyUI EasyUI表单验证
Dec 01 Javascript
javascript中Date format(js日期格式化)方法小结
Dec 17 Javascript
实例讲解jquery与json的结合
Jan 07 Javascript
在IE8上JS实现combobox支持拼音检索功能
May 23 Javascript
ES6使用let命令更简单的实现块级作用域实例分析
Mar 31 Javascript
AngularJs 常用的过滤器
May 15 Javascript
基于iScroll实现内容滚动效果
Mar 21 Javascript
Ajax常用封装库——Axios的使用
May 08 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将数据库中的电话号码读取出来并生成图片
2008/08/31 PHP
php Hex RGB颜色值互换的使用
2013/05/10 PHP
php 根据url自动生成缩略图并处理高并发问题
2014/01/23 PHP
jQuery的animate函数实现图文切换动画效果
2015/05/03 Javascript
jQuery实现仿百度帖吧头部固定导航效果
2015/08/07 Javascript
js表单验证实例讲解
2016/03/31 Javascript
vue2.0使用Sortable.js实现的拖拽功能示例
2017/02/21 Javascript
微信小程序报错:this.setData is not a function的解决办法
2017/09/27 Javascript
微信小程序实现导航栏选项卡效果
2020/06/19 Javascript
vue短信验证性能优化如何写入localstorage中
2018/04/25 Javascript
vue如何实现自定义底部菜单栏
2019/07/01 Javascript
JavaScript实现拖动对话框效果的实现代码
2020/10/12 Javascript
在Linux下使用Python的matplotlib绘制数据图的教程
2015/06/11 Python
在Python的Django框架中编写错误提示页面
2015/07/22 Python
python和ruby,我选谁?
2017/09/13 Python
Ubuntu下使用python读取doc和docx文档的内容方法
2018/05/08 Python
Python中的Socket 与 ScoketServer 通信及遇到问题解决方法
2019/04/01 Python
Python3显示当前时间、计算时间差及时间加减法示例代码
2019/09/07 Python
什么是Python变量作用域
2020/06/03 Python
使用keras实现Precise, Recall, F1-socre方式
2020/06/15 Python
Django+RestFramework API接口及接口文档并返回json数据操作
2020/07/12 Python
Matplotlib 绘制饼图解决文字重叠的方法
2020/07/24 Python
用CSS3的box-reflect设置文字倒影效果的方法讲解
2016/03/07 HTML / CSS
HTML5标签大全
2016/11/23 HTML / CSS
Clarks鞋法国官方网站:英国其乐鞋品牌
2018/02/11 全球购物
ProBikeKit德国:在线公路自行车专家
2018/06/03 全球购物
英国网上购买门:Direct Doors
2018/06/07 全球购物
班级年度安全计划书
2014/05/01 职场文书
卫生标语大全
2014/06/21 职场文书
学校班子个人对照检查材料思想汇报
2014/09/27 职场文书
2014年房地产工作总结范文
2014/11/19 职场文书
2014年科室工作总结范文
2014/12/19 职场文书
2016年秋季运动会加油稿
2015/12/21 职场文书
机械生产实习心得体会
2016/01/22 职场文书
CSS完成视差滚动效果
2021/04/27 HTML / CSS
Golang ort 中的sortInts 方法
2022/04/24 Golang