Created SymmetricCryptoStream and SimpleAes classes.
These classes allow for convenient encryption with AES.
This commit is contained in:
parent
41d6153aaa
commit
458acfcd85
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
16
Proculite.Common/Security/Cryptography/SimpleAes.cs
Normal file
16
Proculite.Common/Security/Cryptography/SimpleAes.cs
Normal file
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue