我正在使用 Jasypt 的StrongPasswordEncryptor对用户的密码进行编码并进行匹配。
我创建了一个 util 类来调用它的 API:-
public class EncryptionUtil {
private static final StrongPasswordEncryptor passwordEncryptor = new StrongPasswordEncryptor();
/**
* Default private constructor.
*/
private EncryptionUtil() {
}
/**
* Encrypts a string using {@link StrongPasswordEncryptor}
*
* @param input
* Plain string
* @return encrypted string.
*/
public static final String encyptString(final String input) {
return passwordEncryptor.encryptPassword(input);
}
/**
* @see StrongPasswordEncryptor#checkPassword(String, String)
* @param plainPassword
* @param encryptedPassword
* @return boolean
*/
public static boolean checkPassword(String plainPassword, String encryptedPassword) {
return passwordEncryptor
.checkPassword(plainPassword, encryptedPassword);
}
}
尝试使用 Junit 测试它运行得非常好:-
@Test
public void test() {
String encryptedPassword = EncryptionUtil.encyptString("password");
Assert.assertNotNull(encryptedPassword);
Assert.assertTrue(EncryptionUtil.checkPassword("password",
encryptedPassword));
}
我将用户凭据存储在 SQL 表中。对于测试/开发环境,我在使用EncryptionUtil类加密密码后进行了 SQL 插入。
问题:-
一旦将凭据部署/运行到其他机器上,凭据匹配就会失败 - 当从数据库中获取凭据进行匹配时。
在使用 StrongPasswordEncryptor#encyptString 玩了一下之后,我发现对于相同的普通密码(比如“密码”),多次运行时生成的加密密码是不同的。
IE :-
@Test
public void test() {
String encryptedPassword = EncryptionUtil.encyptString("password");
System.out.println(encryptedPassword);
}
这将在每次运行时打印不同的加密字符串。我不是加密专家,但我相信 StrongPasswordEncryptor 使用的盐在每次运行时都不同。
我该如何解决 ?
我应该能够在通过凭据匹配流程的 SQL 表中插入加密的用户密码。