public void ConstructorSecureString()
        {
            string clearText = GenerateClearText();

            SecureString key = ToSS(GeneratePassPhrase());

            ICryptoEngine engine = new DESEngine(key);

            string encrypted = engine.Encrypt(clearText);
            string decrypted = engine.Decrypt(encrypted);

            Assert.NotEqual(clearText, encrypted);
            Assert.Equal(clearText, decrypted);
        }
        public void SetInitVectorString()
        {
            string clearText = GenerateClearText();

            string key = GeneratePassPhrase();
            string init = GenerateInitVector();

            ICryptoEngine engine = new DESEngine(key)
                .SetInitVector(init);

            string encrypted = engine.Encrypt(clearText);
            string decrypted = engine.Decrypt(encrypted);

            Assert.NotEqual(clearText, encrypted);
            Assert.Equal(clearText, decrypted);
        }
        private ICryptoEngine GenerateEngine()
        {
            var engine = new DESEngine(txtKey.Text);

            if (cbxUseKeySize.Checked)
            {
                var keySize = EnumerationConversions.GetEnumName<DESKeySize>(cmbKeySize.SelectedItem.ToString());

                engine.SetKeySize(keySize);
            }

            if (cbxUseInitVector.Checked)
            {
                engine.SetInitVector(txtInitVector.Text);
            }

            if (cbxUseKeySalt.Checked)
            {
                engine.SetSalt(txtSalt.Text);
            }

            if (cbxUseRandomSalt.Checked)
            {
                engine.SetRandomSaltLength((int)nudSaltMin.Value, (int)nudSaltMax.Value);
            }

            if (cbxUsePasswordIterations.Checked)
            {
                engine.SetIterations((int)nudIterations.Value);
            }

            if (cbxUseEncoding.Checked)
            {
                engine.SetEncoding(cmbEncoding.SelectedItem as Encoding);
            }

            if (cbxUseHashAlgorithm.Checked)
            {
                engine.SetHashAlgorithm((HashType)cmbHashAlgorithm.SelectedItem);
            }

            return engine;
        }
        public void SetRandomSaltLength()
        {
            string clearText = GenerateClearText();

            string key = GeneratePassPhrase();
            string init = GenerateInitVector();

            var minSalt = (byte)DataGenerator.NextInteger(4, 100);
            var maxSalt = (byte)DataGenerator.NextInteger(100, 250);

            ICryptoEngine engine = new DESEngine(key)
                .SetInitVector(init)
                .SetRandomSaltLength(minSalt, maxSalt);

            string encrypted = engine.Encrypt(clearText);
            string decrypted = engine.Decrypt(encrypted);

            Assert.NotEqual(clearText, encrypted);
            Assert.Equal(clearText, decrypted);
        }
        public void InvalidDecryption()
        {
            string randomData = GenerateClearText();

            string fake = Convert.ToBase64String(Encoding.UTF8.GetBytes(randomData));

            string key = GeneratePassPhrase();
            string init = GenerateInitVector();

            ICryptoEngine engine = new DESEngine(key)
                .SetInitVector(init);

            Assert.Throws<CryptographicException>(delegate
            {
                engine.Decrypt(fake);
            });
        }
        public void SetEncoding(Encodings encodingType)
        {
            Encoding encoding = null;

            switch (encodingType)
            {
                //case Encodings.None:
                case Encodings.ASCII:
                    encoding = Encoding.ASCII;
                    break;
                case Encodings.UTF7:
                    encoding = Encoding.UTF7;
                    break;
                case Encodings.UTF8:
                    encoding = Encoding.UTF8;
                    break;
            }

            string clearText = GenerateClearText();

            string key = GeneratePassPhrase();

            ICryptoEngine engine = new DESEngine(key)
                .SetEncoding(encoding);

            string encrypted = engine.Encrypt(clearText);
            string decrypted = engine.Decrypt(encrypted);

            Assert.NotEqual(clearText, encrypted);
            Assert.Equal(clearText, decrypted);
        }
        public void SetPasswordIterations()
        {
            string clearText = GenerateClearText();

            string key = GeneratePassPhrase();
            string init = GenerateInitVector();

            var minSalt = (byte)DataGenerator.NextInteger(4, 100);
            var maxSalt = (byte)DataGenerator.NextInteger(100, 250);
            string saltKey = GenerateRandomSalt();

            var iterations = (byte)DataGenerator.NextInteger(1, 10);

            ICryptoEngine engine = new DESEngine(key)
                .SetInitVector(init)
                .SetRandomSaltLength(minSalt, maxSalt)
                .SetSalt(saltKey)
                .SetKeySize(DESKeySize.Key64Bit)
                .SetIterations(iterations);

            string encrypted = engine.Encrypt(clearText);
            string decrypted = engine.Decrypt(encrypted);

            Assert.NotEqual(clearText, encrypted);
            Assert.Equal(clearText, decrypted);
        }