public void Encrypt_Decrypt_stream_to_file_with_generated_key_iv()
        {
            // Encrypt file
            string key, iv;

            using (var fsIn = new FileStream(_filePlainData, FileMode.Open, FileAccess.Read))
            {
                Bytes.Encrypt(fsIn, _fileEncryptedData, out key, out iv);
            }

            // Decrypt file data
            using (var memoryStream = new MemoryStream())
            {
                Bytes.Decrypt(_fileEncryptedData, memoryStream, key, iv);

                // Verify
                memoryStream.Seek(0, SeekOrigin.Begin);
                Assert.AreEqual(_plainData.Length, memoryStream.Length);

                foreach (var expected in _plainData)
                {
                    var b = memoryStream.ReadByte();
                    Assert.AreEqual(expected, b);
                }
            }
        }
        public void Encrypt_Decrypt_stream_to_file_using_own_Rijndael_algorithm()
        {
            // Encrypt file
            using (var fsIn = new FileStream(_filePlainData, FileMode.Open, FileAccess.Read))
            {
                Bytes.Encrypt(fsIn, _fileEncryptedData, _rijndaelManaged);
            }

            // Decrypt file data
            using (var memoryStream = new MemoryStream())
            {
                using (var fsIn = new FileStream(_fileEncryptedData, FileMode.Open, FileAccess.Read))
                {
                    Bytes.Decrypt(fsIn, memoryStream, _rijndaelManaged);
                }

                // Verify
                memoryStream.Seek(0, SeekOrigin.Begin);
                Assert.AreEqual(_plainData.Length, memoryStream.Length);

                foreach (var expected in _plainData)
                {
                    var b = memoryStream.ReadByte();
                    Assert.AreEqual(expected, b);
                }
            }
        }
Exemple #3
0
        public void Generate_key_iv_encrypt_decrypt()
        {
            foreach (var keySize in (Bytes.KeySize[])Enum.GetValues(typeof(Bytes.KeySize)))
            {
                if (keySize == Bytes.KeySize.Default)
                {
                    continue;
                }
                foreach (var blockSize in (Bytes.BlockSize[])Enum.GetValues(typeof(Bytes.BlockSize)))
                {
                    if (blockSize == Bytes.BlockSize.Default)
                    {
                        continue;
                    }
                    Console.WriteLine("KeySize {0} BlockSize {1}", keySize, blockSize);

                    var key = Bytes.GenerateKey(keySize, blockSize);
                    var iv  = Bytes.GenerateIV(keySize, blockSize);

                    var data = new byte[1024];
                    Bytes.GetRandomBytes(data);

                    var encrypted = Bytes.Encrypt(data, key, iv, keySize, blockSize);
                    var decrypted = Bytes.Decrypt(encrypted, key, iv, keySize, blockSize);
                    Assert.AreEqual(data, decrypted);
                }
            }
        }
        public void Encrypt_32k_10_times_using_all_padding_and_cypher_modes()
        {
            foreach (var paddingMode in (PaddingMode[])Enum.GetValues(typeof(PaddingMode)))
            {
                if (paddingMode == PaddingMode.None)
                {
                    continue;
                }

                foreach (var cipherMode in (CipherMode[])Enum.GetValues(typeof(CipherMode)))
                {
                    if (!Bytes.SetPaddingAndCipherModes(paddingMode, cipherMode))
                    {
                        continue; // invalid padding/cipher mode
                    }
                    Console.WriteLine("Padding Mode {0} CipherMode Mode {1}", paddingMode, cipherMode);

                    var key    = Bytes.GenerateKey();
                    var iv     = Bytes.GenerateIV();
                    var random = new Random();

                    for (var n = 0; n < 10; n++)
                    {
                        var size = random.Next(32768) + 1;
                        var data = new byte[size];
                        Bytes.GetRandomBytes(data);

                        var encrypted = Bytes.Encrypt(data, key, iv);
                        var decrypted = Bytes.Decrypt(encrypted, key, iv);
                        Assert.AreEqual(data, decrypted);
                    }
                }
            }
            Bytes.ResetPaddingAndCipherModes();
        }
        public void Generate_key_with_password_and_salt_iv_encrypt_decrypt_128(int iterationCount)
        {
            byte[] key = Bytes.GenerateKey("password", "saltsaltsalt", Bytes.KeySize.Size128, iterationCount);
            byte[] iv  = Bytes.GenerateIV();

            var rng  = new RNGCryptoServiceProvider();
            var data = new byte[1024];

            rng.GetBytes(data);

            byte[] encrypted = Bytes.Encrypt(data, key, iv);
            byte[] decrypted = Bytes.Decrypt(encrypted, key, iv);
            Assert.AreEqual(data, decrypted);
        }
        public void Generate_key_iv_encrypt_decrypt()
        {
            byte[] key = Bytes.GenerateKey();
            byte[] iv  = Bytes.GenerateIV();

            var rng  = new RNGCryptoServiceProvider();
            var data = new byte[1024];

            rng.GetBytes(data);

            byte[] encrypted = Bytes.Encrypt(data, key, iv);
            byte[] decrypted = Bytes.Decrypt(encrypted, key, iv);
            Assert.AreEqual(data, decrypted);
        }
