【发布时间】:2021-10-31 00:25:59
【问题描述】:
嘿,我有一个关于在我的数据库中存储加密数据的最佳方式的问题。我使用 MySQL 数据库 Node.js,并将 6.6.5 续集为 ORM。
这是我的工作:
- 使用 beforeCreate 和 beforeUpdate 挂钩,我正在加密我的数据 在将其存储到数据库之前。
- 使用 beforeFind 钩子我加密了查询之前的条件 这样做。
- 我使用 afterCreate、afterUpdate 和 afterFind 钩子解密
在为它创建更新或查询后使用它的数据。
但是查询本身给我带来了一些问题,我认为这些问题与我加密数据的方式有关。我将 Node.js 加密模块与 aes-256-cbc 算法和 random IV 一起用于每个加密。 使用随机 IV,每次加密都会产生不同的字符串。这就是为什么即使我使用 beforeFind 挂钩来加密我的条件,查询也永远不会返回任何结果。
myModel.create({myField: "someData"});
// with the beforeCreate hook encrypting this the database will contain something like this
// myField: "1ac4e952cf6207e5fd79630e0e82c901"
myModel.findAll({ where: { myField: "someData" } });
// The beforeFind hook encrypts this condition but as mentioned the result is not the same
// as the encrpyted value in the database
// It will look something like this:
// { where: { myField: "e203a4e22cf654w5fd7390300ef2c2f2" } }
// Because "1ac4e952cf6207e5fd79630e0e82c901" != "e203a4e22cf654w5fd7390300ef2c2f2"
// the query results in null
我显然可以使用相同的 IV 来加密我的数据,这会导致对同一来源的每次加密都会产生相同的加密字符串,但如果有任何其他方法可以让它像这样工作,我宁愿不这样做.
所以基本上我的两个问题是:
有没有办法通过使用随机 IV 的加密来完成这项工作? 或者有没有更好的方法将加密的数据存储在数据库中?
提前谢谢大家!
【问题讨论】:
标签: mysql encryption sequelize.js node-crypto sequelize-hooks