bunch of fixes

This commit is contained in:
TheXamlGuy
2024-05-02 20:58:12 +01:00
parent 7dfbb91762
commit 8d62f36931
17 changed files with 180 additions and 134 deletions
+34
View File
@@ -0,0 +1,34 @@
using System.Security.Cryptography;
namespace Toolkit.Foundation;
public class AesEncryptor :
IEncryptor
{
private const int IvSize = 16;
public byte[] Encrypt(byte[] data,
byte[] key)
{
if (key.Length != 32)
{
throw new ArgumentException("Key must be 256 bits (32 bytes).");
}
using Aes aes = Aes.Create();
aes.Key = key;
aes.GenerateIV();
using MemoryStream memoryStream = new();
memoryStream.Write(aes.IV.AsSpan(0, IvSize));
using (ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV))
using (CryptoStream cryptoStream = new(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(data, 0, data.Length);
cryptoStream.FlushFinalBlock();
}
return memoryStream.ToArray();
}
}