0

I came to know about jBCrypt for hashing a password and storing in DB. But I didnt find any option to get back the actual value from the hashed value. Only BCrypt.checkpw(password, hashedPassword) is available which is returning boolean.http://www.mindrot.org/projects/jBCrypt/ How can I get the actual value out of hashed value. If it is not possible in jBCrypt, is there any other way to encrypt and decrypt values in java? Thanks in advance...

4

3 回答 3

1

除了使用散列函数,您还可以使用对称加密算法,如 提供的Spring Security,来自他们的Crypto Module,更具体地说是他们的Encryptors类。

这是一个基本的加密/解密示例:

public static void main(String[] args) {
        final String salt = UUID.randomUUID().toString().replace("-", "");
        TextEncryptor textEncryptor = Encryptors.delux("my-super-secure-password-for-the-encryptor", salt);
        final String passwordToBeEncrypted = "my-secure-password-to-be-encrypted";
        final String encrypted = textEncryptor.encrypt(passwordToBeEncrypted);
        textEncryptor.decrypt(encrypted);
        System.out.println(passwordToBeEncrypted.equalsIgnoreCase(textEncryptor.decrypt(encrypted)));
}

在这里,我使用的是delux. 根据他们的文档:

创建一个使用“更强”的基于密码的加密的文本加密器。

请记住,这是一种非常幼稚的加密和解密方法。

我不建议您将此解决方案复制粘贴到您的生产代码中。

为了使该功能能够投入生产,您希望将提供给 的密码Encryptors.delux()存储在安全的地方。此外,您还希望使用不同的方式为您的密码生成一个 salt(可能为每个新密码加密生成一个 salt)并将其存储以供以后解密密码的地方使用。

此外,您可能不希望将密码保留为纯文本(字符串),而是将其保留为char[]or byte[],但这应该从您可以开始的地方开始。

还有一个不同的库,来自 Apache,Apache Commons Crypto,它使用与Spring Crypto.

请记住,使用库而不是自己实现会更安全,因为使用包javax.crypto将要求您知道自己在做什么,并且不会造成不必要的伤害。

旁注:您可能会遇到 jdk 限制为 128 位的情况。要从 256 位中受益,请确保添加Java Cryptography Extension

于 2018-08-27T09:41:36.190 回答
0

The definition of a hash function has resistance to preimages: given h(x), it should be impossible to recover x. A hash function being "reversible" is the exact opposite of that property. Therefore, you cannot reverse hash function hence it is not possible to get actual value from hashed value.You cannot get x from h(x),only thing you can do is for the coming new password y compute h(y) and see if it is equal to h(x).

Not just jBcrypt any secured hash function won't provide this functionality of recovery

于 2018-08-27T09:22:11.697 回答
0

但是我没有找到任何从散列值中取回实际值的选项

嗯 - 这是密码散列函数的主要目的。

有没有其他方法可以在java中加密和解密值?提前致谢...

Java中有很多加密/解密值的示例,只需搜索它,即使在SO上也是如此。你不妨看看我关于 Java 加密的博客——它是关于基本的低级加密 API。

我希望您不是要对用户密码使用加密——即使是使密码可逆的远程可能性也会使您的系统有潜在的泄漏危险。

于 2018-08-27T10:44:09.233 回答