コード例 #1
0
        public void ReturnTheUnencryptedStringWhenDecryptingAnUnencryptedString()
        {
            SimpleEncryptDecrypt sed = new SimpleEncryptDecrypt();

            // Invalid base64 string--it should just return the original
            string unencrypted = "abc1234";
            string d           = sed.Decrypt(unencrypted);

            Assert.Equal(unencrypted, d);

            unencrypted = "ChangeMe";
            d           = sed.Decrypt(unencrypted);
            Assert.Equal(unencrypted, d);
        }
コード例 #2
0
        public void Decrypt()
        {
            IEncryptDecrypt encrypter = new SimpleEncryptDecrypt();

            // keys to make test reproduceable
            byte[] key  = GetConstantKey(32);
            byte[] iv   = GetConstantKey(16);
            var    data = new Tuple <byte[], byte[]>(key, iv);

            mocks.HttpContext.Object.Session.Add(SimpleEncryptDecrypt.EncryptFieldData, data);

            var randomName   = Guid.NewGuid().ToString();
            var randomValue  = Guid.NewGuid().ToString();
            var encryptValue = encrypter.Encrypt(mocks.HttpContext.Object.Session, randomName, randomValue);

            var collection = new NameValueCollection {
                { encryptValue.Item1, encryptValue.Item2 }
            };

            Assert.IsFalse(collection.AllKeys.Contains(randomName));
            Assert.IsTrue(collection.AllKeys.Contains(encryptValue.Item1));
            Assert.AreEqual(encryptValue.Item2, collection[encryptValue.Item1]);

            encrypter.Decrypt(mocks.HttpContext.Object.Session, collection);

            Assert.IsTrue(collection.AllKeys.Contains(randomName));
            Assert.AreEqual(randomValue, collection[randomName]);
            Assert.IsTrue(collection.AllKeys.Contains(encryptValue.Item1));
            Assert.AreEqual(encryptValue.Item2, collection[encryptValue.Item1]);
        }
コード例 #3
0
ファイル: Program.cs プロジェクト: gaybro8777/common
        static void EncryptOrDecrypt(string str, bool encrypt, bool verbose)
        {
            SimpleEncryptDecrypt sed = new SimpleEncryptDecrypt();

            ConsoleLogger   logger = null;
            Action <string> output = null;

            if (verbose)
            {
                logger = new ConsoleLogger(typeof(Program), LogManager.LogLevel.Debug);
                output = (x) => logger.Debug(x);
            }

            string s = null;

            if (encrypt)
            {
                s = sed.Encrypt(str, output);
            }
            else
            {
                s = sed.Decrypt(str, output);
            }

            Console.WriteLine(str + " -> " + s);
        }
コード例 #4
0
ファイル: Configuration.cs プロジェクト: gaybro8777/common
        public override string GetValue(SQLiteConnection conn)
        {
            string value             = base.GetValue(conn);
            SimpleEncryptDecrypt sed = new SimpleEncryptDecrypt();

            return(sed.Decrypt(value));
        }
コード例 #5
0
        public void ProperlyEncryptAndDecryptStringsOfDifferentSizes(string plain)
        {
            SimpleEncryptDecrypt sed = new SimpleEncryptDecrypt();
            string e = sed.Encrypt(plain);

            Assert.NotEqual(plain, e);
            string d = sed.Decrypt(e);

            Assert.Equal(plain, d);
        }
コード例 #6
0
        public void ProperlyEncryptThenDecryptToTheSameString()
        {
            SimpleEncryptDecrypt sed = new SimpleEncryptDecrypt();

            string t1        = "abcd1234";
            string t1_cypher = sed.Encrypt(t1);

            Assert.NotEqual(t1, t1_cypher);
            string t1_plain = sed.Decrypt(t1_cypher);

            Assert.Equal(t1, t1_plain);
        }
