简体   繁体   English

基于哈希密码加密数据

[英]Encrypt Data Based on Hashed Password

I want to use aes 256 bit to encrypt data based on a hashed password. 我想使用AES 256位基于哈希密码对数据进行加密。 The problem is that hashes like argon2 and bcrypt automatically add a salt, so the password is different each time. 问题在于,像argon2和bcrypt之类的哈希会自动添加一个盐,因此每次的密码都不相同。 Is there any way to do this? 有什么办法吗?

Here's an example of what I tried: 这是我尝试过的一个示例:

const aes = require("aes256");
const argon2 = require("argon2");
const pass = process.argv[2];
const data = process.argv[3];
argon2.hash(pass).then(result => console.log(aes.encrypt(result, data));

This produces a different output each time, so it isn't possible to decrypt the data unless the hash is saved. 每次都会产生不同的输出,因此除非保存哈希,否则无法解密数据。 (Which would be pointless since the goal is to prevent the data from being decrypted). (这将毫无意义,因为目标是防止数据被解密)。

You don't want to hash the password to produce a key -- you want to derive the key from a password. 您不想散列密码以产生密钥-您想从密码派生密钥。 There are standard ways to do this. 有标准的方法可以做到这一点。 Here is one example: PBKDF2 link to wikipedia . 这是一个示例: 指向Wikipedia的 PBKDF2 链接 If you have aes.encrypt in some library, the chance is high that there is some kind of key derivation also. 如果您在某些库中具有aes.encrypt,则也存在某种密钥派生的机会很高。

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

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