Exemple #1
0
        public void EncryptAndDecryptFile16Bytes()
        {
            byte[] inputBuffer = CreateInputBuffer(16);

            MemoryStream      outputStream      = null;
            EncryptionManager encryptionManager = null;

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

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

                int written = encryptionManager.TransformFinalBlock(inputBuffer, 0, inputBuffer.Length);

                encryptedData = outputStream.ToArray();

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

            try
            {
                outputStream = new MemoryStream();

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

                int written = encryptionManager.TransformFinalBlock(encryptedData, 0, encryptedData.Length);

                decryptedData = outputStream.ToArray();

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

            Assert.AreEqual(16, decryptedData.Length);
            for (int i = 0; i < 16; i++)
            {
                Assert.AreEqual((byte)i, decryptedData[i]);
            }
        }
Exemple #2
0
        public void EncryptAndDecryptFile1Byte()
        {
            byte[]            inputBuffer       = { 0xab };
            MemoryStream      outputStream      = null;
            EncryptionManager encryptionManager = null;

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

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

                int written = encryptionManager.TransformFinalBlock(inputBuffer, 0, inputBuffer.Length);

                encryptedData = outputStream.ToArray();

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

            try
            {
                outputStream = new MemoryStream();

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

                int written = encryptionManager.TransformFinalBlock(encryptedData, 0, encryptedData.Length);

                decryptedData = outputStream.ToArray();

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

            Assert.AreEqual(1, decryptedData.Length);
            Assert.AreEqual(0xab, decryptedData[0]);
        }
Exemple #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);
                }
            }
        }