实现密码加密,采用SHA安全算法

This commit is contained in:
F嘉阳
2018-01-19 22:54:14 +08:00
parent 6cecf8e1a6
commit c5b41b0394
8 changed files with 240 additions and 96 deletions

View File

@@ -20,5 +20,6 @@
</attributes> </attributes>
</classpathentry> </classpathentry>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/mysql-connector-java-5.1.45-bin.jar"/> <classpathentry kind="lib" path="WebRoot/WEB-INF/lib/mysql-connector-java-5.1.45-bin.jar"/>
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-codec-1.11.jar"/>
<classpathentry kind="output" path="WebRoot/WEB-INF/classes"/> <classpathentry kind="output" path="WebRoot/WEB-INF/classes"/>
</classpath> </classpath>

Binary file not shown.

View File

@@ -87,7 +87,7 @@ String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.
<div class="input-field col s6 m6 l6"> <div class="input-field col s6 m6 l6">
<p class="margin right-align medium-small"><a href="forgot-password.jsp">Forgot password?</a></p> <p class="margin right-align medium-small"><a href="forgot-password.jsp">Forgot password?</a></p>
</div> </div>
</div>
</form> </form>
</div> </div>
</div> </div>

View File

@@ -38,101 +38,116 @@
</body> </body>
</html> --%> </html> --%>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no"> <meta name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
<title>skic用户注册</title> <title>skic用户注册</title>
<!-- <link rel="stylesheet" type="text/css" href="css/normalize.css" /> --> <!-- <link rel="stylesheet" type="text/css" href="css/normalize.css" /> -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.1/css/materialize.min.css"> <link rel="stylesheet"
<style type="text/css"> href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.1/css/materialize.min.css">
html, <style type="text/css">
body { html, body {
height: 100%; height: 100%;
} }
html {
display: table; html {
margin: auto; display: table;
} margin: auto;
body { }
display: table-cell;
vertical-align: middle; body {
color:#404d5b; display: table-cell;
} vertical-align: middle;
.margin { color: #404d5b;
margin: 0 !important; }
}
.card-panel{ min-width:350px;} .margin {
</style> margin: 0 !important;
<!--[if IE]> }
.card-panel {
min-width: 350px;
}
</style>
<!--[if IE]>
<script src="http://apps.bdimg.com/libs/html5shiv/3.7/html5shiv.min.js"></script> <script src="http://apps.bdimg.com/libs/html5shiv/3.7/html5shiv.min.js"></script>
<![endif]--> <![endif]-->
</head> </head>
<body class=" cyan darken-1"> <body class=" cyan darken-1">
<div id="login-page" class="row"> <div id="login-page" class="row">
<div class="col s12 z-depth-6 card-panel"> <div class="col s12 z-depth-6 card-panel">
<form class="login-form" action="<%=request.getContextPath()%>/CheckRegisterServlet" method="post" name="loginTest"> <form class="login-form"
<div class="row"> action="<%=request.getContextPath()%>/CheckRegisterServlet"
<div class="input-field col s12 center"> method="post" name="loginTest">
<img src="http://w3lessons.info/logo.png" alt="" class="responsive-img valign profile-image-login"> <div class="row">
<p class="center login-form-text">Welcome to skic</p> <div class="input-field col s12 center">
</div> <img src="http://w3lessons.info/logo.png" alt=""
</div> class="responsive-img valign profile-image-login">
<div class="row margin"> <p class="center login-form-text">Welcome to skic</p>
<div class="input-field col s12"> </div>
<i class="mdi-social-person-outline prefix"></i> </div>
<input name="userName" type="text" class="validate"> <div class="row margin">
<label for="username" class="center-align">Username</label> <div class="input-field col s12">
</div> <i class="mdi-social-person-outline prefix"></i> <input
</div> name="userName" type="text" class="validate"> <label
<div class="row margin"> for="username" class="center-align">Username</label>
<div class="input-field col s12"> </div>
<i class="mdi-social-person-outline prefix"></i> </div>
<input name="realName" type="text" class="validate"> <div class="row margin">
<label for="username" class="center-align">Realname</label> <div class="input-field col s12">
</div> <i class="mdi-social-person-outline prefix"></i> <input
</div> name="realName" type="text" class="validate"> <label
<div class="row margin"> for="username" class="center-align">Realname</label>
<div class="input-field col s12"> </div>
<i class="mdi-communication-email prefix"></i> </div>
<input name="email" type="email" class="validate"> <div class="row margin">
<label for="email" class="center-align">Email</label> <div class="input-field col s12">
</div> <i class="mdi-communication-email prefix"></i> <input name="email"
</div> type="email" class="validate"> <label for="email"
<div class="row margin"> class="center-align">Email</label>
<div class="input-field col s12"> </div>
<i class="mdi-action-lock-outline prefix"></i> </div>
<input type="tel" name="tel" class="validate"> <div class="row margin">
<label for="username" class="center-align">PhoneNum</label> <div class="input-field col s12">
</div> <i class="mdi-action-lock-outline prefix"></i> <input type="tel"
</div> name="tel" class="validate"> <label for="username"
<div class="row margin"> class="center-align">PhoneNum</label>
<div class="input-field col s12"> </div>
<i class="mdi-action-lock-outline prefix"></i> </div>
<input id="password" type="password" class="validate"> <div class="row margin">
<label for="password">Password</label> <div class="input-field col s12">
</div> <i class="mdi-action-lock-outline prefix"></i> <input
</div> name="passWord" type="password" class="validate"> <label
<div class="row margin"> for="password">Password</label>
<div class="input-field col s12"> </div>
<i class="mdi-action-lock-outline prefix"></i> </div>
<input id="password-again" type="password"> <div class="row margin">
<label for="password-again">Re-type password</label> <div class="input-field col s12">
</div> <i class="mdi-action-lock-outline prefix"></i> <input
</div> name="passWord2" type="password"> <label
<div class="row"> for="password-again">Re-type password</label>
<div class="input-field col s12"> </div>
<a href="register.html" class="btn waves-effect waves-light col s12">register</a> </div>
</div> <div class="row">
<div class="input-field col s12"> <div class="input-field col s12">
<p class="margin center medium-small sign-up">Have an account? <a href="login.jsp">Login</a></p> <button class="btn waves-effect waves-light col s12" type="submit"
</div> name="action">register</button>
</div> </div>
</form> <div class="input-field col s12">
</div> <p class="margin center medium-small sign-up">
</div> Have an account? <a href="login.jsp">Login</a>
</p>
</div>
</div>
</form>
</div>
</div>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script> <script type="text/javascript"
src="http://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<!--materialize js--> <!--materialize js-->
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.1/js/materialize.min.js"></script> <script
src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.97.1/js/materialize.min.js"></script>
</body> </body>
</html> </html>

