I'm trying to make my user data more secure by encrypting sensitive data like Firstname and Lastname of a client. But when I use the following code from medium and update my database, the data doesn't get encrypted after adding an new client.
Source:
https://medium.com/emrekizildas/encrypt-your-database-columns-with-entityframework-1f129b19bdf8
Other suggestions for encrypting data are also welcome!
These are the things I tried:
- Updating from .NET Core 3.1 to .NET 5.0 and changing package version
- Creating 128bit key instead of "test123456"
- AesEncryption: https://github.com/Eastrall/EntityFrameworkCore.DataEncryption
Model/DbContext:
using EntityFrameworkCore.EncryptColumn;
using EntityFrameworkCore.EncryptColumn.Extension;
using EntityFrameworkCore.EncryptColumn.Interfaces;
using EntityFrameworkCore.EncryptColumn.Util;
using Globals.Entities;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using System;
private readonly IEncryptionProvider _provider;
public Model()
{
Initialize.EncryptionKey = "test123456";
this._provider = new GenerateEncryptionProvider();
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.UseEncryption(this._provider);
}
Example entity:
using EntityFrameworkCore.EncryptColumn.Attribute;
namespace Globals.Entities
{
public class Client
{
[Key]
[Required]
public int Id { get; set; }
[EncryptColumn]
public string FirstName { get; set; }
[EncryptColumn]
public string LastName { get; set; }
public Client(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}
}
}
Commands for reinstalling the database:
drop-database
remove-migration
add-migration initialCreate
update-database
IEncryptionProviderin startup.cs or program.cs?