猫鼬吸气剂和二传手不能正常运作

时间:2017-06-13 20:13:11

标签: node.js mongoose mongoose-schema node-crypto

我尝试在插入MongoDB之前和之后加密和解密值。我使用mongoose模式并调用get和set方法进行加密和解密。通过调用set方法对数据进行加密,但是从MongoDB检索数据时,它不会解密。这是我的架构和加密和解密方法:

var tempSchema = new Schema({    
  _id: {type: Schema.ObjectId, auto: true},
  name:{type: String},
  sample_details:{
    _id: false,
    objects: [{
      object_key:{type: String},
      object_value:{type: String, get:decrypt, set:encrypt}
    }],
    type_params:[{
      type_key:{type: String},
      type_value:{type: String, get:decrypt, set:encrypt}
    }],
    test_body:{type: String, get:decrypt, set:encrypt}
  }}, {
  toObject : {getters: true, setters: true},
  toJSON : {getters: true, setters: true}
});

以下是使用的加密和解密方法:

function encrypt(text){
     var cipher = crypto.createCipher('aes-256-cbc', secret_key);
     var crypted = cipher.update(text,'utf8','hex');
     crypted += cipher.final('hex');
     return crypted;
}

function decrypt(text){
     if (text === null || typeof text === 'undefined') {
         return text;
     };
     var decipher = crypto.createDecipher('aes-256-cbc', secret_key);
     var dec = decipher.update(text, 'hex', 'utf8');
     dec += decipher.final('utf8');
     return dec;
}

感谢任何帮助,object_value和type_value在保存到db时被加密,但在从db中检索时没有被解密。

2 个答案:

答案 0 :(得分:1)

我找到了this issue,这可能是你的问题。

如果objectstype_params作为对象(不是数组)传递,则getter正常工作。

我已经创建了代码片段以测试您的代码(希望,我已经正确地理解了它)。 encrypt()decrypt()被简化,因为它们在问题中无关紧要:

var tempSchema = new Schema({
    _id: {type: Schema.ObjectId, auto: true},
    name: {type: String},
    sample_details: {
        _id: false,
        objects: {
                object_key: {type: String},
                object_value: {type: String, get: decrypt, set: encrypt}
            },
        type_params: {
                type_key: {type: String},
                type_value: {type: String, get: decrypt, set: encrypt}
            },
        test_body: {type: String, get: decrypt, set: encrypt}
    }}, {
    toObject: {getters: true, setters: true},
    toJSON: {getters: true, setters: true},
    runSettersOnQuery: true
});


var tempModel = mongoose.model('tempmodel', tempSchema);

var dataToSave = {name: 'Name',
                  sample_details: {
                    objects: { //just an object here, no array
                            object_key: 'ObjKey',
                            object_value: 'ObjVal'
                        },
                    type_params: { //just an object here, no array
                            type_key: 'TypeKey',
                            type_value: 'TypeVal'
                        },
                    test_body: 'TestBody'
                  }
};

var doc = new tempModel(dataToSave);

doc.save().then((doc) => {
    console.log(util.inspect(doc, false, null));
    mongoose.disconnect();
});

function encrypt(text){
    return text + '_enc';
}

function decrypt(text){
    return text + '_dec';
}

控制台中的结果:

{ sample_details:
  { objects: { object_key: 'ObjKey', object_value: 'ObjVal_enc_dec' }, //'_dec' postfix is the result of decript()
    type_params: { type_key: 'TypeKey', type_value: 'TypeVal_enc_dec' }, //'_dec' postfix is the result of decript()
    test_body: 'TestBody_enc_dec' },
  name: 'Name',
  _id: 5aaec6c2b8f0701c9476420f,
  __v: 0,
  id: '5aaec6c2b8f0701c9476420f' }

答案 1 :(得分:0)

我遇到了类似的问题,并且对我有用:

  1. 将“转换为json时使用吸气剂”选项添加到Schema中,例如:

payment_id

  1. 将猫鼬结果映射到json,例如:

new mongoose.Schema({...}, {toJSON: {getters: true}})

(描述:猫鼬似乎仅在转换为json时才使用getter,并且默认情况下将其禁用。)