Beispiel #1
0
        public void ShouldBecorrectWriteNotCrypt()
        {
            //init
            int          originalSize       = 100004;
            int          expectedPackedSize = 100016;
            MemoryStream original           = new MemoryStream(originalSize);
            Random       rnd = new Random();

            for (var i = 0; i < originalSize; i++)
            {
                original.WriteByte((byte)rnd.Next(0, 255));
            }
            byte[] key  = new byte[40];
            Random rnd2 = new Random();

            rnd2.NextBytes(key);

            //encrypt
            MemoryStream          inconteiner           = new MemoryStream();
            CryptContainerStorage cryptContainerStorage = new CryptContainerStorage(original, inconteiner, new MockCrypter());

            cryptContainerStorage.Pack();

            original.Position    = 0;
            inconteiner.Position = 8;
            for (var i = 0; i < originalSize; i++)
            {
                int originalByte    = original.ReadByte();
                int destinationByte = inconteiner.ReadByte();
                Assert.AreEqual(originalByte, destinationByte);
                Assert.AreNotEqual(originalByte, -1);
                Assert.AreNotEqual(destinationByte, -1);
            }
        }
Beispiel #2
0
        public void ShouldBeCorrectPackAndExtract()
        {
            //init
            int          originalSize       = 16; //100004;
            int          expectedPackedSize = 16; //100016;
            MemoryStream original           = new MemoryStream(originalSize);
            Random       rnd = new Random();

            for (var i = 0; i < originalSize; i++)
            {
                original.WriteByte((byte)rnd.Next(0, 255));
            }
            byte[] key  = new byte[40];
            byte[] key2 = new byte[40];
            Random rnd2 = new Random();

            rnd2.NextBytes(key);
            Array.Copy(key, key2, key.Length);

            //encrypt
            var                   crypter1              = new Blowfish(key);
            MemoryStream          inconteiner           = new MemoryStream();
            CryptContainerStorage cryptContainerStorage = new CryptContainerStorage(original, inconteiner, crypter1);

            cryptContainerStorage.Pack();

            //decrypt
            var          crypter2    = new Blowfish(key2);
            MemoryStream destination = new MemoryStream();

            inconteiner.Position = 0;
            CryptContainerStorage cryptContainerStorage2 = new CryptContainerStorage(destination, inconteiner, crypter2);

            cryptContainerStorage2.Extract();

            //asserts
            Assert.AreEqual(original.Length, destination.Length);
            original.Position    = 0;
            destination.Position = 0;
            for (var i = 0; i < originalSize; i++)
            {
                int originalByte    = original.ReadByte();
                int destinationByte = destination.ReadByte();
                Assert.AreEqual(originalByte, destinationByte);
                Assert.AreNotEqual(originalByte, -1);
                Assert.AreNotEqual(destinationByte, -1);
            }
        }
Beispiel #3
0
        public void ShouldBeCrypt()
        {
            //init
            int          originalSize       = 100004;
            int          expectedPackedSize = 100016;
            MemoryStream original           = new MemoryStream(originalSize);
            Random       rnd = new Random();

            for (var i = 0; i < originalSize; i++)
            {
                original.WriteByte((byte)rnd.Next(0, 255));
            }
            byte[] key  = new byte[40];
            Random rnd2 = new Random();

            rnd2.NextBytes(key);

            //encrypt
            MemoryStream          inconteiner           = new MemoryStream();
            CryptContainerStorage cryptContainerStorage = new CryptContainerStorage(original, inconteiner, new Blowfish(key));

            cryptContainerStorage.Pack();

            original.Position    = 0;
            inconteiner.Position = 8;
            int  _originalByte    = original.ReadByte();
            int  _destinationByte = inconteiner.ReadByte();
            long counter          = 0;

            for (var i = 0; i < originalSize - 1; i++)
            {
                int originalByte    = original.ReadByte();
                int destinationByte = inconteiner.ReadByte();
                if (originalByte == destinationByte)
                {
                    counter++;
                }
            }

            Assert.IsTrue(((float)counter / (float)originalSize) < 0.05);
        }
