diff --git a/Utilities/Cryptography/AesCounterMode.cs b/Utilities/Cryptography/AesCounterMode.cs index 82b347e..c0ccf01 100644 --- a/Utilities/Cryptography/AesCounterMode.cs +++ b/Utilities/Cryptography/AesCounterMode.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Security.Cryptography; namespace Teknik.Utilities.Cryptography @@ -105,7 +106,7 @@ namespace Teknik.Utilities.Cryptography // Initialize Counter _Counter = new PooledArray(initialCounter.Length); - initialCounter.Array.CopyTo(_Counter.Array, 0); + initialCounter.CopyTo(_Counter.Array); // Initialize the encrypted counter _EncryptedCounter = new PooledArray(_BlockSize / 8); @@ -208,7 +209,7 @@ namespace Teknik.Utilities.Cryptography public void ResetCounter() { - _IV.Array.CopyTo(_Counter.Array, 0); + _IV.CopyTo(_Counter.Array); _Iterations = 0; } diff --git a/Utilities/Cryptography/AesCounterStream.cs b/Utilities/Cryptography/AesCounterStream.cs index 7719988..7c80dc0 100644 --- a/Utilities/Cryptography/AesCounterStream.cs +++ b/Utilities/Cryptography/AesCounterStream.cs @@ -1,5 +1,6 @@ using System; using System.IO; +using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -22,16 +23,18 @@ namespace Teknik.Utilities.Cryptography public AesCounterStream(Stream stream, bool encrypt, PooledArray key, PooledArray iv) { _Inner = stream; + var keyBytes = key.ToArray(); + var ivBytes = iv.ToArray(); // Create the Aes Cipher using AesCounterMode aes = new AesCounterMode(iv); if (encrypt) { - _Cipher = (CounterModeCryptoTransform)aes.CreateEncryptor(key.Array, iv.Array); // Encrypt + _Cipher = (CounterModeCryptoTransform)aes.CreateEncryptor(keyBytes, ivBytes); // Encrypt } else { - _Cipher = (CounterModeCryptoTransform)aes.CreateDecryptor(key.Array, iv.Array); // Decrypt + _Cipher = (CounterModeCryptoTransform)aes.CreateDecryptor(keyBytes, ivBytes); // Decrypt } // Sync the counter diff --git a/Utilities/PooledArray.cs b/Utilities/PooledArray.cs index 34779c8..1871a18 100644 --- a/Utilities/PooledArray.cs +++ b/Utilities/PooledArray.cs @@ -28,6 +28,16 @@ namespace Teknik.Utilities Length = array.Length; } + public void CopyTo(byte[] destination) + { + System.Array.Copy(Array, destination, Length); + } + + public byte[] ToArray() + { + return Array.Take(Length).ToArray(); + } + public void Dispose() { _arrayPool.Return(Array);