public void RsaEncryptedTextCanBeDecrypted()
        {
            var plainText = "Hello world!";

            var RSAKeys       = DataCryptography.GenerateRsaKeys();
            var encryptedText = DataCryptography.EncryptAESKey(plainText, RSAKeys.Item1);
            var decryptedText = DataCryptography.DecryptAESKey(encryptedText, RSAKeys.Item2);

            Assert.AreEqual(plainText, decryptedText);
        }
        public void SHA512GivesTheSameHashForTheSameInput()
        {
            var plainText = "Hello world!";

            var sha512Hash1 = DataCryptography.SHA512(plainText);
            var sha512Hash2 = DataCryptography.SHA512(plainText);


            Assert.AreEqual(sha512Hash1, sha512Hash2);
        }
Ejemplo n.º 3
0
        protected MySqlConnection CreateConnection()
        {
            string constr     = ConfigurationManager.AppSettings["Constr"];
            string dbpass     = ConfigurationManager.AppSettings["DBPASS"];
            string dbpassPain = DataCryptography.Decrypt(dbpass);

            constr = constr + dbpassPain;
            MySqlConnection con = new MySqlConnection(constr);

            return(con);
        }
Ejemplo n.º 4
0
        public byte[] ViewDatabasesNames()
        {
            string           clientName  = (Thread.CurrentPrincipal.Identity as GenericIdentity).Name.Split(',', ';')[0].Split('=')[1];
            X509Certificate2 certificate = CertManager.GetCertificateFromStorage(StoreName.TrustedPeople, StoreLocation.LocalMachine, clientName);

            string message = "----------------------------------------------------\nDatabases names:\n\n";

            foreach (string databaseName in DbList.Keys)
            {
                message += $"{databaseName}\n";
            }

            return(DataCryptography.EncryptData(certificate, message + "\n----------------------------------------------------\n"));
        }
Ejemplo n.º 5
0
        public byte[] ViewAll(string databaseName)
        {
            string           message     = "----------------------------------------------------\nAll entities:\n\n";
            string           clientName  = (Thread.CurrentPrincipal.Identity as GenericIdentity).Name.Split(',', ';')[0].Split('=')[1];
            X509Certificate2 certificate = CertManager.GetCertificateFromStorage(StoreName.TrustedPeople, StoreLocation.LocalMachine, clientName);

            if (DbList.ContainsKey(databaseName))
            {
                foreach (Information information in DbList[databaseName].Values)
                {
                    message += information.ToString();
                }

                message += "----------------------------------------------------\n";
                return(DataCryptography.EncryptData(certificate, message));
            }
            else
            {
                return(DataCryptography.EncryptData(certificate, $"Database with name '{databaseName}' doesn't exists.\n"));
            }
        }
Ejemplo n.º 6
0
        public byte[] AverageSalaryByCountryAndPayday(string databaseName, string country, string payDay)
        {
            string           clientName  = (Thread.CurrentPrincipal.Identity as GenericIdentity).Name.Split(',', ';')[0].Split('=')[1];
            X509Certificate2 certificate = CertManager.GetCertificateFromStorage(StoreName.TrustedPeople, StoreLocation.LocalMachine, clientName);

            Dictionary <string, List <Information> > informations = new Dictionary <string, List <Information> >();
            string message = "----------------------------------------------------\nAvarage salary by country and payday:\n\n";

            if (DbList.ContainsKey(databaseName))
            {
                foreach (Information information in DbList[databaseName].Values)
                {
                    if (information.Drzava == country.Trim().ToLower() && information.Year == payDay.Trim())
                    {
                        if (informations.ContainsKey(information.Drzava))
                        {
                            informations[information.Drzava].Add(information);
                        }
                        else
                        {
                            informations.Add(information.Drzava, new List <Information>()
                            {
                                information
                            });
                        }
                    }
                }

                foreach (var pair in informations)
                {
                    message += $"{pair.Key}:\t{pair.Value.Average(x => x.MesecnaPrimanja).ToString()}\n";
                }
            }
            else
            {
                return(DataCryptography.EncryptData(certificate, $"Database with name '{databaseName}' doesn't exists.\n"));
            }

            return(DataCryptography.EncryptData(certificate, message + "\n----------------------------------------------------\n"));
        }
