Example #1
0
        static void Main(string[] args)
        {
            if (args.Length < 2)
            {
                Console.WriteLine("Syntax: EncryptFile.exe <input-file> <output-file>");
                Console.WriteLine("  For decryption, swap input and output file");
                return;
            }

            byte[] key = new byte[]
            {
                0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x6, 0x7,
                0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0xE, 0xF
            };

            ICryptoTransform cipher = RabbitCipher.CreateEncryptor(key);

            byte[] buffer = new byte[10000];

            Stopwatch sw = new Stopwatch();

            sw.Start();

            using (FileStream infs = new FileStream(args[0], FileMode.Open, FileAccess.Read))
                using (FileStream outfs = new FileStream(args[1], FileMode.Create, FileAccess.Write))
                    using (CryptoStream cs = new CryptoStream(outfs, cipher, CryptoStreamMode.Write))
                    {
                        while (true)
                        {
                            int r = infs.Read(buffer, 0, buffer.Length);
                            if (r <= 0)
                            {
                                break;
                            }
                            cs.Write(buffer, 0, r);
                        }
                    }

            sw.Stop();
            Console.WriteLine($"Elapsed time: {sw.ElapsedMilliseconds} ms");
        }
        private void MainTest()
        {
            RabbitCipher rc = new RabbitCipher(); //initialize

            //generate IV And Key to be Used
            rc.GenerateIV();
            rc.GenerateKey();
            //----------------------------<<

            //begin encrypting
            try
            {
                using (SymmetricAlgorithm RabbitCipher = new RabbitCipher())
                    using (ICryptoTransform transform = RabbitCipher.CreateEncryptor(rc.Key, rc.IV))
                    {
                        byte[] test_file = new byte[7675];             //not an actual file
                        byte[] output    = new byte[test_file.Length]; //encyption does not change file size
                        if (test_file.Length % 16 != 0)                //encrypts in bytes of 16 so if it is not perfectly made of 16 blocks we will need to transform the final block
                        {
                            int encrypted_bytes = transform.TransformBlock(test_file, 0, test_file.Length, output, 0);
                            //now the data is all encrypted in the output byte[] array
                        }
                        else
                        {
                            int encrypted_bytes = transform.TransformBlock(test_file, 0, test_file.Length, output, 0);
                            //now the data is all encrypted in the output byte[] array
                        }
                    }
            }
            catch (Exception ex)
            {
                Console.WriteLine("\nThere was An Error When Encrypting <> Error Code:\n\n" + ex.ToString());
            }
            //------------------<<

            //begin Decrypting
            try
            {
                using (SymmetricAlgorithm RabbitCipher = new RabbitCipher())
                    using (ICryptoTransform transform = RabbitCipher.CreateDecryptor(rc.Key, rc.IV))
                    {
                        byte[] encrypted_data = new byte[4343];
                        byte[] plaintext_data = new byte[encrypted_data.Length];
                        if (encrypted_data.Length % 16 != 0)
                        {
                            int decrypted_bytes = transform.TransformBlock(encrypted_data, 0, encrypted_data.Length, plaintext_data, 0);
                            //now decrypted
                        }
                        else
                        {
                            transform.TransformBlock(encrypted_data, 0, encrypted_data.Length, plaintext_data, 0);
                            //now decrypted
                        }
                    }
            }
            catch (Exception ex)
            {
                Console.WriteLine("\nThere was An Error When Decrypting <> Error Code:\n\n" + ex.ToString());
            }
            //------------------<<
        }