View File

@@ -0,0 +1,91 @@
package com.coding.util;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class Coding {
public static final String KEY_SHA = "SHA";
public static final String KEY_MD5 = "MD5";
public static final String KEY_MAC = "HmacMD5";
// sun不推荐使用它们自己的base64,用apache的挺好
/**
* BASE64解密
*/
public static byte[] decryptBASE64(byte[] dest) {
if (dest == null) {
return null;
}
return Base64.decodeBase64(dest);
}
/**
* BASE64加密
*/
public static byte[] encryptBASE64(byte[] origin) {
if (origin == null) {
return null;
}
return Base64.encodeBase64(origin);
}
/**
* MD5加密
*
* @throws NoSuchAlgorithmException
*/
public static byte[] encryptMD5(byte[] data) throws NoSuchAlgorithmException {
if (data == null) {
return null;
}
MessageDigest md5 = MessageDigest.getInstance(KEY_MD5);
md5.update(data);
return md5.digest();
}
/**
* SHA加密
*
* @throws NoSuchAlgorithmException
*/
public static byte[] encryptSHA(byte[] data) throws NoSuchAlgorithmException {
if (data == null) {
return null;
}
MessageDigest sha = MessageDigest.getInstance(KEY_SHA);
sha.update(data);
return sha.digest();
}
/**
* 初始化HMAC密钥
*
* @throws NoSuchAlgorithmException
*/
public static String initMacKey() throws NoSuchAlgorithmException {
KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_MAC);
SecretKey secretKey = keyGenerator.generateKey();
return new String(encryptBASE64(secretKey.getEncoded()));
}
/**
* HMAC加密
*
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
*/
public static byte[] encryptHMAC(byte[] data, String key) throws NoSuchAlgorithmException, InvalidKeyException {
SecretKey secretKey = new SecretKeySpec(decryptBASE64(key.getBytes()), KEY_MAC);
Mac mac = Mac.getInstance(secretKey.getAlgorithm());
mac.init(secretKey);
return mac.doFinal(data);
}
}

View File