Ejemplo n.º 7
0
        public byte[] ViewMaxPayed(string databaseName)
        {
            string           clientName  = (Thread.CurrentPrincipal.Identity as GenericIdentity).Name.Split(',', ';')[0].Split('=')[1];
            X509Certificate2 certificate = CertManager.GetCertificateFromStorage(StoreName.TrustedPeople, StoreLocation.LocalMachine, clientName);


            //Debugger.Launch();
            Dictionary <string, List <Information> > informations = new Dictionary <string, List <Information> >();
            string message = "----------------------------------------------------\nMax salary from all states:\n\n";

            if (DbList.ContainsKey(databaseName))
            {
                foreach (Information information in DbList[databaseName].Values)
                {
                    if (informations.ContainsKey(information.Drzava))
                    {
                        informations[information.Drzava].Add(information);
                    }
                    else
                    {
                        informations.Add(information.Drzava, new List <Information>()
                        {
                            information
                        });
                    }
                }

                foreach (var pair in informations)
                {
                    message += $"{pair.Key}:\t{pair.Value.Max(x => x.MesecnaPrimanja).ToString()}\n";
                }
            }
            else
            {
                return(DataCryptography.EncryptData(certificate, $"Database with name '{databaseName}' doesn't exists.\n"));
            }

            return(DataCryptography.EncryptData(certificate, message + "\n----------------------------------------------------\n"));
        }
Ejemplo n.º 8
0
        private static void SelectOption(WCFClient proxy, string option)
        {
            //Debugger.Launch();
            string databaseName        = String.Empty;
            string returnedValueString = String.Empty;
            string city    = String.Empty;
            string country = String.Empty;
            string payday  = String.Empty;
            string temp    = String.Empty;
            string message = String.Empty;

            byte[] signature;
            short  fromAge;
            short  toAge;

            if (option != "9")
            {
                Console.Write("\nEnter database name: ");
                databaseName = Console.ReadLine();
            }

            switch (option)
            {
            case "1":
                returnedValueString = proxy.CreateDatabase(databaseName);
                Console.WriteLine(Environment.NewLine + returnedValueString);

                break;

            case "2":
                returnedValueString = proxy.DeleteDatabase(databaseName);
                Console.WriteLine(Environment.NewLine + returnedValueString);

                break;

            case "3":
                message = CreateMessage(databaseName, "Insert");

                signature = DigitalSignature.Create(message, proxy.Credentials.ClientCertificate.Certificate);

                returnedValueString = proxy.Insert(message, signature);
                Console.WriteLine(Environment.NewLine + returnedValueString);

                break;

            case "4":
                message = CreateMessage(databaseName, "Edit");

                signature = DigitalSignature.Create(message, proxy.Credentials.ClientCertificate.Certificate);

                returnedValueString = proxy.Edit(message, signature);
                Console.WriteLine(Environment.NewLine + returnedValueString);

                break;

            case "5":
                returnedValueString = DataCryptography.DecryptData(proxy.Credentials.ClientCertificate.Certificate, proxy.ViewAll(databaseName));
                Console.WriteLine(Environment.NewLine + returnedValueString);

                break;

            case "6":
                returnedValueString = DataCryptography.DecryptData(proxy.Credentials.ClientCertificate.Certificate, proxy.ViewMaxPayed(databaseName));
                Console.WriteLine(Environment.NewLine + returnedValueString);

                break;

            case "7":
                Console.Write("Country: ");
                country = Console.ReadLine();

                do
                {
                    Console.Write("Payday: ");
                    payday = Console.ReadLine();
                } while (!Int32.TryParse(payday, out int id));

                returnedValueString = DataCryptography.DecryptData(proxy.Credentials.ClientCertificate.Certificate, proxy.AverageSalaryByCountryAndPayday(databaseName, country, payday));

                //returnedValueString = proxy.AverageSalaryByCountryAndPayday(databaseName, country, payday);
                Console.WriteLine(Environment.NewLine + returnedValueString);

                break;

            case "8":
                Console.Write("City: ");
                city = Console.ReadLine();

                do
                {
                    do
                    {
                        Console.Write("From age: ");
                        temp = Console.ReadLine();
                    } while (!short.TryParse(temp, out fromAge));

                    do
                    {
                        Console.Write("To age: ");
                        temp = Console.ReadLine();
                    } while (!short.TryParse(temp, out toAge));
                } while (fromAge > toAge);

                returnedValueString = DataCryptography.DecryptData(proxy.Credentials.ClientCertificate.Certificate, proxy.AverageSalaryByCityAndAge(databaseName, city, fromAge, toAge));
                Console.WriteLine(Environment.NewLine + returnedValueString);

                break;

            case "9":
                returnedValueString = DataCryptography.DecryptData(proxy.Credentials.ClientCertificate.Certificate, proxy.ViewDatabasesNames());
                Console.WriteLine(Environment.NewLine + returnedValueString);

                break;

            case "10":
                Console.WriteLine("Exit");
                break;

            default:
                Console.WriteLine("Unknown command");
                break;
            }
        }
