Esempio n. 1
0
        public void TestRoundtrip()
        {
            // ARRANGE
            // =======
            // File
            FileInformation fi = new FileInformation();

            byte[] sourceFile = fi.FileContents;

            // Encryption and Decryption
            FormMain        mainForm = new FormMain(); // MainForm also holds user preferences in memory.
            PasswordHandler pwh      = new PasswordHandler("password", mainForm);
            AESencrypter    aesEnc   = new AESencrypter(fi.GenerateFileInfoHeader(), fi.FileContents, mainForm);
            AESdecrypter    aesDec   = new AESdecrypter(mainForm);

            byte[] encryptedFile;
            byte[] decryptedFile;

            // Bitmap Encoder and Decoder
            BitmapEncoder bmpEncoder = new BitmapEncoder();
            BitmapDecoder bmpDecoder = new BitmapDecoder(false);
            Bitmap        encodedBitmap;

            byte[] bytesFromImage;

            // Header Parser
            HeaderParser hp = new HeaderParser();

            byte[] parsedDecrypted;

            // ACT
            // ===
            Task.Run(async() => {
                // Encrypt the file.
                encryptedFile = aesEnc.EncryptBytes();

                // Encode the encrypted file into the bitmap.
                encodedBitmap = await bmpEncoder.EncodedBitmap(encryptedFile, aesEnc.InitializationVector);

                // Retrieve the encrypted bytes back out of the bitmap.
                bytesFromImage = await bmpDecoder.BytesFromImage(encodedBitmap);

                // Decrypt the bytes pulled from the image.
                decryptedFile = aesDec.DecryptedBytes(bytesFromImage, mainForm.EncryptionKey, aesEnc.InitializationVector);

                // Parse the header from the decrypted file.
                parsedDecrypted = hp.fileContentsWithoutHeader(decryptedFile);

                // ASSERT
                // ======
                for (int i = 0; i < fi.FileContents.Length; i++)
                {
                    // Assert that the bytes that went in are the same as the bytes that came out.
                    Assert.AreEqual(fi.FileContents[i], parsedDecrypted[i]);
                }
            }).GetAwaiter().GetResult();
        }
Esempio n. 2
0
        public void TestBMPEncoderAndBMPDecoder()
        {
            // ARRANGE
            BitmapEncoder bmpE = new BitmapEncoder();

            byte[] file = new byte[] { 10, 249, 12, 1, 180, 29, 2, 78, 45, 12, 12, 13, 69, 45, 78, 111 };
            byte[] IV   = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };

            byte[] bothTogether = new byte[file.Length + IV.Length];
            for (int i = 0; i < IV.Length; i++)
            {
                bothTogether[i] = IV[i];
            }

            for (int i = 0; i < file.Length; i++)
            {
                bothTogether[i + IV.Length] = file[i];
            }

            BitmapDecoder bmpD = new BitmapDecoder(false);
            Bitmap        encBMP;

            byte[] bytesFromImage;
            byte[] onlyRelevantBytes;
            // ACT

            Task.Run(async() =>
            {
                encBMP         = await bmpE.EncodedBitmap(file, IV);
                bytesFromImage = await bmpD.BytesFromImage(encBMP);

                // Trim excess off end of bytesFromImage.
                onlyRelevantBytes = new byte[bothTogether.Length];
                for (int i = 0; i < bothTogether.Length; i++)
                {
                    onlyRelevantBytes[i] = bytesFromImage[i];
                }
                Trace.WriteLine("XXXXX Comparing " + onlyRelevantBytes.Length + "|" + bothTogether.Length);
                // ASSERT
                for (int i = 0; i < bothTogether.Length; i++)
                {
                    Assert.AreEqual(onlyRelevantBytes[i], bothTogether[i]);
                }
            }).GetAwaiter().GetResult();
        }