public static void RegisterInConfig(Configuration configFile)
        {
            RijndaelManaged rm = new RijndaelManaged();

            rm.GenerateKey();
            rm.GenerateIV();

            ConfigurationSectionGroup group = configFile.SectionGroups[Constants.DataProvidersSectionGroupName];

            if (group == null)
            {
                group = new ConfigurationSectionGroup();
                configFile.SectionGroups.Add(Constants.DataProvidersSectionGroupName, group);
            }

            try { group.Sections.Remove(SectionName); }
            catch { }

            DataProviderKeysSection section = new DataProviderKeysSection();

            section.IVString  = Strings.SerializeObject(rm.IV);
            section.KeyString = Strings.SerializeObject(rm.Key);

            group.Sections.Add(SectionName, section);

            configFile.Save();
        }
        public static bool IsValid(DataProviderKeysSection section)
        {
            if (section == null || string.IsNullOrEmpty(section.IVString) || string.IsNullOrEmpty(section.KeyString))
            {
                return(false);
            }
            RijndaelManaged rm = new RijndaelManaged();

            rm.GenerateIV();
            if (rm.IV.Length != section.IV.Length)
            {
                return(false);
            }
            rm.GenerateKey();
            if (rm.Key.Length != section.Key.Length)
            {
                return(false);
            }
            return(true);
        }
        public static RijndaelManaged GetFromConfig(Configuration configFile)
        {
            RijndaelManaged           rm    = new RijndaelManaged();
            ConfigurationSectionGroup group = configFile.SectionGroups[Constants.DataProvidersSectionGroupName];

            if (group == null)
            {
                group = new ConfigurationSectionGroup();
                configFile.SectionGroups.Add(Constants.DataProvidersSectionGroupName, group);
                configFile.Save();
            }

            DataProviderKeysSection section = group.Sections[SectionName] as DataProviderKeysSection;

            if (!IsValid(section))
            {
                if (section == null)
                {
                    section = new DataProviderKeysSection();
                    group.Sections.Add(DataProviderKeysSection.SectionName, section);
                }
                rm.GenerateIV();
                rm.GenerateKey();
                section.IVString  = Strings.SerializeObject(rm.IV);
                section.KeyString = Strings.SerializeObject(rm.Key);

                if (!section.SectionInformation.IsProtected)
                {
                    section.SectionInformation.ProtectSection("RsaProtectedConfigurationProvider");
                }

                configFile.Save();
                ConfigurationManager.RefreshSection(SectionName);
            }
            rm.IV  = section.IV;
            rm.Key = section.Key;
            return(rm);
        }
Пример #4
0
 public static byte[] EncryptObject(object o)
 {
     return(EncryptObject(o, DataProviderKeysSection.GetFromConfig()));
 }
Пример #5
0
 public static T DecryptObject <T>(byte[] encryptedData)
 {
     return(DecryptObject <T>(encryptedData, DataProviderKeysSection.GetFromConfig()));
 }