mongodb,node.js和加密数据

时间:2022-03-31 18:27:54

I'm working on a project which involves a lot of encrypted data. Basically, these are JSON objects serialized into a String, then encrypted with AES256 into a Cyphertext, and then have to be stored in Mongo. I could of course do this the way described above, which will store the cyphertext as String into a BSON document. However, this way, if for some reason along the way the Cyphertext isn't treated properly (for instance, different charset or whatever reason), the cyphertext is altered and I cannot rebuild the original string anymore. With millions of records, that's unacceptable (it's also slow).

我正在开发一个涉及大量加密数据的项目。基本上,这些是序列化为String的JSON对象,然后用AES256加密成Cyphertext,然后必须存储在Mongo中。我当然可以按照上述方式执行此操作,它将密文作为String存储到BSON文档中。但是,这样一来,如果出于某种原因,Cyphertext没有得到正确处理(例如,不同的字符集或任何原因),密文会被改变,我不能再重建原始字符串了。有数百万条记录,这是不可接受的(它也很慢)。

Is there a proper way to save the cyphertext in some kind of native binary format, retrieve it binary and then return it to the original string? I'm used to working with strings, my skills with binary format are pretty rusty. I'm very interested in hearing your thoughts on the subject.

有没有一种正确的方法来保存某种原生二进制格式的密文,检索二进制文件然后将其返回到原始字符串?我习惯使用字符串,我的二进制格式技巧非常生疏。我很想听听你对这个问题的看法。

Thanks everyone for your input,

谢谢各位的意见,

F*

法比安

1 个解决方案

#1


4  

yes :)

是的:)

var Binary = require('mongodb').Binary;
var doc = {
  data: new Binary(new Buffer(256))
}

or with 1.1.5 of the driver you can do

var doc = {
  data: new Buffer(256)
}

The data is always returned as a Binary object however and not a buffer. The link to the docs is below.

数据始终作为二进制对象返回,而不是缓冲区。文档的链接如下。

http://mongodb.github.com/node-mongodb-native/api-bson-generated/binary.html

http://mongodb.github.com/node-mongodb-native/api-bson-generated/binary.html

#1


4  

yes :)

是的:)

var Binary = require('mongodb').Binary;
var doc = {
  data: new Binary(new Buffer(256))
}

or with 1.1.5 of the driver you can do

var doc = {
  data: new Buffer(256)
}

The data is always returned as a Binary object however and not a buffer. The link to the docs is below.

数据始终作为二进制对象返回,而不是缓冲区。文档的链接如下。

http://mongodb.github.com/node-mongodb-native/api-bson-generated/binary.html

http://mongodb.github.com/node-mongodb-native/api-bson-generated/binary.html