【发布时间】:2019-01-27 01:26:55
【问题描述】:
我想加密一些机密信息并将其保存到数据库中,然后再解密。
我已经让所有这些工作正常,并且我正在保护和持久化 Aws S3 和 KMS 上的密钥。
这是否允许我无限期地解密数据还是我必须考虑什么?
ConfigureServices 的代码片段 - startup.cs
services.AddSingleton<IAmazonS3>(new AmazonS3Client(RegionEndpoint.EUWest2));
services.AddSingleton<IAmazonKeyManagementService>(new AmazonKeyManagementServiceClient(RegionEndpoint.EUWest2));
services.AddDataProtection()
.ProtectKeysWithAwsKms(Configuration.GetSection("KmsProtection"))
.PersistKeysToAwsS3(Configuration.GetSection("S3Persistence"));
var cipherOptions = Configuration.GetSection("CipherOptions");
services.Configure<CipherOptions>(cipherOptions);
services.AddScoped(typeof(ICipherService), typeof(CipherService));
CipherService.cs
public class CipherService : ICipherService
{
private readonly IDataProtectionProvider dataProtectionProvider;
private readonly string purpose;
public CipherService(IDataProtectionProvider dataProtectionProvider, IOptions<CipherOptions> options)
{
this.dataProtectionProvider = dataProtectionProvider;
this.purpose = options.Value.Purpose;
}
public string Encrypt(string input)
{
var protector = dataProtectionProvider.CreateProtector(purpose);
return protector.Protect(input);
}
public string Decrypt(string cipherText)
{
var protector = dataProtectionProvider.CreateProtector(purpose);
return protector.Unprotect(cipherText);
}
}
【问题讨论】:
标签: security encryption asp.net-core