public static string Encrypt(string data, AXMLSerializationSettings opt)
 {
     return(AESThenHMAC.SimpleEncryptWithPassword(data, AppSettings.ENCRYPTION_KEY, opt));
 }
Example #2
0
        private static Config UnsecureSensitiveProperties(Config config)
        {
            if (config.KeyA == null || config.KeyB == null || config.KeyC == null)
            {
                Log.Fatal("No keys are viable, something happened w/ the config file and we can't recover Email settings");
                Handler.NotifyError("[Fatal] Encryption Keys aren't viable/are missing, we can't recover Email settings, you will need to reconfigure them.");
                Console.WriteLine("[Fatal] Encryption Keys aren't viable/are missing, we can't recover Email settings, you will need to reconfigure them. Config is being backed up in case you believe you can recover the keys in the config directory/folder");
                Backup();
                UI.StopForMessage();
                return(null);
            }

            config.SMTPUsername  = string.IsNullOrWhiteSpace(config.SMTPUsername) ? "" : AESThenHMAC.SimpleDecrypt(config.SMTPUsername, config.KeyB, config.KeyA, config.KeyC.Length);
            config.SMTPPassword  = string.IsNullOrWhiteSpace(config.SMTPPassword) ? "" : AESThenHMAC.SimpleDecrypt(config.SMTPPassword, config.KeyA, config.KeyC, config.KeyB.Length);
            config.SMTPUrl       = string.IsNullOrWhiteSpace(config.SMTPUrl) ? "" : AESThenHMAC.SimpleDecrypt(config.SMTPUrl, config.KeyC, config.KeyA, config.KeyB.Length);
            config.SMTPEmailFrom = string.IsNullOrWhiteSpace(config.SMTPEmailFrom) ? "" : AESThenHMAC.SimpleDecrypt(config.SMTPEmailFrom, config.KeyB, config.KeyC, config.KeyA.Length);
            config.SMTPEmailName = string.IsNullOrWhiteSpace(config.SMTPEmailName) ? "" : AESThenHMAC.SimpleDecrypt(config.SMTPEmailName, config.KeyC, config.KeyB, config.KeyA.Length);
            return(config);
        }
 private string Decrypt(string data, AXMLSerializationSettings opt)
 {
     return(AESThenHMAC.SimpleDecryptWithPassword(data, ENCRYPTION_KEY, opt));
 }
