Exemple #1
0
        private void EncryptStreamWithSize(long streamSize, bool checkResult = true)
        {
            long minSize = streamSize + EnvelopeCryptoProvider.IVBytes;
            long maxSize = minSize + EnvelopeCryptoProvider.BlockBytes;

            List <byte> cipherText = null;

            if (checkResult)
            {
                cipherText = new List <byte>((int)streamSize + EnvelopeCryptoProvider.BlockBytes * 2);
            }

            Stream plainStream = new FakeStream(streamSize);

            byte[] dataKey;
            using (Stream cipherStream = Provider.Encrypt(out dataKey, plainStream))
            {
                long total  = 0;
                var  buffer = new byte[1024 * 512];
                int  bytesRead;
                while ((bytesRead = cipherStream.Read(buffer, 0, buffer.Length)) != 0)
                {
                    total += bytesRead;
                    if (checkResult)
                    {
                        cipherText.AddRange(buffer.Take(bytesRead));
                    }
                }
                total.Should().BeInRange(minSize, maxSize);
            }

            if (checkResult)
            {
                plainStream = new FakeStream(streamSize);
                var expected = new byte[(int)streamSize];
                plainStream.Read(expected, 0, (int)streamSize);
                Provider.Decrypt(dataKey, cipherText.ToArray()).Should().Equal(expected);
            }
        }
		private void EncryptStreamWithSize(long streamSize, bool checkResult = true)
		{
			long minSize = streamSize + EnvelopeCryptoProvider.IVBytes;
			long maxSize = minSize + EnvelopeCryptoProvider.BlockBytes;

			List<byte> cipherText = null;
			if (checkResult)
			{
				cipherText = new List<byte>((int) streamSize + EnvelopeCryptoProvider.BlockBytes * 2);
			}

			Stream plainStream = new FakeStream(streamSize);
			byte[] dataKey;
			using (Stream cipherStream = Provider.Encrypt(out dataKey, plainStream))
			{
				long total = 0;
				var buffer = new byte[1024 * 512];
				int bytesRead;
				while ((bytesRead = cipherStream.Read(buffer, 0, buffer.Length)) != 0)
				{
					total += bytesRead;
					if (checkResult)
					{
						cipherText.AddRange(buffer.Take(bytesRead));
					}
				}
				total.Should().BeInRange(minSize, maxSize);
			}

			if (checkResult)
			{
				plainStream = new FakeStream(streamSize);
				var expected = new byte[(int) streamSize];
				plainStream.Read(expected, 0, (int) streamSize);
				Provider.Decrypt(dataKey, cipherText.ToArray()).Should().Equal(expected);
			}
		}