Exemple #7
0
        public void Generate_key_with_password_and_salt_iv_encrypt_decrypt_256(int iterationCount)
        {
            var key = Bytes.GenerateKey("password", "saltsaltsalt", Bytes.KeySize.Size256, iterationCount);
            var iv  = Bytes.GenerateIV();

            var data = new byte[1024];

            Bytes.GetRandomBytes(data);

            var encrypted = Bytes.Encrypt(data, key, iv);
            var decrypted = Bytes.Decrypt(encrypted, key, iv);

            Assert.AreEqual(data, decrypted);
        }
Exemple #8
0
        public void Generate_key_iv_encrypt_decrypt()
        {
            var key = Bytes.GenerateKey();
            var iv  = Bytes.GenerateIV();

            var data = new byte[1024];

            Bytes.GetRandomBytes(data);

            var encrypted = Bytes.Encrypt(data, key, iv);
            var decrypted = Bytes.Decrypt(encrypted, key, iv);

            Assert.AreEqual(data, decrypted);
        }
Exemple #9
0
        public void Encrypt_Decrypt_file_to_file_with_generated_key_iv()
        {
            Bytes.Encrypt(_filePlainData, _fileEncryptedData, out string key, out string iv);
            Bytes.Decrypt(_fileEncryptedData, _filePlainData, key, iv);

            var decryptedData = File.ReadAllBytes(_filePlainData);

            Assert.AreEqual(_plainData.Length, decryptedData.Length);

            for (var i = 0; i < _plainData.Length; i++)
            {
                Assert.AreEqual(_plainData[i], decryptedData[i]);
            }
        }
        public void Encrypt_Decrypt_file_to_file_using_key_iv()
        {
            byte[] key = Bytes.GenerateKey();
            byte[] iv  = Bytes.GenerateIV();

            Bytes.Encrypt(_filePlainData, _fileEncryptedData, key, iv);
            Bytes.Decrypt(_fileEncryptedData, _filePlainData, key, iv);

            var decryptedData = File.ReadAllBytes(_filePlainData);

            Assert.AreEqual(_plainData.Length, decryptedData.Length);

            for (int i = 0; i < _plainData.Length; i++)
            {
                Assert.AreEqual(_plainData[i], decryptedData[i]);
            }
        }
