---
title: "JavaScript的MD5加密"
author: "hongan"
date: "2008-12-04T20:44:31Z"
tags: ["HTML", "IDEA", "JavaScript"]
slug: "javascript-md5"
draft: false
lastmod: "2008-12-04T20:44:31Z"
id: 287044
url: "https://hongan.iteye.com/blog/287044"
---

1.首先要到[http://pajhome.org.uk/crypt/md5/](http://pajhome.org.uk/crypt/md5/)下载js文件。

2.在页面文件中添加：

&lt;script type="text/javascript" src="md5.js"&gt;&lt;/script&gt;

3.使用函数hex\_md5()进行加密，例：

frm.pwd.value =hex\_md5(frm.pwd.value);

 

使用示例：

```
<html>
<head>
<script type="text/javascript" src="md5.js"></script>
<script>
function md5(){
  var hash=hex_md5(document.getElementById("text").value);
  document.getElementById("result").value=hash;
}
function trans(){
  frm.pwd.value =hex_md5(frm.pwd.value);
  document.getElementById("result2").value=frm.pwd.value;
}
function submit(){
  //默认的提交处理，自定义的提交前处理方法不要用submit作为函数名
}
//onsubmit="pwd.value =hex_md5(pwd.value)"
</script>

</head>
<body>
http://pajhome.org.uk/crypt/md5/<br>
MD5加密：<br>
转换前：<input type="text" id="text" style="width:300" value="hello world"/><br>
转换后：<input type="text" id="result" style="width:300"/><br>
<input type="button" value="test" onclick="md5()"/>
<br>
<form name="frm" >
name:<input type="text" id="name" /><br>
password:<input type="password" id="pwd"/><br>
<input type="button" value="submit" onclick="trans()" /><br>
加密后的密码：<input type="text" id="result2" style="width:300"/>
</form>
</body>
</html>
```