Пример #1
0
        /// <summary>
        /// Tests EncryptStream and DecryptStream using cloud storage. A single
        /// file is uploaded in encrypted form and then downloaded.
        /// </summary>
        public void test_Encrypt_and_Decrypt_streams_on_cloud()
        {
            Console.WriteLine("About to upload a file to the cloud and download it back, "
                              + "using EncryptStream and DecryptStream. Press any key to continue, or ESC to skip.\n");
            if (hit_esc_key())
            {
                return;
            }

            var backup_services = create_cloud_backup_services_from_xml(cloud_backup_services_xml);

            // Choose one of the "backup_services" for use in testing.
            CloudBackupService backup_service = backup_services[2];
            string             bucket_name    = "xxxxxxxx";

            string original_file = @"E:\temp\temp\temp.bin";
            string base_path     = @"E:\temp\temp";
            string relative_path = original_file.Substring(base_path.Length + 1);

            string decrypted_file = @"E:\temp\temp2\temp.bin";

            var encrypt_stream = new EncryptStream();
            var decrypt_stream = new DecryptStream();

            Random random = new Random();

            byte[] buffer = new byte[1024];

            // create random file
            using (var fs = new FileStream(original_file, FileMode.Create, FileAccess.Write))
            {
                for (int j = 0; j < 640; j++)
                {
                    random.NextBytes(buffer);
                    fs.Write(buffer, 0, buffer.Length);
                }
            }

            // encrypted upload
            Console.WriteLine("Encrypting and uploading file to the cloud.");
            encrypt_stream.reset(original_file, relative_path, key);
            backup_service.upload(encrypt_stream, bucket_name, "temp.bin");

            Console.WriteLine("The file has been encrypted and uploaded to the cloud.");
            Thread.Sleep(2000);

            // download and decrypt
            decrypt_stream.reset(key, full_path_request_handler);

            // be sure the download can be done in pieces
            backup_service.download(bucket_name, "temp.bin", decrypt_stream, 0, 1024);
            Console.WriteLine("The first 1kB header says the pre-encrypt file size is "
                              + file_pre_encrypt_size + " bytes.");

            backup_service.download(bucket_name, "temp.bin", decrypt_stream, 1024, file_pre_encrypt_size);

            decrypt_stream.Flush();
            backup_service.delete(bucket_name, "temp.bin");

            Debug.Assert(compare_files(original_file, decrypted_file));

            Console.WriteLine("EncryptStream and DecryptStream has been tested using cloud storage.\n");
        }
Пример #2
0
        /// <summary>
        /// Tests EncryptStream and DecryptStream using disk files. Different files
        /// are encrypted and then decrypted, both with and without compression.
        /// </summary>
        public void test_Encrypt_and_Decrypt_streams_on_disk()
        {
            Console.WriteLine("About to test EncryptStream and DecryptStream using files on disk."
                              + " Press any key to continue, or ESC to skip.\n");
            if (hit_esc_key())
            {
                return;
            }

            // paths:
            string original_file = @"E:\temp\temp\temp.bin";
            string base_path     = @"E:\temp\temp";
            string relative_path = original_file.Substring(base_path.Length + 1);

            string encrypted_file = @"E:\temp\temp2\temp_en.bin";
            string decrypted_file = @"E:\temp\temp2\temp.bin";

            var encrypt_stream = new EncryptStream();
            var decrypt_stream = new DecryptStream();

            Random random = new Random();

            for (int i = 0; i < 5; i++)
            {
                bool do_not_compress = false;

                // The different tests try different input files.
                if (i == 0)
                {
                    // zero byte file
                    using (var fs = new FileStream(original_file, FileMode.Create, FileAccess.Write)) { }
                }
                else if (i == 1)
                {
                    // one byte file
                    using (var fs = new FileStream(original_file, FileMode.Create, FileAccess.Write))
                        fs.WriteByte((byte)(random.Next(0, 256)));
                }

                else if (i == 2 || i == 3)
                {
                    // test #2: file with repetitive bytes
                    // test #3: file with repetitive bytes - no compression
                    if (i == 3)
                    {
                        do_not_compress = true;
                    }

                    byte[][] patterns = new byte[32][];
                    for (int j = 0; j < patterns.Length; j++)
                    {
                        patterns[j] = new byte[32];
                        random.NextBytes(patterns[j]);
                    }

                    using (var fs = new FileStream(original_file, FileMode.Create, FileAccess.Write))
                    {
                        for (int j = 0; j < 1024 * 10; j++)
                        {
                            int pattern_to_use = random.Next(0, 32);
                            fs.Write(patterns[pattern_to_use], 0, patterns[pattern_to_use].Length);
                        }
                    }
                }

                else if (i == 4)
                {
                    // random file
                    byte[] buffer = new byte[1024];

                    using (var fs = new FileStream(original_file, FileMode.Create, FileAccess.Write))
                    {
                        for (int j = 0; j < 1024; j++)
                        {
                            random.NextBytes(buffer);
                            fs.Write(buffer, 0, buffer.Length);
                        }
                    }
                }

                // The rest of the test is the same - encrypt, decrypt, the compare files.

                encrypt_stream.reset(original_file, relative_path, key, do_not_compress);
                using (var output_fs = new FileStream(encrypted_file, FileMode.Create))
                    encrypt_stream.CopyTo(output_fs);

                decrypt_stream.reset(key, full_path_request_handler);
                using (var fs = new FileStream(encrypted_file, FileMode.Open))
                    fs.CopyTo(decrypt_stream);
                decrypt_stream.Flush();

                Debug.Assert(compare_files(original_file, decrypted_file));
            }

            Console.WriteLine("EncryptStream and DecryptStream tested using disk.\n");
        }