-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
71 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace AesSample.Fluent; | ||
|
||
public class EncryptedDatabaseContext : DatabaseContext | ||
{ | ||
public EncryptedDatabaseContext(DbContextOptions<DatabaseContext> options) | ||
: base(options, null) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,25 +23,38 @@ static void Main(string[] args) | |
byte[] encryptionIV = keyInfo.IV; | ||
var encryptionProvider = new AesProvider(encryptionKey, encryptionIV); | ||
|
||
using var context = new DatabaseContext(options, encryptionProvider); | ||
context.Database.EnsureCreated(); | ||
|
||
var user = new UserEntity | ||
using (var context = new DatabaseContext(options, encryptionProvider)) | ||
{ | ||
FirstName = "John", | ||
LastName = "Doe", | ||
Email = "[email protected]", | ||
EncryptedData = new byte[2] { 1, 2 }, | ||
EncryptedDataAsString = new byte[2] { 3, 4 } | ||
}; | ||
context.Database.EnsureCreated(); | ||
|
||
var user = new UserEntity | ||
{ | ||
FirstName = "John", | ||
LastName = "Doe", | ||
Email = "[email protected]", | ||
Notes = "Hello world!", | ||
EncryptedData = new byte[2] { 1, 2 }, | ||
EncryptedDataAsString = new byte[2] { 3, 4 } | ||
}; | ||
|
||
context.Users.Add(user); | ||
context.SaveChanges(); | ||
|
||
context.Users.Add(user); | ||
context.SaveChanges(); | ||
Console.WriteLine($"Users count: {context.Users.Count()}"); | ||
} | ||
|
||
Console.WriteLine($"Users count: {context.Users.Count()}"); | ||
using (var context = new EncryptedDatabaseContext(options)) | ||
{ | ||
UserEntity user = context.Users.First(); | ||
|
||
Console.WriteLine($"Encrypted User: {user.FirstName} {user.LastName} - {user.Email} (Notes: {user.Notes})"); | ||
} | ||
|
||
user = context.Users.First(); | ||
using (var context = new DatabaseContext(options, encryptionProvider)) | ||
{ | ||
UserEntity user = context.Users.First(); | ||
|
||
Console.WriteLine($"User: {user.FirstName} {user.LastName} - {user.Email}"); | ||
Console.WriteLine($"User: {user.FirstName} {user.LastName} - {user.Email} (Notes: {user.Notes})"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,25 +23,31 @@ static void Main() | |
byte[] encryptionIV = keyInfo.IV; | ||
var encryptionProvider = new AesProvider(encryptionKey, encryptionIV); | ||
|
||
using var context = new DatabaseContext(options, encryptionProvider); | ||
context.Database.EnsureCreated(); | ||
|
||
var user = new UserEntity | ||
using (var context = new DatabaseContext(options, encryptionProvider)) | ||
{ | ||
FirstName = "John", | ||
LastName = "Doe", | ||
Email = "[email protected]", | ||
EncryptedData = new byte[2] { 1, 2 }, | ||
EncryptedDataAsString = new byte[2] { 3, 4 } | ||
}; | ||
context.Database.EnsureCreated(); | ||
|
||
var user = new UserEntity | ||
{ | ||
FirstName = "John", | ||
LastName = "Doe", | ||
Email = "[email protected]", | ||
Notes = "Hello world!", | ||
EncryptedData = new byte[2] { 1, 2 }, | ||
EncryptedDataAsString = new byte[2] { 3, 4 } | ||
}; | ||
|
||
context.Users.Add(user); | ||
context.SaveChanges(); | ||
context.Users.Add(user); | ||
context.SaveChanges(); | ||
|
||
Console.WriteLine($"Users count: {context.Users.Count()}"); | ||
Console.WriteLine($"Users count: {context.Users.Count()}"); | ||
} | ||
|
||
user = context.Users.First(); | ||
using (var context = new DatabaseContext(options, encryptionProvider)) | ||
{ | ||
UserEntity user = context.Users.First(); | ||
|
||
Console.WriteLine($"User: {user.FirstName} {user.LastName} - {user.Email}"); | ||
Console.WriteLine($"User: {user.FirstName} {user.LastName} - {user.Email} (Notes: {user.Notes})"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters