如何加密需要在node.js中解密的数据? [英] How to encrypt data that needs to be decrypted in node.js?

查看:67
本文介绍了如何加密需要在node.js中解密的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用 bcrypt 对不需要解密的密码和数据进行哈希处理.我们应该怎么做才能保护需要解密的其他用户信息?

We are using bcrypt for hashing passwords and data that never needs to be decrypted. What should we do to protect other user information that does need to be decrypted?

例如,假设我们不希望用户的真实姓名采用纯文本格式,以防有人获取对数据库的访问权.这是一些敏感数据,但还需要不时调用并以纯文本显示.有没有简单的方法可以做到这一点?

For example, let's say that we didn't want a user's real name to be in plain text in case someone was to obtain access to the database. This is somewhat sensitive data but also needs to be called from time to time and displayed in plain text. Is there a simple way to do this?

推荐答案

您可以使用加密模块:

var crypto = require('crypto');
var assert = require('assert');

var algorithm = 'aes256'; // or any other algorithm supported by OpenSSL
var key = 'password';
var text = 'I love kittens';

var cipher = crypto.createCipher(algorithm, key);  
var encrypted = cipher.update(text, 'utf8', 'hex') + cipher.final('hex');
var decipher = crypto.createDecipher(algorithm, key);
var decrypted = decipher.update(encrypted, 'hex', 'utf8') + decipher.final('utf8');

assert.equal(decrypted, text);

修改

现在已弃用 createCipher createDecipher ,而使用 createCipheriv createDecipheriv

Now createCipher and createDecipher is deprecated instead use createCipheriv and createDecipheriv

这篇关于如何加密需要在node.js中解密的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