1

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:

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
6
  • 1
    I recommend hashing the sensitive date instead of encrypting. because, hashing is a one-way function. Commented Dec 10, 2022 at 7:09
  • 1
    Did you inject IEncryptionProvider in startup.cs or program.cs? Commented Dec 10, 2022 at 7:14
  • @AlirezaK Thank you for the advice. I also did not inject the provider in startup.cs (or program.cs). But do you have an example of this because the sources I used, don't include it. Commented Dec 11, 2022 at 10:52
  • 1
    I think this link can help you stackoverflow.com/a/74744534/4444757 Commented Dec 11, 2022 at 12:40
  • 1
    You're welcome. You can refer to the follow repo to understand how hashing works in Blazor. github.com/Kamalifar/BlazorServerCookieAuthentication-master/… Commented Dec 13, 2022 at 5:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.