Beispiel #4
0
        public void ShouldBeCorrectPackAndExtractWithoutCrypter()
        {
            //init
            int          originalSize       = 100004;
            int          expectedPackedSize = 100016;
            MemoryStream original           = new MemoryStream(originalSize);
            Random       rnd = new Random();

            for (var i = 0; i < originalSize; i++)
            {
                original.WriteByte((byte)rnd.Next(0, 255));
            }

            //encrypt
            MemoryStream          inconteiner           = new MemoryStream();
            CryptContainerStorage cryptContainerStorage = new CryptContainerStorage(original, inconteiner, new MockCrypter());

            cryptContainerStorage.Pack();

            //decrypt
            MemoryStream          destination            = new MemoryStream();
            CryptContainerStorage cryptContainerStorage2 = new CryptContainerStorage(destination, inconteiner, new MockCrypter());

            cryptContainerStorage2.Extract();

            //asserts
            Assert.AreEqual(original.Length, destination.Length);
            original.Position    = 0;
            destination.Position = 0;
            for (var i = 0; i < originalSize; i++)
            {
                int originalByte    = original.ReadByte();
                int destinationByte = destination.ReadByte();
                Assert.AreEqual(originalByte, destinationByte);
                Assert.AreNotEqual(originalByte, -1);
                Assert.AreNotEqual(destinationByte, -1);
            }
        }
Beispiel #5
0
        public void ExtractShouldBeCorrectSize()
        {
            long         originalSize = 100004;
            long         packedSize   = 100016;
            MemoryStream original     = new MemoryStream();
            MemoryStream inconteiner  = new MemoryStream((int)packedSize);
            byte         randomData   = 200;

            byte[] sizeValue = LongToArray(originalSize);
            inconteiner.Write(sizeValue, 0, 8);
            for (var i = 0; i < originalSize; i++)
            {
                inconteiner.WriteByte(randomData);
            }

            original = new MemoryStream(0);
            CryptContainerStorage cryptContainerStorageForUnpack = new CryptContainerStorage(original, inconteiner, new MockCrypter());

            cryptContainerStorageForUnpack.Extract();

            //Извлеченный должен быть как исходный
            Assert.AreEqual(originalSize, original.Length);
        }
Beispiel #6
0
        public void PackShouldBeCorrect_WritedSizeVariable()
        {
            int          originalSize       = 100004;
            int          expectedPackedSize = 100016;
            MemoryStream original           = new MemoryStream(originalSize);
            byte         randomData         = 200;

            for (var i = 0; i < originalSize; i++)
            {
                original.WriteByte(randomData);
            }
            MemoryStream          inconteiner           = new MemoryStream();
            CryptContainerStorage cryptContainerStorage = new CryptContainerStorage(original, inconteiner, new MockCrypter());

            cryptContainerStorage.Pack();

            inconteiner.Position = 0;
            byte[] buffer = new byte[8];
            inconteiner.Read(buffer, 0, 8);
            long writedSizeVariable = ArrayToLong(buffer);

            Assert.AreEqual(originalSize, writedSizeVariable);
        }
Beispiel #7
0
        public void PackShouldBeCorrectSize()
        {
            int          originalSize       = 100004;
            int          expectedPackedSize = 100016;
            MemoryStream original           = new MemoryStream(originalSize);
            byte         randomData         = 200;

            for (var i = 0; i < originalSize; i++)
            {
                original.WriteByte(randomData);
            }
            MemoryStream          inconteiner           = new MemoryStream();
            CryptContainerStorage cryptContainerStorage = new CryptContainerStorage(original, inconteiner, new MockCrypter());

            cryptContainerStorage.Pack();

            //размер запакованного = (размер исходного расширенный до крастности 8) + 8 байт
            Assert.AreEqual(expectedPackedSize, inconteiner.Length);

            inconteiner.Position = 0;
            byte[] buffer = new byte[8];
            inconteiner.Read(buffer, 0, 8);
            long writedSizeVariable = ArrayToLong(buffer);
        }