1

我正在使用 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 表中插入加密的用户密码。

4

1 回答 1

1

每次运行测试程序都会有所不同,因为StrongPasswordEncryptor每次启动期间都会创建并附加到类EncryptionUtil,因为它被标记为staticfinal。这意味着提供的 SALT 对于每个实例也是新的StrongPasswordEncryptor

您需要直接从文档中阅读此内容

在 Web 应用程序中,Jasypt 允许开发人员避免在 Web 应用程序内的文件中存储 PBE 加密器的加密密码,而是在每次部署应用程序时通过 Web 界面指定这些密码。

  • 特殊的 *Config 类:org.jasypt.encryption.pbe.WebPBEConfigorg.jasypt.encryption.pbe.WebStringPBEConfig,当分配给加密器时,“标记”这个加密器有资格通过网络接收其密码。

  • 一个上下文监听器, org.jasypt.web.pbeconfig.WebPBEInitializationContextListener它将让我们创建我们的加密器,设置它们的 WebPBEConfig 配置并将它们注册到我们应用程序的某个位置。如果我们使用 Spring 框架,则不需要此上下文侦听器。

  • 一个过滤器,org.jasypt.web.pbeconfig.WebPBEConfigFilter将避免任何用户访问 Web 应用程序,直到管理员设置加密密码。

  • 一个 servlet,org.jasypt.web.pbeconfig.WebPBEConfigServlet它将向授权用户显示一个表单,他/她可以使用该表单为所有具有 WebPBEConfig 的加密器设置加密密码。

请从文档中完整阅读

于 2014-05-19T13:22:01.027 回答