public static string Decrypt(string encryptedText) { StringBuilder stringBuilder = new StringBuilder(); RSACryptoServiceProvider.UseMachineKeyStore = true; using (RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider()) { mojoEncryptionConfiguration config = mojoEncryptionConfiguration.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()); }
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()) { mojoEncryptionConfiguration config = mojoEncryptionConfiguration.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)); }
public object Create(object parent, object configContext, XmlNode node) { mojoEncryptionConfiguration config = new mojoEncryptionConfiguration(); config.LoadValuesFromConfigurationXml(node); return(config); }
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()) { mojoEncryptionConfiguration config = mojoEncryptionConfiguration.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); }
public static string Encrypt(string clearText) { StringBuilder stringBuilder = new System.Text.StringBuilder(); RSACryptoServiceProvider.UseMachineKeyStore = true; using (RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider()) { mojoEncryptionConfiguration config = mojoEncryptionConfiguration.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 mojoEncryptionConfiguration GetConfig() { //return (mojoEncryptionConfiguration)ConfigurationManager.GetSection("system.web/mojoEncryption"); try { if ( (HttpRuntime.Cache["mojoEncryptionConfiguration"] != null) && (HttpRuntime.Cache["mojoEncryptionConfiguration"] is mojoEncryptionConfiguration) ) { return((mojoEncryptionConfiguration)HttpRuntime.Cache["mojoEncryptionConfiguration"]); } mojoEncryptionConfiguration config = new mojoEncryptionConfiguration(); string pathToConfigFile = System.Web.Hosting.HostingEnvironment.MapPath(ConfigHelper.GetStringProperty("mojoCryptoHelperKeyFile", "~/mojoEncryption.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( "mojoEncryptionConfiguration", config, aggregateCacheDependency, DateTime.Now.AddYears(1), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default, null); return((mojoEncryptionConfiguration)HttpRuntime.Cache["mojoEncryptionConfiguration"]); } 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); }
public object Create(object parent, object configContext, XmlNode node) { mojoEncryptionConfiguration config = new mojoEncryptionConfiguration(); config.LoadValuesFromConfigurationXml(node); return config; }
public static mojoEncryptionConfiguration GetConfig() { //return (mojoEncryptionConfiguration)ConfigurationManager.GetSection("system.web/mojoEncryption"); try { if ( (HttpRuntime.Cache["mojoEncryptionConfiguration"] != null) && (HttpRuntime.Cache["mojoEncryptionConfiguration"] is mojoEncryptionConfiguration) ) { return (mojoEncryptionConfiguration)HttpRuntime.Cache["mojoEncryptionConfiguration"]; } mojoEncryptionConfiguration config = new mojoEncryptionConfiguration(); string pathToConfigFile = System.Web.Hosting.HostingEnvironment.MapPath(ConfigHelper.GetStringProperty("mojoCryptoHelperKeyFile", "~/mojoEncryption.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( "mojoEncryptionConfiguration", config, aggregateCacheDependency, DateTime.Now.AddYears(1), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default, null); return (mojoEncryptionConfiguration)HttpRuntime.Cache["mojoEncryptionConfiguration"]; } 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; }