コード例 #7
0
        public void ProperlyEncryptThenDecryptGUIDs()
        {
            SimpleEncryptDecrypt sed = new SimpleEncryptDecrypt();

            for (int i = 0; i < 100; ++i)
            {
                string guid   = Guid.NewGuid().ToString();
                string guid_e = sed.Encrypt(guid);
                Assert.NotEqual(guid, guid_e);
                string guid_d = sed.Decrypt(guid_e);
                Assert.Equal(guid, guid_d);
            }
        }
コード例 #8
0
        public static SystemConfiguration Get(bool obfuscate, SQLiteConnection conn)
        {
            SystemConfiguration config = new SystemConfiguration();
            string sql = string.Empty;
            ILog   log = LogManager.GetLogger(typeof(SystemConfigurationStore));

            try
            {
                Dictionary <string, string> monitoredDrives = null;
                SimpleEncryptDecrypt        sed             = new SimpleEncryptDecrypt();

                sql = "SELECT ConfigurationID, Path, Value FROM Configuration WHERE IsValid = 1;";
                using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                    using (SQLiteDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            string path  = reader.GetString(1);
                            string value = reader.IsDBNull(2) ? "" : reader.GetString(2);
                            if (path.EndsWith("connection_string", StringComparison.InvariantCultureIgnoreCase))
                            {
                                if (obfuscate)
                                {
                                    value = "*****";
                                }
                                else
                                {
                                    value = sed.Decrypt(value);
                                }
                            }

                            config.configuration[path] = new ConfigurationData()
                            {
                                configID = reader.GetInt32(0),
                                path     = path,
                                value    = value
                            };

                            if (string.Compare(path, "languages", true) == 0)
                            {
                                Languages lang = new Languages();
                                lang.EnableFromDelimitedString(value);
                                foreach (Language l in lang.All)
                                {
                                    config.languages.Add(new LanguageConfiguration()
                                    {
                                        languageCode = l.GetDescription(), language = l.ToString(), isEnabled = lang.IsEnabled(l)
                                    });
                                }
                            }
                        }
                    }

                Attribute attr = new Attribute();
                config.softwareVersion = attr.Get("software.version", conn);
                if (config.softwareVersion == null)
                {
                    config.softwareVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
                }
                monitoredDrives = attr.GetMultiple("all.drives.descriptions", conn);
                List <string> device_names = monitoredDrives.Keys.ToList();
                foreach (string name in device_names)
                {
                    string[] n = name.Split('.');
                    if (n.Length > 0)
                    {
                        monitoredDrives.ChangeKey(name, n[0]);
                    }
                }

                sql = "SELECT GroupID, Name FROM DeviceGroups";
                using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                    using (SQLiteDataReader reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            Group g = new Group()
                            {
                                id = reader.GetInt32(0), name = reader.GetString(1)
                            };
                            config.groups.Add(g);
                        }
                    }

                config.devices = new Database().GetDevices(conn);

                if (monitoredDrives != null)
                {
                    foreach (DeviceInfo dev in config.devices)
                    {
                        string drive_info;
                        if (monitoredDrives.TryGetValue(dev.name, out drive_info))
                        {
                            try
                            {
                                dev.driveNames = JsonConvert.DeserializeObject <Dictionary <string, string> >(drive_info);
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                }

                LoadMonitoredDrives(config.devices, conn);

                // Get the most recent data that has been recorded
                sql = "SELECT Timestamp FROM Data ORDER BY Timestamp DESC LIMIT 1;";
                using (SQLiteCommand command = new SQLiteCommand(sql, conn))
                    using (SQLiteDataReader reader = command.ExecuteReader())
                    {
                        if (reader.Read())
                        {
                            try
                            {
                                DateTimeOffset ts = DateTimeOffset.Parse(reader.GetString(0));
                                config.mostRecentData = ts;
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
            }
            catch (Exception ex)
            {
                log.Error("GetSystemConfiguration: " + sql);
                log.Error(ex);
            }

            log.Debug("Configuration: \n" + JsonConvert.SerializeObject(config.devices, Formatting.Indented));

            return(config);
        }