我正在使用此代码来加密和解密密码
public class SecureDigester
{
private static final char digits[] =
{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
'F' };
private static String byteArrayToHexString(byte[] b)
{
StringBuffer hexString = new StringBuffer(b.length);
for (int i = 0; i < b.length; i++)
{
hexString.append(digits[(b[i] & 0xF0) >> 4]);
hexString.append(digits[b[i] & 0x0F]);
}
return hexString.toString();
}
public static String digest(String plaintext)
{
try
{
MessageDigest md = MessageDigest.getInstance("SHA");
md.update(plaintext.getBytes("UTF-8"));
byte[] mdBytes = md.digest();
String hashString = byteArrayToHexString(mdBytes);
return hashString;
} catch (Exception e)
{
throw new RuntimeException(e);
}
}
}
在我的登录中,我使用此代码来解密密码:
String passwordDigest = SecureDigester.digest(password);
if (!user.getPassword().equals(passwordDigest))
{
// authentication failed: bad password
}
现在我有了 forgot-password.jsp 页面,该页面将用户的用户名和密码发送到他/她指定的电子邮件。但是当我使用下面的代码时,我收到了一个加密密码,它也与我数据库中的加密密码不同。
String Email = req.getParameter("email");
User userItem = new UserDAO().findEmail(Email);
SendMailSSL sendEmail = new SendMailSSL();
String password = userItem.getPassword();
String EPassword = SecureDigester.digest(password);
sendEmail.send(userItem.getUsername(), EPassword, userItem.getEmail());
如何解决这个问题?