diff --git a/Proculite.Common.Test/Security/Cryptography/SymmetricCryptoStreamTest.cs b/Proculite.Common.Test/Security/Cryptography/SymmetricCryptoStreamTest.cs new file mode 100644 index 0000000..64e8d15 --- /dev/null +++ b/Proculite.Common.Test/Security/Cryptography/SymmetricCryptoStreamTest.cs @@ -0,0 +1,26 @@ +namespace Proculite.Common.Test.Security.Cryptography +{ + using System.Security.Cryptography; + using Proculite.Common.Security.Cryptography; + + public class SymmetricCryptoStreamTest + { + [Fact] + public void InputEncryptedDecrypted_EqualOutput() + { + Aes aes = SimpleAes.Create( + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] + ); + + byte[] original = [1]; + SymmetricCryptoStream symmetricCryptoStream = new SymmetricCryptoStream(aes); + + byte[] result = symmetricCryptoStream.DecryptBytes( + symmetricCryptoStream.EncryptBytes(original) + ); + Assert.Equal(result[0], original[0]); + Assert.Single(result); + } + } +} diff --git a/Proculite.Common/Security/Cryptography/SimpleAes.cs b/Proculite.Common/Security/Cryptography/SimpleAes.cs new file mode 100644 index 0000000..8ce7b1f --- /dev/null +++ b/Proculite.Common/Security/Cryptography/SimpleAes.cs @@ -0,0 +1,16 @@ +using System.Security.Cryptography; + +namespace Proculite.Common.Security.Cryptography +{ + public static class SimpleAes + { + public static Aes Create(byte[] key, byte[] iv) + { + Aes aes = Aes.Create(); + aes.IV = iv; + aes.Key = key; + + return aes; + } + } +} diff --git a/Proculite.Common/Security/Cryptography/SymmetricCryptoStream.cs b/Proculite.Common/Security/Cryptography/SymmetricCryptoStream.cs new file mode 100644 index 0000000..cce926b --- /dev/null +++ b/Proculite.Common/Security/Cryptography/SymmetricCryptoStream.cs @@ -0,0 +1,63 @@ +using System.IO; +using System.Security.Cryptography; + +namespace Proculite.Common.Security.Cryptography +{ + public class SymmetricCryptoStream + { + private readonly SymmetricAlgorithm _symmetricAlgorithm; + + public SymmetricCryptoStream(SymmetricAlgorithm symmetricAlgorithm) + { + _symmetricAlgorithm = symmetricAlgorithm; + } + + public void EncryptStream(Stream readFromStream, Stream writeToStream) + { + ICryptoTransform encryptor = _symmetricAlgorithm.CreateEncryptor(); + using ( + CryptoStream cryptoStream = new CryptoStream( + readFromStream, + encryptor, + CryptoStreamMode.Read + ) + ) + { + cryptoStream.CopyTo(writeToStream); + } + } + + public void DecryptStream(Stream readFromStream, Stream writeToStream) + { + ICryptoTransform decryptor = _symmetricAlgorithm.CreateDecryptor(); + using ( + CryptoStream cryptoStream = new CryptoStream( + readFromStream, + decryptor, + CryptoStreamMode.Read + ) + ) + { + cryptoStream.CopyTo(writeToStream); + } + } + + public byte[] EncryptBytes(byte[] source) + { + MemoryStream sourceBytes = new MemoryStream(source); + MemoryStream encryptedBytes = new MemoryStream(); + EncryptStream(sourceBytes, encryptedBytes); + + return encryptedBytes.ToArray(); + } + + public byte[] DecryptBytes(byte[] source) + { + MemoryStream sourceBytes = new MemoryStream(source); + MemoryStream decryptedBytes = new MemoryStream(); + DecryptStream(sourceBytes, decryptedBytes); + + return decryptedBytes.ToArray(); + } + } +}