Beispiel #1
0
        public static string Decrypt(string encryptedText)
        {
            StringBuilder stringBuilder = new StringBuilder();

            RSACryptoServiceProvider.UseMachineKeyStore = true;

            using (RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider())
            {
                CEncryptionConfiguration config = CEncryptionConfiguration.GetConfig();
                if (config.RsaKey.Length == 0)
                {
                    log.Error("CryptoHelper.LoadRsaKey failed to load key from config, key was an empty string.");
                    throw new ArgumentException("CryptoHelper.LoadRsaKey failed to load key from config, key was an empty string.");
                }

                rsaProvider.FromXmlString(config.RsaKey);

                byte[] decryptedStr = rsaProvider.Decrypt(StringToByteArray(encryptedText.Trim()), false);

                for (int i = 0; i <= decryptedStr.Length - 1; i++)
                {
                    stringBuilder.Append(Convert.ToChar(decryptedStr[i]));
                }
            }

            return(stringBuilder.ToString());
        }
Beispiel #2
0
        public static string SignAndSecureData(string[] values)
        {
            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.LoadXml("<x></x>");

            for (int i = 0; i < values.Length; i++)
            {
                XmlHelper.AddNode(xmlDoc, "v" + i.ToString(), values[i]);
            }

            RSACryptoServiceProvider.UseMachineKeyStore = true;

            using (RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider())
            {
                CEncryptionConfiguration config = CEncryptionConfiguration.GetConfig();

                if (config.RsaKey.Length == 0)
                {
                    log.Error("CryptoHelper.LoadRsaKey failed to load key from config, key was an empty string.");
                    throw new ArgumentException("CryptoHelper.LoadRsaKey failed to load key from config, key was an empty string.");
                }

                rsaProvider.FromXmlString(config.RsaKey);

                byte[] signature = rsaProvider.SignData(Encoding.ASCII.GetBytes(xmlDoc.InnerXml),
                                                        "SHA1");

                XmlHelper.AddNode(xmlDoc, "s", Convert.ToBase64String(signature, 0, signature.Length));
            }


            return(EncryptRijndaelManaged(xmlDoc.InnerXml));
        }
Beispiel #3
0
        public static bool DecryptAndVerifyData(string input, out string[] values)
        {
            string xml = DecryptRijndaelManaged(input);

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(xml);

            values = null;

            XmlNode node = xmlDoc.GetElementsByTagName("s")[0];

            node.ParentNode.RemoveChild(node);

            byte[] signature = Convert.FromBase64String(node.InnerText);

            byte[] data = Encoding.ASCII.GetBytes(xmlDoc.InnerXml);

            RSACryptoServiceProvider.UseMachineKeyStore = true;

            using (RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider())
            {
                CEncryptionConfiguration config = CEncryptionConfiguration.GetConfig();

                if (config.RsaKey.Length == 0)
                {
                    log.Error("CryptoHelper.LoadRsaKey failed to load key from config, key was an empty string.");
                    throw new ArgumentException("CryptoHelper.LoadRsaKey failed to load key from config, key was an empty string.");
                }

                rsaProvider.FromXmlString(config.RsaKey);
                if (!rsaProvider.VerifyData(data, "SHA1", signature))
                {
                    return(false);
                }
            }

            int count;

            for (count = 0; count < 100; count++)
            {
                if (xmlDoc.GetElementsByTagName("v" + count.ToString())[0] == null)
                {
                    break;
                }
            }

            values = new string[count];

            for (int i = 0; i < count; i++)
            {
                values[i] = xmlDoc.GetElementsByTagName("v" + i.ToString())[0].InnerText;
            }

            return(true);
        }
Beispiel #4
0
        public static string Encrypt(string clearText)
        {
            StringBuilder stringBuilder = new System.Text.StringBuilder();

            RSACryptoServiceProvider.UseMachineKeyStore = true;

            using (RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider())
            {
                CEncryptionConfiguration config = CEncryptionConfiguration.GetConfig();

                if (config.RsaKey.Length == 0)
                {
                    log.Error("CryptoHelper.LoadRsaKey failed to load key from config, key was an empty string.");
                    throw new ArgumentException("CryptoHelper.LoadRsaKey failed to load key from config, key was an empty string.");
                }

                rsaProvider.FromXmlString(config.RsaKey);

                byte[] encryptedStr;
                encryptedStr = rsaProvider.Encrypt(Encoding.ASCII.GetBytes(clearText), false);

                for (int i = 0; i <= encryptedStr.Length - 1; i++)
                {
                    if (i != encryptedStr.Length - 1)
                    {
                        stringBuilder.Append(encryptedStr[i] + "~");
                    }
                    else
                    {
                        stringBuilder.Append(encryptedStr[i]);
                    }
                }
            }

            return(stringBuilder.ToString());
        }
        public static CEncryptionConfiguration GetConfig()
        {
            //return (CEncryptionConfiguration)ConfigurationManager.GetSection("system.web/CEncryption");

            try
            {
                if (
                    (HttpRuntime.Cache["CEncryptionConfiguration"] != null) &&
                    (HttpRuntime.Cache["CEncryptionConfiguration"] is CEncryptionConfiguration)
                    )
                {
                    return((CEncryptionConfiguration)HttpRuntime.Cache["CEncryptionConfiguration"]);
                }

                CEncryptionConfiguration config
                    = new CEncryptionConfiguration();



                string pathToConfigFile
                    = System.Web.Hosting.HostingEnvironment.MapPath("~/CEncryption.config");

                log.Debug("path to crypto key " + pathToConfigFile);

                if (!File.Exists(pathToConfigFile))
                {
                    log.Error("crypto file not found " + pathToConfigFile);
                    return(config);
                }

                FileInfo fileInfo = new FileInfo(pathToConfigFile);

                XmlDocument configXml = new XmlDocument();
                configXml.Load(fileInfo.FullName);
                config.LoadValuesFromConfigurationXml(configXml.DocumentElement);



                AggregateCacheDependency aggregateCacheDependency
                    = new AggregateCacheDependency();


                aggregateCacheDependency.Add(new CacheDependency(pathToConfigFile));

                System.Web.HttpRuntime.Cache.Insert(
                    "CEncryptionConfiguration",
                    config,
                    aggregateCacheDependency,
                    DateTime.Now.AddYears(1),
                    TimeSpan.Zero,
                    System.Web.Caching.CacheItemPriority.Default,
                    null);

                return((CEncryptionConfiguration)HttpRuntime.Cache["CEncryptionConfiguration"]);
            }
            catch (HttpException ex)
            {
                log.Error(ex);
            }
            catch (System.Xml.XmlException ex)
            {
                log.Error(ex);
            }
            catch (ArgumentException ex)
            {
                log.Error(ex);
            }
            catch (NullReferenceException ex)
            {
                log.Error(ex);
            }

            return(null);
        }