Exemple #11
0
        public void Encrypt_32k_100_times()
        {
            var key    = Bytes.GenerateKey();
            var iv     = Bytes.GenerateIV();
            var random = new Random();

            for (var n = 0; n < 100; n++)
            {
                var size = random.Next(32768) + 1;
                var data = new byte[size];
                Bytes.GetRandomBytes(data);

                var encrypted = Bytes.Encrypt(data, key, iv);
                var decrypted = Bytes.Decrypt(encrypted, key, iv);
                Assert.AreEqual(data, decrypted);
            }
        }
        public void Encrypt_32k_100_times()
        {
            byte[] key    = Bytes.GenerateKey();
            byte[] iv     = Bytes.GenerateIV();
            var    random = new Random();

            var rng = new RNGCryptoServiceProvider();

            for (int n = 0; n < 100; n++)
            {
                int size = random.Next(32768) + 1;
                var data = new byte[size];
                rng.GetBytes(data);

                byte[] encrypted = Bytes.Encrypt(data, key, iv);
                byte[] decrypted = Bytes.Decrypt(encrypted, key, iv);
                Assert.AreEqual(data, decrypted);
            }
        }
Exemple #13
0
        public void Generate_key_with_password_and_salt_iv_encrypt_decrypt_256(int iterationCount)
        {
            foreach (var keySize in (Bytes.KeySize[])Enum.GetValues(typeof(Bytes.KeySize)))
            {
                if (keySize == Bytes.KeySize.Default)
                {
                    continue;
                }
                Console.WriteLine("KeySize {0}", keySize);
                var key = Bytes.GenerateKey("password", "saltsaltsalt", keySize, iterationCount);
                var iv  = Bytes.GenerateIV();

                var data = new byte[1024];
                Bytes.GetRandomBytes(data);

                var encrypted = Bytes.Encrypt(data, key, iv);
                var decrypted = Bytes.Decrypt(encrypted, key, iv);
                Assert.AreEqual(data, decrypted);
            }
        }
Exemple #14
0
        public void Encrypt_Decrypt_stream_to_file_using_own_Rijndael_algorithm()
        {
            foreach (var paddingMode in (PaddingMode[])Enum.GetValues(typeof(PaddingMode)))
            {
                if (paddingMode == PaddingMode.None)
                {
                    continue;
                }

                foreach (var cipherMode in (CipherMode[])Enum.GetValues(typeof(CipherMode)))
                {
                    if (!Bytes.SetPaddingAndCipherModes(paddingMode, cipherMode))
                    {
                        continue; // invalid padding/cipher mode
                    }
                    Console.WriteLine("Padding Mode {0} CipherMode Mode {1}", paddingMode, cipherMode);
                    foreach (var keySize in (Bytes.KeySize[])Enum.GetValues(typeof(Bytes.KeySize)))
                    {
                        if (keySize == Bytes.KeySize.Default)
                        {
                            continue;
                        }
                        foreach (var blockSize in (Bytes.BlockSize[])Enum.GetValues(typeof(Bytes.BlockSize)))
                        {
                            if (blockSize == Bytes.BlockSize.Default)
                            {
                                continue;
                            }
                            Console.WriteLine("    KeySize {0} BlockSize {1}", keySize, blockSize);

                            var rm = new RijndaelManaged
                            {
                                KeySize   = (int)keySize,
                                BlockSize = (int)blockSize,
                                Padding   = paddingMode,
                                Mode      = cipherMode
                            };

                            // Encrypt file
                            using (var fsIn = new FileStream(_filePlainData, FileMode.Open, FileAccess.Read))
                            {
                                Bytes.Encrypt(fsIn, _fileEncryptedData, rm);
                            }

                            // Decrypt file data
                            using (var memoryStream = new MemoryStream())
                            {
                                using (var fsIn = new FileStream(_fileEncryptedData, FileMode.Open, FileAccess.Read))
                                {
                                    Bytes.Decrypt(fsIn, memoryStream, rm);
                                }

                                // Verify
                                memoryStream.Seek(0, SeekOrigin.Begin);
                                Assert.AreEqual(_plainData.Length, memoryStream.Length);

                                foreach (var expected in _plainData)
                                {
                                    var b = memoryStream.ReadByte();
                                    Assert.AreEqual(expected, b);
                                }
                            }
                        }
                    }
                }
            }
        }