Beispiel #1
0
        public void VerifyEncryptedSizeCalculationsModBlockSize()
        {
            /*
             *  Test with size that is (MOD BlockSize == 0)
             */
            short padding;
            var   encryptedSize = EncryptionManager.CalculateEncryptedFileSize(1024, out padding);

            // Header size is 1k bytes. Expect to see header + plaintext
            Assert.AreEqual(EncryptedFileHeader.HeaderSize + 1024 + 16, encryptedSize);
            Assert.AreEqual(0, padding);
        }
Beispiel #2
0
        public void VerifyEncryptedSizeCalculations1Byte()
        {
            /*
             *  Test with 1 byte input
             */
            short padding;
            var   encryptedSize = EncryptionManager.CalculateEncryptedFileSize(1, out padding);

            // Header size is 1k bytes. Expect to see header + blocksize + padding
            int expectedEncryptedSize = EncryptedFileHeader.HeaderSize + 16 + padding;

            Assert.AreEqual(expectedEncryptedSize, encryptedSize);
            Assert.AreEqual(1, padding);
        }
Beispiel #3
0
        public void EncryptAndDecryptFileExactBlockBoundary()
        {
            byte[] inputBuffer = CreateInputBuffer(EncryptedFileHeader.HeaderSize);

            short padding;
            long  expectedEncryptedSize =
                EncryptionManager.CalculateEncryptedFileSize(inputBuffer.Length, out padding);

            MemoryStream inputStream  = null;
            MemoryStream outputStream = null;

            EncryptionManager encryptionManager = null;

            byte[] encryptedData;
            byte[] decryptedData;
            try
            {
                inputStream  = new MemoryStream(inputBuffer);
                outputStream = new MemoryStream();

                encryptionManager = new EncryptionManager(
                    ReadTestCert(),
                    EncryptionMode.Encrypt,
                    outputStream,
                    inputBuffer.Length);

                int    written   = 0;
                int    blockSize = 2048;
                byte[] buf       = new byte[blockSize];

                while (true)
                {
                    int read = inputStream.Read(buf, 0, blockSize);
                    if (read == blockSize)
                    {
                        written += encryptionManager.TransformBlock(buf, 0, read);
                    }
                    else
                    {
                        // Read the end of the input stream
                        written += encryptionManager.TransformFinalBlock(buf, 0, read);
                        break;
                    }
                }

                encryptedData = outputStream.ToArray();

                Assert.AreEqual(expectedEncryptedSize, encryptedData.Length);
                Assert.AreEqual(encryptedData.Length, written);
            }
            finally
            {
                encryptionManager?.Dispose();
                outputStream?.Dispose();
                inputStream?.Dispose();
            }

            long expectedDecryptedSize =
                EncryptionManager.CalculateDecryptedFileSize(expectedEncryptedSize, out padding);

            Assert.AreEqual(expectedDecryptedSize, inputBuffer.Length);

            try
            {
                inputStream  = new MemoryStream(encryptedData);
                outputStream = new MemoryStream();

                encryptionManager = new EncryptionManager(
                    ReadTestCert(),
                    EncryptionMode.Decrypt,
                    outputStream,
                    encryptedData.Length);

                int    written   = 0;
                int    blockSize = 2048;
                byte[] buf       = new byte[blockSize];

                while (true)
                {
                    int read = inputStream.Read(buf, 0, blockSize);
                    if (read == blockSize)
                    {
                        written += encryptionManager.TransformBlock(buf, 0, read);
                    }
                    else
                    {
                        // Read the end of the input stream
                        written += encryptionManager.TransformFinalBlock(buf, 0, read);
                        break;
                    }
                }

                decryptedData = outputStream.ToArray();

                Assert.AreEqual(decryptedData.Length, written);
                Assert.AreEqual(inputBuffer.Length, decryptedData.Length);
            }
            finally
            {
                encryptionManager?.Dispose();
                outputStream?.Dispose();
                inputStream?.Dispose();
            }

            for (int i = 0; i < decryptedData.Length; i++)
            {
                if (decryptedData[i] != i % 256)
                {
                    Assert.Fail("Invalid data at byte " + i);
                }
            }
        }