@@ -0,0 +1,18 @@
package com.coding.util;
import java.math.BigInteger;
public class CodingTest {
public static void main(String[] args) throws Exception {
String data = "简单加密2";
System.out.println(new BigInteger(Coding.encryptBASE64(data.getBytes())).toString(16));
System.out.println(new BigInteger(Coding.encryptBASE64(data.getBytes())).toString(32));
System.out.println(new String(Coding.decryptBASE64(Coding.encryptBASE64(data.getBytes()))));
System.out.println(new BigInteger(Coding.encryptMD5(data.getBytes())).toString());
System.out.println(new BigInteger(Coding.encryptSHA(data.getBytes())).toString());
System.out.println(new BigInteger(Coding.encryptHMAC(data.getBytes(), Coding.initMacKey())).toString());
}
}

View File

@@ -1,6 +1,8 @@
package com.skic.servlet; package com.skic.servlet;
import java.io.IOException; import java.io.IOException;
import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
import java.util.Map; import java.util.Map;
import javax.servlet.RequestDispatcher; import javax.servlet.RequestDispatcher;
@@ -9,6 +11,7 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import com.coding.util.Coding;
import com.skic.DTO.UserDTO; import com.skic.DTO.UserDTO;
import com.skic.service.CheckLoginService; import com.skic.service.CheckLoginService;
@@ -35,6 +38,13 @@ public class CheckLoginServlet extends HttpServlet {
request.setCharacterEncoding("utf-8"); request.setCharacterEncoding("utf-8");
String userName = request.getParameter("userName"); String userName = request.getParameter("userName");
String passWord = request.getParameter("passWord"); String passWord = request.getParameter("passWord");
String passWordBase64 = null;
try {
passWordBase64 = new BigInteger(Coding.encryptSHA(passWord.getBytes())).toString();//对密码进行加密比较
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String adminflag = ""; String adminflag = "";
String vipflag = ""; String vipflag = "";
String rootflag = ""; String rootflag = "";
@@ -47,7 +57,7 @@ public class CheckLoginServlet extends HttpServlet {
}else { }else {
UserDTO user = new UserDTO(); UserDTO user = new UserDTO();
user.setUserName(userName); user.setUserName(userName);
user.setPassword(passWord); user.setPassword(passWordBase64);//比对加密后的密码
Map<String, Integer> rsMap = cks.CheckUser(user); Map<String, Integer> rsMap = cks.CheckUser(user);
request.getSession().setAttribute("login_status", rsMap.get("login_status"));//存储登录状态 request.getSession().setAttribute("login_status", rsMap.get("login_status"));//存储登录状态
if (rsMap.get("login_status")==1) { if (rsMap.get("login_status")==1) {

View File

@@ -1,6 +1,8 @@
package com.skic.servlet; package com.skic.servlet;
import java.io.IOException; import java.io.IOException;
import java.math.BigInteger;
import java.security.NoSuchAlgorithmException;
import javax.servlet.RequestDispatcher; import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException; import javax.servlet.ServletException;
@@ -8,6 +10,7 @@ import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import com.coding.util.Coding;
import com.skic.DTO.UserDTO; import com.skic.DTO.UserDTO;
import com.skic.service.CheckRegisterService;; import com.skic.service.CheckRegisterService;;
@@ -30,12 +33,18 @@ public class CheckRegisterServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response); doPost(request, response);
} }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
request.setCharacterEncoding("utf-8"); request.setCharacterEncoding("utf-8");
String userName = request.getParameter("userName"); String userName = request.getParameter("userName");
String realName = request.getParameter("realName"); String realName = request.getParameter("realName");
String email = request.getParameter("email"); String email = request.getParameter("email");
String passWord = request.getParameter("passWord1"); String passWord = request.getParameter("passWord");
String passWordBase64 = null;
try {
passWordBase64 = new BigInteger(Coding.encryptSHA(passWord.getBytes())).toString();//对密码进行加密处理
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
String tel = request.getParameter("tel"); String tel = request.getParameter("tel");
RequestDispatcher rd = null; RequestDispatcher rd = null;
String forwardString = null; String forwardString = null;
@@ -46,7 +55,7 @@ public class CheckRegisterServlet extends HttpServlet {
}else { }else {
UserDTO user = new UserDTO(); UserDTO user = new UserDTO();
user.setUserName(userName); user.setUserName(userName);
user.setPassword(passWord); user.setPassword(passWordBase64);
user.setRealName(realName); user.setRealName(realName);
user.setEmail(email);; user.setEmail(email);;
user.setPhoneNum(tel); user.setPhoneNum(tel);