Ejemplo n.º 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());
        }
Ejemplo n.º 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));
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 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());
        }