在使用用户名密码进行登录时,有时候为了安全需要把密码加密进行传输。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>登录</title>
<script src="<%=request.getContextPath()%>/js/aes.js"></script>
<script type="text/javascript">
function submitForm(){
var theForm = document.forms[0];
if(theForm.j_username.value !='' && theForm.j_password.value !=''){
if(parent!=null){
theForm.target = "_parent";
theForm.j_password.value = getAES(theForm.j_password.value);
theForm.submit();
}
}
}
function getAES(string){
var key = 'zxmd2013qazwsx12'; //密钥
var iv = 'qazwsx12zxmd2013';
var encrypted =getAesString(string,key,iv); //密文
return encrypted;
}
function getAesString(data,key,iv){
var key = CryptoJS.enc.Utf8.parse(key);
var iv = CryptoJS.enc.Utf8.parse(iv);
var encrypted =CryptoJS.AES.encrypt(data,key,{
iv:iv,
mode:CryptoJS.mode.CBC,
padding:CryptoJS.pad.Pkcs7
});
return encrypted.toString(); //返回的是base64格式的密文
}
</script>
</head>
<body>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="center" valign="middle" width="10000px">
<form method="post" id="loginForm" action="<%=request.getContextPath()%>/LoginServlet">
<table width="283px" height="80px" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="68" height="34" align="right">用户名</td>
<td width="215">
<input name="j_username" id="j_username" type="text" />
</td>
</tr>
<tr>
<td align="right">密码</td>
<td>
<input name="j_password" id="j_password" autocomplete="off" type="password" />
</td>
</tr>
<tr>
<td colspan="2" height="45px" align="center">
<input name="" id="templateId" οnclick="submitForm()" type="button" value="登录" />
<input name="input2" type="button" value="取消" οnclick="javascript:this.form.reset();" />
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
在后台取数据时
request.getParameter("j_username");//用户名
request.getParameter("j_password");//密文密码