I would like to encrypt some confidential information and save it to a database, and later decrypt it.
I've got all of this working fine, and I am protecting and persisting the key on Aws S3 and KMS.
Will this allow me to decrypt data indefinitely or do I have to consider anything?
Code Snippet for 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);
}
}