简体   繁体   English

解密由 crypto.pbkdf2 对象创建的密码

[英]Decrypt Password Created by crypto.pbkdf2 Object

I have the following code in javascript, running on NodeJs:我在 javascript 中有以下代码,在 NodeJs 上运行:

encryptPassword: function(password) {
    if (!password || !this.salt) return '';
    var salt = new Buffer(this.salt, 'base64');
    return crypto.pbkdf2Sync(password, salt, 10000, 64).toString('base64');
}

How can I implement the decrypt function?如何实现解密功能? It can be in java or in javascript.它可以在java或javascript中。

Thx!谢谢!

PBKDF2 is a one-way hashing algorithm. PBKDF2 是一种单向哈希算法。 It's not possible to decrypt the generated hash.无法解密生成的哈希。 You can read more about this 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.因为这些操作是一种方式,所以您不能“解密”输出——您不能将摘要转换为原始输入。

If you want to use PBKDF2 to store and compare passwords, you might be interested in the pbkdf2 library.如果您想使用 PBKDF2 来存储和比较密码,您可能会对pbkdf2库感兴趣。 It makes generation and comparison of passwords easy:它使密码的生成和比较变得容易:

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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM