public void WriteTest2() { var key = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; var content = new byte[] { 0x66, 0xe9, 0x4b, 0xd4, 0xef, 0x8a, 0x2c, 0x3b, 0x88, 0x4c, 0xfa, 0x59, 0xca, 0x34, 0x2b, 0x2e }; var expected = new byte[] { 0x04, 0x10, 0xbe, 0xfc, 0xcd, 0xe6, 0x94, 0x4b, 0x69, 0xdd, 0x00, 0x7d, 0xeb, 0xe3, 0x9a, 0x9d, 0x3a, 0xb6, 0x07, 0x36, 0xf9, 0xcd, 0x20, 0xa2, 0xcf, 0xe1, 0xf2, 0x48, 0x34, 0xb6, 0x25, 0x3d }; var ms = new MemoryStream(); ms.Write(content, 0, content.Length); ms.Position = 0; var ecb = new EcbStream(ms, key); ecb.Position = 15; ecb.Write(new byte[] { 0x11, 0x11 }, 0, 2); var actualLength = ecb.Length; ecb.Flush(); var flushedLength = ecb.Length; Assert.IsTrue(ms.ToArray().SequenceEqual(expected)); Assert.AreEqual(32, ecb.Length); Assert.AreEqual(17, actualLength); Assert.AreEqual(32, flushedLength); Assert.AreEqual(0, ms.Position); }
public void WriteTest3() { var key = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; var content = new byte[] { 0x66, 0xe9, 0x4b, 0xd4, 0xef, 0x8a, 0x2c, 0x3b, 0x88, 0x4c, 0xfa, 0x59, 0xca, 0x34, 0x2b, 0x2e }; var expected = new byte[] { 0x66, 0xe9, 0x4b, 0xd4, 0xef, 0x8a, 0x2c, 0x3b, 0x88, 0x4c, 0xfa, 0x59, 0xca, 0x34, 0x2b, 0x2e, 0x4c, 0x02, 0x1d, 0x6d, 0xf5, 0x51, 0xd7, 0xf8, 0x74, 0xa4, 0xe0, 0x30, 0x8f, 0x4c, 0xb6, 0x13 }; var ms = new MemoryStream(); ms.Write(content, 0, content.Length); ms.Position = 0; var ecb = new EcbStream(ms, key); ecb.Position = 17; ecb.Write(new byte[] { 0x11, 0x11 }, 0, 2); var actualLength = ecb.Length; ecb.Flush(); var flushedLength = ecb.Length; Assert.IsTrue(ms.ToArray().SequenceEqual(expected)); Assert.AreEqual(32, ecb.Length); Assert.AreEqual(19, actualLength); Assert.AreEqual(32, flushedLength); Assert.AreEqual(0, ms.Position); }
private Task <bool> DoCipher(Stream input, Stream output, IProgress <ProgressReport> progress, bool decrypt) { var requestId = Guid.NewGuid().ToString("N"); var key = OnRequestKey("AES256 ECB Key", 32, requestId, out var error); if (key == null) { return(Task.Factory.StartNew(() => { progress.Report(new ProgressReport { Percentage = 0, Message = error }); return false; })); } return(Task.Factory.StartNew(() => { progress.Report(new ProgressReport { Percentage = 0, Message = decrypt ? "Decryption..." : "Encryption..." }); using (var ecb = new EcbStream(decrypt ? input : output, key)) { var buffer = new byte[0x10000]; var totalLength = decrypt ? ecb.Length : input.Length; while (ecb.Position < totalLength) { var length = (int)Math.Min(0x10000, totalLength - ecb.Position); if (decrypt) { ecb.Read(buffer, 0, length); output.Write(buffer, 0, length); } else { input.Read(buffer, 0, length); ecb.Write(buffer, 0, length); } progress.Report(new ProgressReport { Percentage = (double)ecb.Position / totalLength * 100, Message = decrypt ? "Decryption..." : "Encryption...", }); } } progress.Report(new ProgressReport { Percentage = 100, Message = decrypt ? "Decryption finished." : "Encryption finished." }); return true; })); }
public void WriteTest1() { var key = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; var content = new byte[] { 0x66, 0xe9, 0x4b, 0xd4, 0xef, 0x8a, 0x2c, 0x3b, 0x88, 0x4c, 0xfa, 0x59, 0xca, 0x34, 0x2b, 0x2e }; var expected = new byte[] { 0x40, 0x15, 0xaf, 0xf5, 0x6f, 0xb5, 0xb9, 0x20, 0xc9, 0x55, 0x44, 0x90, 0x35, 0xda, 0xa7, 0x41 }; var ms = new MemoryStream(content); var ecb = new EcbStream(ms, key); ecb.Position = 3; ecb.Write(new byte[] { 0x11, 0x11 }, 0, 2); ecb.Flush(); Assert.IsTrue(ms.ToArray().SequenceEqual(expected)); Assert.AreEqual(5, ecb.Position); Assert.AreEqual(0, ms.Position); }