Compare commits
2 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1eb93d2f9a | ||
|
|
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
27
Proculite.Common/Security/Cryptography/SimpleAes.cs
Normal file
27
Proculite.Common/Security/Cryptography/SimpleAes.cs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
using System.Security.Cryptography;
|
||||
|
||||
namespace Proculite.Common.Security.Cryptography
|
||||
{
|
||||
public static class SimpleAes
|
||||
{
|
||||
public static Aes Create(byte[] key, byte[] iv, CipherMode cipherMode = CipherMode.CBC)
|
||||
{
|
||||
Aes aes = Aes.Create();
|
||||
aes.IV = iv;
|
||||
aes.Key = key;
|
||||
aes.Mode = cipherMode;
|
||||
|
||||
return aes;
|
||||
}
|
||||
|
||||
public static SymmetricCryptoStream CreateCryptoStream(
|
||||
byte[] key,
|
||||
byte[] iv,
|
||||
CipherMode cipherMode = CipherMode.CBC
|
||||
)
|
||||
{
|
||||
Aes aes = Create(key, iv, cipherMode);
|
||||
return new SymmetricCryptoStream(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