This commit is contained in:
sam 2024-08-13 13:08:50 +02:00
commit ded4f4db26
Signed by: sam
GPG key ID: 5F3C3C1B3166639D
43 changed files with 2052 additions and 0 deletions

View 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);
}
}