using System; using System.Security.Cryptography; namespace Hazel.Crypto { /// /// AES provider using the default System.Security.Cryptography implementation /// public class DefaultAes : IAes { private readonly ICryptoTransform encryptor_; /// /// Create a new default instance of the AES block cipher /// /// Encryption key public DefaultAes(ByteSpan key) { // Create the AES block cipher using (Aes aes = Aes.Create()) { aes.KeySize = key.Length * 8; aes.BlockSize = aes.KeySize; aes.Mode = CipherMode.ECB; aes.Padding = PaddingMode.Zeros; aes.Key = key.ToArray(); this.encryptor_ = aes.CreateEncryptor(); } } /// public void Dispose() { this.encryptor_.Dispose(); } /// public int EncryptBlock(ByteSpan inputSpan, ByteSpan outputSpan) { if (inputSpan.Length != outputSpan.Length) { throw new ArgumentException($"ouputSpan length ({outputSpan.Length}) does not match inputSpan length ({inputSpan.Length})", nameof(outputSpan)); } return this.encryptor_.TransformBlock(inputSpan.GetUnderlyingArray(), inputSpan.Offset, inputSpan.Length, outputSpan.GetUnderlyingArray(), outputSpan.Offset); } } }