init
This commit is contained in:
commit
ded4f4db26
43 changed files with 2052 additions and 0 deletions
36
Catalogger.Backend/Database/EncryptionService.cs
Normal file
36
Catalogger.Backend/Database/EncryptionService.cs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
|
||||
namespace Catalogger.Backend.Database;
|
||||
|
||||
public class EncryptionService(Config config) : IEncryptionService
|
||||
{
|
||||
private readonly byte[] _secretKey = Convert.FromBase64String(config.Database.EncryptionKey);
|
||||
|
||||
public byte[] Encrypt(string data)
|
||||
{
|
||||
using var aes = Aes.Create();
|
||||
aes.Key = _secretKey;
|
||||
|
||||
var output = new List<byte>();
|
||||
output.AddRange(aes.IV);
|
||||
|
||||
var plaintext = Encoding.UTF8.GetBytes(data);
|
||||
var ciphertext = aes.EncryptCbc(plaintext, aes.IV);
|
||||
|
||||
output.AddRange(ciphertext);
|
||||
return output.ToArray();
|
||||
}
|
||||
|
||||
public string Decrypt(byte[] input)
|
||||
{
|
||||
using var aes = Aes.Create();
|
||||
aes.Key = _secretKey;
|
||||
|
||||
var iv = input.Take(aes.IV.Length).ToArray();
|
||||
var ciphertext = input.Skip(aes.IV.Length).ToArray();
|
||||
var plaintext = aes.DecryptCbc(ciphertext, iv);
|
||||
|
||||
return Encoding.UTF8.GetString(plaintext);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue