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中的 $(&quot;#jb51&quot;)与document.getElementById(&quot;jb51&quot;) 的区别
Jul 26 Javascript
使用jquery局部刷新(jquery.load)从数据库取出数据
Jan 22 Javascript
Underscore.js常用方法总结
Feb 28 Javascript
javascript获取select值的方法分析
Jul 02 Javascript
js实现简易的单数字随机抽奖(0-9)
Mar 19 Javascript
JS实现兼容性好,自动置顶的淘宝悬浮工具栏效果
Sep 18 Javascript
js 去掉字符串前后空格实现代码集合
Mar 25 Javascript
JavaScript 函数的定义-调用、注意事项
Apr 16 Javascript
基于node.js之调试器详解
Aug 22 Javascript
4个顶级JavaScript高级文本编辑器
Oct 10 Javascript
JavaScript常见事件对象与操作实例总结
Jan 05 Javascript
vue在线动态切换主题色方案
Mar 26 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
PHP7 新特性详细介绍
2016/09/06 PHP
发现的以前不知道的函数
2006/09/19 Javascript
JSQL SQLProxy 的 php 版本代码
2010/05/05 Javascript
javascript中this做事件参数相关问题解答
2013/03/17 Javascript
随窗体滑动的小插件sticky源码
2013/06/21 Javascript
异步动态加载js与css文件的js代码
2013/09/15 Javascript
Extjs4中Form的使用之本地hiddenfield
2013/11/26 Javascript
IE6-8中Date不支持toISOString的修复方法
2014/05/04 Javascript
jquery基础知识第一讲之认识jquery
2016/03/17 Javascript
jquery删除table当前行的实例代码
2016/10/07 Javascript
jQuery得到多个值只能用取Class ,不能用取ID的方法
2016/12/04 Javascript
Bootstrap标签页(Tab)插件使用方法
2017/03/21 Javascript
nodejs个人博客开发第四步 数据模型
2017/04/12 NodeJs
JavaScript订单操作小程序完整版
2017/06/23 Javascript
JavaScript中的连续赋值问题实例分析
2019/07/12 Javascript
使用ThinkJs搭建微信中控服务的实现方法
2019/08/08 Javascript
使用 Vue 实现一个虚拟列表的方法
2019/08/20 Javascript
JavaScript多种滤镜算法实现代码实例
2019/12/10 Javascript
javascript实现画板功能
2020/04/12 Javascript
原生JS实现音乐播放器
2021/01/26 Javascript
python 算法 排序实现快速排序
2012/06/05 Python
python中使用urllib2伪造HTTP报头的2个方法
2014/07/07 Python
Python单元测试框架unittest使用方法讲解
2015/04/13 Python
python实现决策树分类算法
2017/12/21 Python
python实现单链表中删除倒数第K个节点的方法
2018/09/28 Python
Python 中的lambda函数介绍
2018/10/10 Python
用python生成1000个txt文件的方法
2018/10/25 Python
Django框架封装外部函数示例
2019/05/28 Python
Python中模块(Module)和包(Package)的区别详解
2019/08/07 Python
Python实现RGB与HSI颜色空间的互换方式
2019/11/27 Python
OpenCV中VideoCapture类的使用详解
2020/02/14 Python
python实现网页录音效果
2020/10/26 Python
HTML5使用drawImage()方法绘制图像
2014/06/23 HTML / CSS
Bulk Powders意大利:运动补充在线商店
2019/02/09 全球购物
男方父母婚礼答谢词
2014/01/25 职场文书
儿童诗两首教学反思
2016/02/23 职场文书