I messed up my passwords and saved them in a database. But I can not log in without decrypting the password. How to do it?
My code that tries to do this but doesn't work:
@RequestMapping(method = RequestMethod.POST)
public String processLogin(Person user, BindingResult result,
@RequestParam("userName") String username,
@RequestParam("password") String password) {
try {
password = Hex.encodeHexString(MessageDigest.getInstance("SHA-256").digest());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
ValidateUser(username, password);
String destination = "";
if (success == true) {
destination = "redirect:/person.html";
}
else {
destination = "redirect:/index.html";
}
return destination;
}
public boolean ValidateUser(String username, String password) {
List<Person> users = service.getAllPersons();
for (Person allUsers : users) {
if (allUsers.getUserName().equals(username) &&
allUsers.getPassword().equals(password)) {
success = true;
}
}
return success;
}
And here is my cry of md5:
public void setPassword(String password) {
String md5 = null;
try {
MessageDigest digest = MessageDigest.getInstance("MD5");
digest.update(password.getBytes(), 0, password.length());
md5 = new BigInteger(1, digest.digest()).toString(16);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
this.password = md5;
}
source
share