【发布时间】:2017-09-30 16:13:51
【问题描述】:
我创建了一个用于散列和加盐密码的实用程序类。然后我将用户的密码存储在用户表中的 SQL 数据库中。我想使用 EL 从数据库中提取密码,解密并在 JSP 中显示。如何解密从数据库中取回的密码?这是实用程序类:
公共类 PasswordUtil {
/* This code uses SHA-256. If this algorithm isn't available to you,
you can try a weaker level of encryption such as SHA-128.
*/
public static String hashPassword(String password)
throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.reset();
md.update(password.getBytes());
byte[] mdArray = md.digest();
StringBuilder sb = new StringBuilder(mdArray.length * 2);
for (byte b : mdArray) {
int v = b & 0xff;
if (v < 16) {
sb.append('0');
}
sb.append(Integer.toHexString(v));
}
return sb.toString();
}
public static String getSalt() {
Random r = new SecureRandom();
byte[] saltBytes = new byte[32];
r.nextBytes(saltBytes);
return Base64.getEncoder().encodeToString(saltBytes);
}
public static String hashAndSaltPassword(String password)
throws NoSuchAlgorithmException {
String salt = getSalt();
return hashPassword(password + salt);
}
public static void checkPasswordStrength(String password) throws Exception {
if (password == null || password.trim().isEmpty()) {
throw new Exception("Password cannot be empty.");
} else if (password.length() < 8) {
throw new Exception("Password is to short. " +
"Must be at least 8 characters long.");
}
}
public static boolean validatePassword(String password) {
try {
checkPasswordStrength(password);
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
return true;
}
}
这是我想在其上显示解密密码的 JSP(为简洁起见,只是来自 JSP 的表格):
<table>
<tr>
<td class="alignRight">First Name:</td>
<td>${user.firstName}</td>
</tr>
<tr>
<td class="alignRight">Last Name:</td>
<td>${user.lastName}</td>
</tr>
<tr>
<td class="alignRight">Phone Number:</td>
<td>${user.phone}</td>
</tr>
<tr>
<td class="alignRight">Address:</td>
<td>${user.address}</td>
</tr>
<tr>
<td class="alignRight">City:</td>
<td>${user.city}</td>
</tr>
<tr>
<td class="alignRight">State:</td>
<td>${user.state}</td>
</tr>
<tr>
<td class="alignRight">Zipcode:</td>
<td>${user.zip}</td>
</tr>
<tr>
<td class="alignRight">Email:</td>
<td>${user.email}</td>
</tr>
<tr>
<td class="alignRight">Your user name is:</td>
<td>${user.userName}</td>
</tr>
<tr>
<td class="alignRight">Temporary password:</td>
<td>${user.password}</td>
</tr>
</table>
【问题讨论】:
-
我想使用 EL 从数据库中提取密码,解密并显示在 JSP 中。 这看起来像 真的糟糕 想法。
-
老实说,不解密密码要好得多(更安全),而是将一种加密与数据库中保存的另一种加密进行比较以进行匹配。
-
您不必解散密码即可实现该目标。您只需要在哈希之前显示原始密码。
-
不要加密密码,当攻击者得到数据库时,他也会得到加密密钥。仅使用散列函数是不够的,仅添加盐对提高安全性无济于事。使用随机盐在 HMAC 上迭代大约 100 毫秒,然后将盐与哈希一起保存。使用
password_hash、PBKDF2、Bcrypt等函数或类似函数。关键是让攻击者花费大量时间通过蛮力寻找密码。 -
我没有被要求取消散列,但我想知道如果你需要,你会怎么做散列的整个目的是防止人们完全这样做那。 hashing != encryption.
标签: java jsp encryption hash salt