Example #4
0
        private static Config SecureSensitiveProperties(Config config)
        {
            Config finalConfig = (Config)config.MemberwiseClone();

            if (finalConfig.KeyA == null || finalConfig.KeyB == null || finalConfig.KeyC == null)
            {
                GenerateKeys(finalConfig);
            }

            finalConfig.SMTPUsername  = string.IsNullOrWhiteSpace(finalConfig.SMTPUsername) ? "" : AESThenHMAC.SimpleEncrypt(finalConfig.SMTPUsername, finalConfig.KeyB, finalConfig.KeyA, finalConfig.KeyC);
            finalConfig.SMTPPassword  = string.IsNullOrWhiteSpace(finalConfig.SMTPPassword) ? "" : AESThenHMAC.SimpleEncrypt(finalConfig.SMTPPassword, finalConfig.KeyA, finalConfig.KeyC, finalConfig.KeyB);
            finalConfig.SMTPUrl       = string.IsNullOrWhiteSpace(finalConfig.SMTPUrl) ? "" : AESThenHMAC.SimpleEncrypt(finalConfig.SMTPUrl, finalConfig.KeyC, finalConfig.KeyA, finalConfig.KeyB);
            finalConfig.SMTPEmailFrom = string.IsNullOrWhiteSpace(finalConfig.SMTPEmailFrom) ? "" : AESThenHMAC.SimpleEncrypt(finalConfig.SMTPEmailFrom, finalConfig.KeyB, finalConfig.KeyC, finalConfig.KeyA);
            finalConfig.SMTPEmailName = string.IsNullOrWhiteSpace(finalConfig.SMTPEmailName) ? "" : AESThenHMAC.SimpleEncrypt(finalConfig.SMTPEmailName, finalConfig.KeyC, finalConfig.KeyB, finalConfig.KeyA);
            return(finalConfig);
        }
 public T Decrypt(string source, string salt)
 {
     var e = Encoding.UTF8;
         var decryptedBytes = AESThenHMAC.SimpleDecryptWithPassword(Convert.FromBase64String(source), salt);
         return JsonConvert.DeserializeObject<T>(e.GetString(decryptedBytes));
 }
        private void BtnSalvar_Click(object sender, RoutedEventArgs e)
        {
            ParametroDB paramdbNew = new ParametroDB();
            Processos   proc       = new Processos();

            if (TxbHost.Text != "" && TxbPort.Text != "" && TxbServiceName.Text != "" && TxbUser.Text != "" && PwbSenha.Password != "" && CboDriver.SelectedIndex != -1 && CboConexao.SelectedIndex != -1 && TxbDescr.Text != "" && TxbGrupo.Text != "" && TxbToken.Text != "")
            {
                if (proc.VerificaConexaoBanco(TxbHost.Text, TxbPort.Text, TxbServiceName.Text, TxbUser.Text, PwbSenha.Password, (string)CboDriver.SelectedItem) == true)
                {
                    if (lastIndex != 0)
                    {
                        if (CboDriver.SelectedIndex == 0)
                        {
                            paramDB[lastIndex - 1].Driver = "oracle";
                        }
                        else
                        {
                            paramDB[lastIndex - 1].Driver = "sqlserver";
                        }
                        paramDB[lastIndex - 1].Descr        = TxbDescr.Text;
                        paramDB[lastIndex - 1].Host         = TxbHost.Text;
                        paramDB[lastIndex - 1].Port         = TxbPort.Text;
                        paramDB[lastIndex - 1].ServiceName  = TxbServiceName.Text;
                        paramDB[lastIndex - 1].User         = TxbUser.Text;
                        paramDB[lastIndex - 1].Password     = AESThenHMAC.SimpleEncryptWithPassword(PwbSenha.Password, proc.GetMacAdress());
                        paramDB[lastIndex - 1].Trusted_Conn = "True";
                        paramDB[lastIndex - 1].Token        = TxbToken.Text;
                        try
                        {
                            paramDB[lastIndex - 1].Grupo = Convert.ToInt64(TxbGrupo.Text);
                            salvaDB(paramDB[lastIndex - 1]);
                        }
                        catch (Exception error)
                        {
                            System.Windows.MessageBox.Show("Campo Grupo deve ser numerico", "Atenção", MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }
                    }
                    else
                    {
                        if (CboDriver.SelectedIndex == 0)
                        {
                            paramdbNew.Driver = "oracle";
                        }
                        else
                        {
                            paramdbNew.Driver = "sqlserver";
                        }
                        paramdbNew.Descr        = TxbDescr.Text;
                        paramdbNew.Host         = TxbHost.Text;
                        paramdbNew.Port         = TxbPort.Text;
                        paramdbNew.ServiceName  = TxbServiceName.Text;
                        paramdbNew.User         = TxbUser.Text;
                        paramdbNew.Password     = AESThenHMAC.SimpleEncryptWithPassword(PwbSenha.Password, proc.GetMacAdress());
                        paramdbNew.Trusted_Conn = "True";
                        paramdbNew.Token        = TxbToken.Text;
                        try
                        {
                            paramdbNew.Grupo = Convert.ToInt64(TxbGrupo.Text);
                            salvaDB(paramdbNew);
                        }
                        catch (Exception error)
                        {
                            System.Windows.MessageBox.Show("Campo Grupo deve ser numerico", "Atenção", MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }
                    }

                    cleanScreen();
                    lastIndex = -1;
                }
                else if (proc.VerificaConexaoBanco(TxbHost.Text, TxbPort.Text, TxbServiceName.Text, TxbUser.Text, PwbSenha.Password, (string)CboDriver.SelectedItem, "False") == true)
                {
                    if (lastIndex != 0)
                    {
                        if (CboDriver.SelectedIndex == 0)
                        {
                            paramDB[lastIndex - 1].Driver = "oracle";
                        }
                        else
                        {
                            paramDB[lastIndex - 1].Driver = "sqlserver";
                        }
                        paramDB[lastIndex - 1].Descr        = TxbDescr.Text;
                        paramDB[lastIndex - 1].Host         = TxbHost.Text;
                        paramDB[lastIndex - 1].Port         = TxbPort.Text;
                        paramDB[lastIndex - 1].ServiceName  = TxbServiceName.Text;
                        paramDB[lastIndex - 1].User         = TxbUser.Text;
                        paramDB[lastIndex - 1].Password     = AESThenHMAC.SimpleEncryptWithPassword(PwbSenha.Password, proc.GetMacAdress());
                        paramDB[lastIndex - 1].Trusted_Conn = "False";
                        paramDB[lastIndex - 1].Token        = TxbToken.Text;
                        try
                        {
                            paramDB[lastIndex - 1].Grupo = Convert.ToInt64(TxbGrupo.Text);
                            salvaDB(paramDB[lastIndex - 1]);
                        }
                        catch (Exception error)
                        {
                            System.Windows.MessageBox.Show("Campo Grupo deve ser numerico", "Atenção", MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }
                    }
                    else
                    {
                        if (CboDriver.SelectedIndex == 0)
                        {
                            paramdbNew.Driver = "oracle";
                        }
                        else
                        {
                            paramdbNew.Driver = "sqlserver";
                        }
                        paramdbNew.Descr        = TxbDescr.Text;
                        paramdbNew.Host         = TxbHost.Text;
                        paramdbNew.Port         = TxbPort.Text;
                        paramdbNew.ServiceName  = TxbServiceName.Text;
                        paramdbNew.User         = TxbUser.Text;
                        paramdbNew.Password     = AESThenHMAC.SimpleEncryptWithPassword(PwbSenha.Password, proc.GetMacAdress());
                        paramdbNew.Trusted_Conn = "False";
                        StaticParametersDB.SetListBanco(paramdbNew);
                        paramdbNew.Token = TxbToken.Text;
                        try
                        {
                            paramdbNew.Grupo = Convert.ToInt64(TxbGrupo.Text);
                            salvaDB(paramdbNew);
                        }
                        catch (Exception error)
                        {
                            System.Windows.MessageBox.Show("Campo Grupo deve ser numerico", "Atenção", MessageBoxButton.OK, MessageBoxImage.Warning);
                            return;
                        }
                    }

                    cleanScreen();
                    lastIndex = -1;
                }
            }
            else
            {
                System.Windows.MessageBox.Show("Preencha todos os campos antes de continuar", "Atenção", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }
        private void init()
        {
            init2();
            BtnDelete.Visibility = Visibility.Hidden;
            CboDriver.Items.Insert(0, "Oracle");
            CboDriver.Items.Insert(1, "SQLServer");

            CboDriver.SelectedIndex = -1;
            var sessao      = AuxiliarNhibernate.AbrirSessao();
            var ParametroDB = new ParametroDB_DAO(sessao);

            try
            {
                var process = new Processos();

                var param = ParametroDB.BuscarPorID(1);//Armazenamento.GetParametrosDB();

                try
                {
                    if (param.Driver == "oracle")
                    {
                        CboDriver.SelectedIndex = 0;
                    }
                    else
                    {
                        CboDriver.SelectedIndex = 1;
                    }
                    TxbHost.Text        = param.Host;
                    TxbPort.Text        = param.Port;
                    TxbServiceName.Text = param.ServiceName;
                    TxbUser.Text        = param.User;


                    StaticParametersDB.SetDriver("oracle");
                    StaticParametersDB.SetHost(TxbHost.Text);
                    StaticParametersDB.SetPort(TxbPort.Text);
                    StaticParametersDB.SetServiceName(TxbServiceName.Text);
                    StaticParametersDB.SetUser(TxbUser.Text);

                    PwbSenha.Password = AESThenHMAC.SimpleDecryptWithPassword(param.Password, process.GetMacAdress());
                    StaticParametersDB.SetPassword(PwbSenha.Password);

                    BtnDelete.Visibility = Visibility.Visible;
                }
                catch (Exception e)
                {
                    if (StaticParametersDB.GetDriver() != null)
                    {
                        var driver = StaticParametersDB.GetDriver();
                        if (driver == "oracle")
                        {
                            CboDriver.SelectedIndex = 0;
                        }
                        else
                        {
                            CboDriver.SelectedIndex = 1;
                        }
                        TxbHost.Text        = StaticParametersDB.GetHost();
                        TxbPort.Text        = StaticParametersDB.GetPort();
                        TxbServiceName.Text = StaticParametersDB.GetServiceName();
                        TxbUser.Text        = StaticParametersDB.GetUser();
                        PwbSenha.Password   = StaticParametersDB.GetPassword();

                        BtnDelete.Visibility = Visibility.Visible;
                    }
                }
            }catch (Exception e)
            {
            }

            sessao.Close();
        }
Example #8
0
        public static void Main(string[] args)
        {
            byte[] key256 = new byte[32];
            for (int i = 0; i < 32; i++)
            {
                key256[i] = Convert.ToByte(i % 256);
            }

            string message  = "Hello World";
            string password = "******";

            byte[] nonSecretOrg = Encoding.UTF8.GetBytes("Pay Bob Zero Dollars");
            byte[] nonSecretMod = Encoding.UTF8.GetBytes("Pay Bob $ 1,000,000.");

            // Encrypt with associated data
            //string encrypted = AESGCM.SimpleEncrypt(message, key256, nonSecretOrg);
            string encrypted = AESThenHMAC.SimpleEncryptWithPassword(message, password, nonSecretOrg);

            Console.WriteLine("AESThenHMAC Encrypted: {0}", encrypted);

            // Decrypt with original associated data
            //string decrypted = AESGCM.SimpleDecrypt(encrypted, key256, nonSecretOrg.Length);
            string decrypted = AESThenHMAC.SimpleDecryptWithPassword(encrypted, password, nonSecretOrg.Length);

            Console.WriteLine("AESThenHMAC Decrypted: {0}", decrypted);
            //Console.WriteLine("Auth cleartext: {0}", Encoding.UTF8.GetString(nonSecretOrg));

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();


            var secret = AESCBC.EncryptString(password, message);

            //Console.WriteLine("AESCBC Encrypted: {0}", BitConverter.ToString(secret));
            Console.WriteLine("AESCBC Encrypted: {0}", Convert.ToBase64String(secret));

            var recovered = AESCBC.DecryptString(password, secret);

            Console.WriteLine("AESCBC Decrypted: {0}", recovered);

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();

            Rijndael.Inputkey = password;

            encrypted = Rijndael.EncryptRijndael(message, "12345678");
            //Console.WriteLine("AESCBC Encrypted: {0}", BitConverter.ToString(secret));
            Console.WriteLine("Rijndael Encrypted: {0}", encrypted);

            decrypted = Rijndael.DecryptRijndael(encrypted, "12345678");
            Console.WriteLine("Rijndael Decrypted: {0}", decrypted);


            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine();


            string passPhrase         = "Pas5pr@se";        // can be any string
            string saltValue          = "12345678";         //"s@1tValue";        // can be any string
            string hashAlgorithm      = "SHA1";             // can be "MD5"
            int    passwordIterations = 10000;              // can be any number
            string initVector         = "@1B2c3D4e5F6g7H8"; // must be 16 bytes
            int    keySize            = 128;                // 256;                // can be 192 or 128

            string cipherText = RijndaelSimple.Encrypt
                                (
                message,
                passPhrase,
                saltValue,
                hashAlgorithm,
                passwordIterations,
                initVector,
                keySize
                                );

            Console.WriteLine(String.Format("RijndaelSimple Encrypted : {0}", cipherText));

            message = RijndaelSimple.Decrypt
                      (
                cipherText,
                passPhrase,
                saltValue,
                hashAlgorithm,
                passwordIterations,
                initVector,
                keySize
                      );

            Console.WriteLine(String.Format("RijndaelSimple Decrypted : {0}", message));

            Console.ReadLine();
        }