Ejemplo n.º 9
0
        public string SaveMember(MemberListDto model, string action)
        {
            string result = "OK";

            try
            {
                conn = CreateConnection();
                MySqlCommand cmd = new MySqlCommand("PD001_SAVE_MEMBERS", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                MySqlParameterCollection param = cmd.Parameters;
                param.Clear();
                AddSQLParam(param, "@id", Util.NVLInt(model.id));
                AddSQLParam(param, "@username", Util.NVLString(model.username));
                //AddSQLParam(param, "@password", Util.NVLString(model.password));
                AddSQLParam(param, "@password", action == "del" ? "" : Util.NVLString(DataCryptography.Encrypt(model.password)));
                AddSQLParam(param, "@fullname", Util.NVLString(model.fullname));
                AddSQLParam(param, "@nickname", Util.NVLString(model.nickname));
                AddSQLParam(param, "@member_level", Util.NVLString(model.level));
                AddSQLParam(param, "@point", Util.NVLInt(model.point));
                AddSQLParam(param, "@email", Util.NVLString(model.email));
                AddSQLParam(param, "@homepage", Util.NVLString(model.homepage));
                AddSQLParam(param, "@telephone", Util.NVLString(model.telephone));
                AddSQLParam(param, "@mobile", Util.NVLString(model.mobile));
                AddSQLParam(param, "@certify_case", Util.NVLString(model.certify_case));
                AddSQLParam(param, "@certify", Util.NVLString(model.certify));
                AddSQLParam(param, "@address", Util.NVLString(model.address));
                AddSQLParam(param, "@address1", Util.NVLString(model.address1));
                AddSQLParam(param, "@address2", Util.NVLString(model.address2));
                AddSQLParam(param, "@address3", Util.NVLString(model.address3));
                AddSQLParam(param, "@icon", Util.NVLString(model.icon));
                AddSQLParam(param, "@img", Util.NVLString(model.img));
                AddSQLParam(param, "@mailling", Util.NVLString(model.mailling));
                AddSQLParam(param, "@sms", Util.NVLString(model.sms));
                AddSQLParam(param, "@member_open", Util.NVLString(model.open));
                AddSQLParam(param, "@signature", Util.NVLString(model.signature));
                AddSQLParam(param, "@profile", Util.NVLString(model.profile));
                AddSQLParam(param, "@memo", Util.NVLString(model.memo));
                AddSQLParam(param, "@adviser", Util.NVLString(model.adviser));
                AddSQLParam(param, "@leave_date", Util.NVLString(model.leave_date));
                AddSQLParam(param, "@intercept_date", Util.NVLString(model.intercept_date));
                AddSQLParam(param, "@txt1", Util.NVLString(model.txt1));
                AddSQLParam(param, "@txt2", Util.NVLString(model.txt2));
                AddSQLParam(param, "@txt3", Util.NVLString(model.txt3));
                AddSQLParam(param, "@txt4", Util.NVLString(model.txt4));
                AddSQLParam(param, "@txt5", Util.NVLString(model.txt5));
                AddSQLParam(param, "@txt6", Util.NVLString(model.txt6));
                AddSQLParam(param, "@txt7", Util.NVLString(model.txt7));
                AddSQLParam(param, "@txt8", Util.NVLString(model.txt8));
                AddSQLParam(param, "@txt9", Util.NVLString(model.txt9));
                AddSQLParam(param, "@txt10", Util.NVLString(model.txt10));
                AddSQLParam(param, "@member_id", Util.NVLInt(1));
                AddSQLParam(param, "@active", Util.NVLInt(model.active));
                AddSQLParam(param, "@status", action);

                conn.Open();
                MySqlDataReader read = cmd.ExecuteReader();
                while (read.Read())
                {
                    result = read.GetString(0).ToString();
                }
                conn.Close();
            }
            catch (Exception e)
            {
                result = e.Message.ToString();
            }
            return(result);
        }