简体   繁体   中英

Encrypt Data Based on Hashed Password

I want to use aes 256 bit to encrypt data based on a hashed password. The problem is that hashes like argon2 and bcrypt automatically add a salt, so the password is different each time. 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 . If you have aes.encrypt in some library, the chance is high that there is some kind of key derivation also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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