예제 #1
0
        public void VerifyDecryptedSizeCalculations()
        {
            /*
             *  Test with 1 byte input
             */
            int   encryptedFileSize = EncryptedFileHeader.HeaderSize + 16 + 1;
            short padding;
            var   decryptedSize = EncryptionManager.CalculateDecryptedFileSize(encryptedFileSize, out padding);

            Assert.AreEqual(1, decryptedSize);
            Assert.AreEqual(1, padding);
        }
예제 #2
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);
                }
            }
        }