我目前正在开发一个允许用户保存敏感日期的应用程序。由于它是我们正在使用的 Web 应用程序NodeJS
并MongoDB
用于持久性。(顺便说一句,我对 Node 和 NoSQL 完全陌生)
我们确实有可以存储某种病史的用户。姓名和电子邮件存储在用户文档中,而其他内容存储在配置文件中。为了提高安全性,我希望encrypt
用户引用他的个人资料,反之亦然。
目前我正在使用Crypto
库NodeJS
来加密(AES256)user_id
用户配置文件中的引用。因此,引用不再是 ObjectID 的一种类型,而是一个字符串
因此,通过直接查看数据库,无法检查哪个配置文件属于哪个用户。密钥encrypt
和decrypt
用户 ID 存储在服务器的 js 文件中的某个位置NodeJS
。
这是一种常见/好方法还是我做错了什么?有没有更好的方法——我读到 mongoDB 不支持任何“内置加密”
至少,这里是加密/解密的代码
module.exports = function() {
this.encryptionSecret = "ANYSECRET";
this.crypto = require('crypto');
this.algorithm = 'aes256';
this.encrypt = function (key) {
var cipher = this.crypto.createCipher(this.algorithm, this.encryptionSecret);
var encrypted = cipher.update(""+key, 'utf8', 'hex') + cipher.final('hex');
return encrypted;
};
this.decrypt = function (encryptedKey) {
var decipher = this.crypto.createDecipher(this.algorithm, this.encryptionSecret);
var decrypted = decipher.update(encryptedKey, 'hex', 'utf8') + decipher.final('utf8');
return decrypted;
};
};