我在我的Web应用程序中使用jsp和servlet.我需要在数据库中存储密码.我发现哈希将是最好的方法.
我用这个代码来做.
String user = request.getParameter("Username");
String pass = request.getParameter("Password1");
String name = request.getParameter("Name");
String mail = request.getParameter("email");
String phone = request.getParameter("phone");
String add1 = request.getParameter("address1");
String add2 = request.getParameter("address2");
String country = request.getParameter("country");
Login login = new Login();
Account account = new Account();
login.setId(user);
login.setPassword(pass);
if (!(add1.equals(""))) {
account.setAddress1(add1);
}
if (!(add2.equals(""))) {
account.setAddress2(add2);
}
if (!(country.equals(""))) {
account.setCountry(country);
}
account.setId(user);
account.setMail_id(mail);
if (!(phone.equals(""))) {
account.setPhone_no(Long.parseLong(phone));
}
account.setName(name);
java.security.MessageDigest d = null;
d = java.security.MessageDigest.getInstance("SHA-1");
d.reset();
d.update(pass.getBytes("UTF-8"));
byte b[] = d.digest();
String tmp = (new BASE64Encoder()).encode(b);
account.setPassword(tmp);
account.setPrivilege(1);
LoginJpaController logcon = new LoginJpaController();
AccountJpaController acccon = new AccountJpaController();
logcon.create(login);
acccon.create(account);
session.setAttribute("user", user);
response.sendRedirect("dashboard.jsp");
} catch (NumberFormatException ex) {
out.println("Invalid data");
}
%>
当我试图打印tmp的值时,我得到了一些其他值.我猜它是密码的哈希值.但是当我将这些数据保存到数据库时,原始密码将保存在那里,而不是tmp中的值.
我使用java derby作为数据库.
问题是什么???