指定された文字列が暗号化されているかプレーンな文字列であるかをどのように確認できますか?
正直なところ、それがすべての質問です。たとえば、DPAPI 暗号化を使用してデータ保護を使用している場合、指定された文字列が既に暗号化された文字列であるか、復号化呼び出しの前である可能性がある場合は、指定された文字列が暗号化されているかどうかを確認します。
"ConnectionStrings": {
"DefaultConnection": "Server=SQL2014;Database=TestDb;Trusted_Connection=false;User Id=test;Password=test@123;MultipleActiveResultSets=true"
}
データ保護の構成
public void ConfigureServices(IServiceCollection services)
{
var dataProtectionBuilder = services.AddDataProtection().SetApplicationName("TestDataProtection");
dataProtectionBuilder.PersistKeysToFileSystem(new System.IO.DirectoryInfo(@"F:\Test Data\TestDPAPI"));
//Configuration goes here
dataProtectionBuilder.AddKeyManagementOptions(options =>
{
options.AutoGenerateKeys = true;
options.NewKeyLifetime = TimeSpan.FromMinutes(1);
});
dataProtectionBuilder.ProtectKeysWithDpapi(true);//Scope to LocalMachine (default Scope.CurrentUser)
dataProtectionBuilder.SetDefaultKeyLifetime(TimeSpan.FromMinutes(1));
dataProtectionBuilder.UseCryptographicAlgorithms(new Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.AuthenticatedEncryptionSettings
{
EncryptionAlgorithm = Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.EncryptionAlgorithm.AES_256_GCM,
ValidationAlgorithm = Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ValidationAlgorithm.HMACSHA512
});
}
サービスは以下のようになります
public class TestClass
{
IDataProtector dataProtector;
public TestClass(IDataProtectionProvider dataProtectorProvider)
{
this.dataProtector = dataProtectorProvider.CreateProtector("purpose");
}
private string Protect(string value)
{
return dataProtector.Protect(value);
}
private string UnProtect(string value)
{
return IsProtected(value)? dataProtector.Unprotect(value):value;
}
private bool IsProtected(string value)
{
//TODO How can we find
return false;
}
}