gpt4 book ai didi

c# - 在写入/读取 c# mongodb 时加密/解密属性

转载 作者:行者123 更新时间:2023-12-04 02:50:54 25 4
gpt4 key购买 nike

只是列出我拥有的所有信息:

简而言之,我正在寻找(字面上)与 this 完全相同的东西但与 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. 使用 DI(vanilla .net 核心 DI)获取 Serializer。在引导方法中考虑类似 BsonSerializer.RegisterSerializer(type,serializer) 的东西,我可以在其中访问服务集合并执行 GetInstance 但随后我需要 string SocialSecurityNumber 使用自定义类型(可能是 SecureString?)

  2. 在序列化程序中使用 DI 获取 key (最初来自 IConfiguration/appsettings.json ,然后最终来自 Azure KeyVault (对我来说是全新的蠕虫))和 EncryptionProvider

  3. 用于搜索的确定性加密。 AesThenHmac 来自这个流行的 post .我可以在其当前实现中很好地存储和检索数据。但是为了搜索 SSN,我需要这个库不提供的确定性加密。

最佳答案

我的解决方案:

型号:

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);
}
}

序列化器(使用 Deterministic Encryption ):

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>());

关于c# - 在写入/读取 c# mongodb 时加密/解密属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55135707/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com