J2SE在java.security.MessageDigest提供了一個MD5、SHA摘要計算類。
結合javascript的md5計算,可以實現前臺口令加密,后臺數據庫中也保存的是md5或者sha的密碼加密摘要。
具體實現如下:
1、login.jsp部分
...
<script type="text/javascript" src='js/md5.js'>
</script>
<html:form action="/login" focus="userid" >
<table class="SubFormStyle">
<tr align=left>
<td><bean:message key="caption.LOGIN.UserID" /> <html:text
property="userid" value=""></html:text></td>
<td><bean:message key="caption.LOGIN.Password" /> <html:password
property="passwordinput" value=""></html:password>
<html:hidden
property="password" value="" ></html:hidden></td>
<td><html:submit onclick="password.value = hex_md5(passwordinput.value);">
<bean:message key="button.Login" />
</html:submit></td>
</tr>
</table>
</html:form>
...
2、我用的是struts,具體的action和form就不再浪費紙張了,我定義了一個loginuser的類來進行用戶密碼校驗,這里只給出校驗的方法:
/**
*
* 校驗密碼,密碼采用MD5算法加密。
*
* @Param PasswordInput, 待校驗密碼
* @Return 校驗通過返回true,否則返回false
*
*
*/
public boolean CheckPassword(String PasswordInput) throws ATError {
this.select();//從數據庫中讀取用戶信息
MessageDigest md;
try {
//生成一個MD5加密計算摘要
md = MessageDigest.getInstance("MD5");
//計算md5函數
md.update(this.password.getBytes());
//digest()最后確定返回md5 hash值,返回值為8為字符串。因為md5 hash值是16位的hex值,實際上就是8位的字符
//BigInteger函數則將8位的字符串轉換成16位hex值,用字符串來表示;得到字符串形式的hash值
String pwd = new BigInteger(1, md.digest()).toString(16);
if (PasswordInput.equals(pwd)) {
return true;
} else {
return false;
}
} catch (NoSuchAlgorithmException e) {
throw new ATError(e, "LoginUser", "CheckPassword", 1000);
}
}
md5.js下載:http://pajhome.org.uk/crypt/md5/index.html
posted on 2006-01-19 09:13
J2EE 閱讀(4281)
評論(3) 編輯 收藏