Example #1
0
        public void TestInvalidCipherCreateCall()
        {
            var config = new CryptoConfig();

            Assert.Throws <ArgumentException>(
                () => CryptoFactory.CreateCipher(config));
        }
        /// <summary>
        /// Triggers processing of CryptoConfig object.
        /// Passes Config object to processing class constructors.
        /// </summary>
        /// <param name="plainText">string coming from MainView's TextEditor.</param>
        /// <param name="config">CryptoConfig object holding all config and cipher parameters.</param>
        /// <returns>updated CryptoConfig object</returns>
        public static CryptoConfig ProcessConfigOnSave(string plainText, CryptoConfig config)
        {
            if (config.IsEncryptActive)
            {
                if (config.IsPbeActive)
                {
                    var pbeBuilder = CryptoFactory.CreatePbe(config);
                    config = pbeBuilder.GenerateKeyBytes();
                }

                var cipherBuilder = CryptoFactory.CreateCipher(config);
                config = cipherBuilder.EncryptTextToBytes(plainText);
            }

            if (!config.IsIntegrityActive)
            {
                return(config);
            }

            if (config.Integrity == Integrity.Dsa)
            {
                var certBuilder = CryptoFactory.CreateCert(config);
                certBuilder.GenerateCerts();
                config = certBuilder.SignInput(config.Cipher);
            }
            else
            {
                var digestBuilder = CryptoFactory.CreateDigest(config);
                config = digestBuilder.SignInput(config.Cipher);
            }

            return(config);
        }
        public void TestValidPbkdf2Sha256Key()
        {
            var config = new CryptoConfig
            {
                IsEncryptActive = true,
                IsPbeActive     = true,
                CipherAlgorithm = CipherAlgorithm.AES,
                KeySize         = 256,
                PbeAlgorithm    = PbeAlgorithm.PBKDF2,
                PbeDigest       = PbeDigest.SHA256,
                PbePassword     = "******".ToCharArray(),
                BlockMode       = BlockMode.CBC,
                Padding         = Padding.Pkcs7,
            };

            var pbeBuilder = CryptoFactory.CreatePbe(config);

            config = pbeBuilder.GenerateKeyBytes();

            var cipherBuilder = CryptoFactory.CreateCipher(config);

            config = cipherBuilder.EncryptTextToBytes("Hallo Welt");
            var decodedCipher = Convert.FromBase64String(config.Cipher);

            var result = cipherBuilder.DecryptBytesToText(decodedCipher);

            Assert.Equal("Hallo Welt", result);
        }
Example #4
0
        public void TestInvalidCtsInput()
        {
            var config = new CryptoConfig
            {
                IsEncryptActive = true,
                BlockMode       = BlockMode.CTS,
                CipherAlgorithm = CipherAlgorithm.AES,
                KeySize         = 128,
                Padding         = Padding.Pkcs7
            };

            var cipherBuilder = CryptoFactory.CreateCipher(config);

            Assert.Throws <Org.BouncyCastle.Crypto.DataLengthException>(
                () => cipherBuilder.EncryptTextToBytes("Hallo Welt"));
        }
        /// <summary>
        /// Triggers processing of CryptoConfig object.
        /// Passes Config object to processing class constructors.
        /// Also verifies integrity of cipher if used.
        /// </summary>
        /// <param name="config">config state after loading all files from disk.</param>
        /// <returns>decrypted cipher in string representation.</returns>
        public static string ProcessConfigOnLoad(CryptoConfig config)
        {
            if (config.IsIntegrityActive)
            {
                if (config.Integrity == Integrity.Dsa)
                {
                    var certBuilder = CryptoFactory.CreateCert(config);
                    certBuilder.GenerateCerts();
                    config = certBuilder.SignInput(config.Cipher);

                    var result = certBuilder.VerifySign(config.Signature, config.Cipher);
                    Console.WriteLine($"Signature verified: {result}");
                }
                else
                {
                    var certBuilder = CryptoFactory.CreateDigest(config);
                    config = certBuilder.SignInput(config.Cipher);

                    var result = certBuilder.VerifySign(config.Signature, config.Cipher);
                    Console.WriteLine($"Digest verified: {result}");
                }
            }

            if (config.IsPbeActive)
            {
                var pbeBuilder = CryptoFactory.CreatePbe(config);
                config = pbeBuilder.GenerateKeyBytes();
            }

            var cipherBuilder = CryptoFactory.CreateCipher(config);

            try
            {
                return(cipherBuilder.DecryptBytesToText(Convert.FromBase64String(config.Cipher)));
            }
            catch (Org.BouncyCastle.Crypto.InvalidCipherTextException e)
            {
                Console.WriteLine(e);
                return("GCM mac error.");
            }
            catch (FormatException e)
            {
                Console.WriteLine(e);
                return("Format error.");
            }
        }
Example #6
0
        public void TestValidRc4Cipher()
        {
            var config = new CryptoConfig
            {
                IsEncryptActive = true,
                CipherAlgorithm = CipherAlgorithm.RC4,
                KeySize         = 2048
            };

            var cipherBuilder = CryptoFactory.CreateCipher(config);

            config = cipherBuilder.EncryptTextToBytes("Hallo Welt");
            var decodedCipher = Convert.FromBase64String(config.Cipher);

            var result = cipherBuilder.DecryptBytesToText(decodedCipher);

            Assert.Equal("Hallo Welt", result);
        }
Example #7
0
        public void TestValidAes192EcbZeroByteCipher()
        {
            var config = new CryptoConfig
            {
                IsEncryptActive = true,
                CipherAlgorithm = CipherAlgorithm.AES,
                KeySize         = 192,
                BlockMode       = BlockMode.ECB,
                Padding         = Padding.ZeroByte
            };

            var cipherBuilder = CryptoFactory.CreateCipher(config);

            config = cipherBuilder.EncryptTextToBytes("Hallo Welt");
            var decodedCipher = Convert.FromBase64String(config.Cipher);

            var result = cipherBuilder.DecryptBytesToText(decodedCipher);

            Assert.Equal("Hallo Welt", result);
        }