亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在寫入/讀取到 c# mongo db 時加密/解密屬性

在寫入/讀取到 c# mongo db 時加密/解密屬性

C#
呼喚遠方 2022-10-23 15:07:26
只是要列出我擁有的所有信息:簡而言之,我正在尋找與 ASP Core (2.2) 和 C# MongoDB Driver (2.7) 完全(字面意思)類似的東西。這似乎是一個常見的要求,我很驚訝我找不到任何已經建成的東西。這是我到目前為止所擁有的:模型:public class Patient{    //comes from the client as XXXXXXXXX, RegEx: "([0-9]{9})"    //[MongoEncrypt]    public EncryptedString SocialSecurityNumber { get; set; }  }屬性:[AttributeUsage(AttributeTargets.Property)]public class MongoEncryptAttribute : BsonSerializerAttribute{    public MongoEncryptAttribute()    {        SerializerType = typeof(MongoEncryptSerializer);    }}自定義序列化器:public interface IMongoEncryptSerializer : IBsonSerializer<EncryptedString>{ }public class MongoEncryptSerializer : SerializerBase<EncryptedString>, IMongoEncryptSerializer{    private readonly string _encryptionKey;    public MongoEncryptSerializer(IConfiguration configuration)    {        _encryptionKey = configuration.GetSection("MongoDb")["EncryptionKey"];    }    public override EncryptedString Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)    {        var encryptedString = context.Reader.ReadString();        return AesThenHmac.SimpleDecryptWithPassword(encryptedString, _encryptionKey);    }    public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, EncryptedString value)    {        var encryptedString = AesThenHmac.SimpleEncryptWithPassword(value, _encryptionKey);        context.Writer.WriteString(encryptedString);    }}
查看完整描述

1 回答

?
犯罪嫌疑人X

TA貢獻2080條經驗 獲得超4個贊

我的解決方案:


模型:


public class Patient

{

    //comes from the client as XXXXXXXXX, RegEx: "([0-9]{9})"

    public EncryptedString SocialSecurityNumber { get; set; }  

}

自定義類型:


public class EncryptedString

{

    private readonly string _value;


    public EncryptedString(string value)

    {

        _value = value;

    }


    public static implicit operator string(EncryptedString s)

    {

        return s._value;

    }


    public static implicit operator EncryptedString(string value)

    {

        if (value == null)

            return null;


        return new EncryptedString(value);

    }

}

序列化器(使用確定性加密):


public interface IEncryptedStringSerializer : IBsonSerializer<EncryptedString> {} 


public class EncryptedStringSerializer : SerializerBase<EncryptedString>, IEncryptedStringSerializer

{

    private readonly IDeterministicEncrypter _encrypter;

    private readonly string _encryptionKey;


    public EncryptedStringSerializer(IConfiguration configuration, IDeterministicEncrypter encrypter)

    {

        _encrypter = encrypter;

        _encryptionKey = configuration.GetSection("MongoDb")["EncryptionKey"];

    }


    public override EncryptedString Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)

    {

        var encryptedString = context.Reader.ReadString();

        return _encrypter.DecryptStringWithPassword(encryptedString, _encryptionKey);

    }


    public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, EncryptedString value)

    {

        var encryptedString = _encrypter.EncryptStringWithPassword(value, _encryptionKey);

        context.Writer.WriteString(encryptedString);

    }

}

注冊序列化器:


collection.AddScoped<IEncryptedStringSerializer, EncryptedStringSerializer>();

//then later...

BsonSerializer.RegisterSerializer<EncryptedString>(sp.GetService<IEncryptedStringSerializer>());


查看完整回答
反對 回復 2022-10-23
  • 1 回答
  • 0 關注
  • 160 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號