示例#1
0
        public void WriteTest3()
        {
            var key      = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            var iv       = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            var expected = new byte[] { 0x66, 0xe9, 0x4b, 0xd4, 0xef, 0x8a, 0x2c, 0x3b, 0x88, 0x4c, 0xfa, 0x59, 0xca, 0x34, 0x2b, 0x2e };
            var ms       = new MemoryStream();
            var cbc      = new CbcStream(ms, key, iv);

            cbc.Write(new byte[16], 0, 16);
            cbc.Position = 12;

            Assert.ThrowsException <InvalidOperationException>(() => cbc.Write(new byte[4], 0, 4));
            Assert.AreEqual(12, cbc.Position);
        }
示例#2
0
        public void WriteTest2()
        {
            var key      = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            var iv       = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            var expected = new byte[] { 0x66, 0xe9, 0x4b, 0xd4, 0xef, 0x8a, 0x2c, 0x3b, 0x88, 0x4c, 0xfa, 0x59, 0xca, 0x34, 0x2b, 0x2e,
                                        0xf7, 0x95, 0xbd, 0x4a, 0x52, 0xe2, 0x9e, 0xd7, 0x13, 0xd3, 0x13, 0xfa, 0x20, 0xe9, 0x8d, 0xbc };
            var ms  = new MemoryStream();
            var cbc = new CbcStream(ms, key, iv);

            cbc.Write(new byte[12], 0, 12);
            cbc.Write(new byte[4], 0, 4);
            cbc.Write(new byte[16], 0, 16);
            cbc.Flush();

            Assert.IsTrue(ms.ToArray().SequenceEqual(expected));
            Assert.AreEqual(32, cbc.Position);
            Assert.AreEqual(0, ms.Position);
        }
示例#3
0
        public void WriteTest1()
        {
            var key      = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            var iv       = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            var expected = new byte[] { 0x66, 0xe9, 0x4b, 0xd4, 0xef, 0x8a, 0x2c, 0x3b, 0x88, 0x4c, 0xfa, 0x59, 0xca, 0x34, 0x2b, 0x2e };
            var ms       = new MemoryStream();
            var cbc      = new CbcStream(ms, key, iv);

            cbc.Write(new byte[16], 0, 16);
            cbc.Flush();

            Assert.IsTrue(ms.ToArray().SequenceEqual(expected));
            Assert.AreEqual(16, cbc.Position);
            Assert.AreEqual(0, ms.Position);
        }
示例#4
0
        private Task <bool> DoCipher(Stream input, Stream output, IProgress <ProgressReport> progress, bool decrypt)
        {
            var key = OnRequestKey("AES128 CBC Key", 32, out var error);

            if (key == null)
            {
                return(Task.Factory.StartNew(() =>
                {
                    progress.Report(new ProgressReport {
                        Percentage = 0, Message = error
                    });
                    return false;
                }));
            }

            var iv = OnRequestKey("AES128 CBC IV", 32, out error);

            if (iv == 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 cbc = new CbcStream(decrypt ? input : output, key, iv))
                {
                    var buffer = new byte[0x10000];
                    var totalLength = decrypt ? cbc.Length : input.Length;
                    while (cbc.Position < totalLength)
                    {
                        var length = (int)Math.Min(0x10000, totalLength - cbc.Position);

                        if (decrypt)
                        {
                            cbc.Read(buffer, 0, length);
                            output.Write(buffer, 0, length);
                        }
                        else
                        {
                            input.Read(buffer, 0, length);
                            cbc.Write(buffer, 0, length);
                        }

                        progress.Report(new ProgressReport {
                            Percentage = (double)cbc.Position / totalLength * 100, Message = decrypt ? "Decryption..." : "Encryption...",
                        });
                    }
                }

                progress.Report(new ProgressReport {
                    Percentage = 100, Message = decrypt ? "Decryption finished." : "Encryption finished."
                });

                return true;
            }));
        }