diff --git a/src/common/Edelstein.Common.Crypto/AESCipher.cs b/src/common/Edelstein.Common.Crypto/AESCipher.cs index 6df0b653f..87fca91ad 100644 --- a/src/common/Edelstein.Common.Crypto/AESCipher.cs +++ b/src/common/Edelstein.Common.Crypto/AESCipher.cs @@ -4,7 +4,7 @@ namespace Edelstein.Common.Crypto; public class AESCipher { - private readonly SymmetricAlgorithm _cipher; + private readonly ICryptoTransform _transformer; public AESCipher() : this(new byte[] { 0x13, 0x08, 0x06, 0xb4, 0x1b, 0x0f, 0x33, 0x52 }) { @@ -13,14 +13,15 @@ public AESCipher() : this(new byte[] { 0x13, 0x08, 0x06, 0xb4, 0x1b, 0x0f, 0x33, public AESCipher(ReadOnlySpan userKey) { var expandedKey = new byte[userKey.Length * 4]; + var cipher = Aes.Create(); for (var i = 0; i < userKey.Length; i++) expandedKey[i * 4] = userKey[i]; - - _cipher = Aes.Create(); - _cipher.KeySize = 256; - _cipher.Key = expandedKey; - _cipher.Mode = CipherMode.ECB; + + cipher.KeySize = 256; + cipher.Key = expandedKey; + cipher.Mode = CipherMode.ECB; + _transformer = cipher.CreateEncryptor(); } public void Transform(Span input, uint pSrc) @@ -32,8 +33,6 @@ public void Transform(Span input, uint pSrc) var srcExp = new byte[sizeof(int) * 4]; var srcBytes = BitConverter.GetBytes(pSrc); - using var crypt = _cipher.CreateEncryptor(); - while (remaining > 0) { for (var i = 0; i < srcExp.Length; ++i) @@ -48,7 +47,7 @@ public void Transform(Span input, uint pSrc) if (sub % srcExp.Length == 0) { - var result = crypt.TransformFinalBlock(srcExp, 0, srcExp.Length); + var result = _transformer.TransformFinalBlock(srcExp, 0, srcExp.Length); Array.Copy(result, srcExp, srcExp.Length); }