我在 NodeJs 上运行的 javascript 中有以下代码:
encryptPassword: function(password) {
if (!password || !this.salt) return '';
var salt = new Buffer(this.salt, 'base64');
return crypto.pbkdf2Sync(password, salt, 10000, 64).toString('base64');
}
如何实现解密功能? 它可以在 java 或 javascript 中。
谢谢!
最佳答案
PBKDF2 是一种单向哈希算法。无法解密生成的哈希。您可以阅读更多相关信息 here .
A one way hash performs a bunch of mathematical operations that transform input into a (mostly) unique output, called a digest. Because these operations are one way, you cannot ‘decrypt’ the output- you can’t turn a digest into the original input.
如果您想使用 PBKDF2 来存储和比较密码,您可能会对 pbkdf2
感兴趣图书馆。它使密码的生成和比较变得容易:
var pbkdf2 = require('pbkdf2');
var p = 'password';
var s = pbkdf2.generateSaltSync(32);
var pwd = pbkdf2.hashSync(p, s, 1, 20, 'sha256');
var bool = pbkdf2.compareSync(pwd, p, s, 1, 20, 'sha256');
关于javascript - 解密由 crypto.pbkdf2 对象创建的密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24563847/