Ejemplo n.º 1
0
        private void MarkEncryptedFile()
        {
            StreamWriter writer = new StreamWriter(iniPath, false);
            string       mark   = "SYSTEM_ENCRYPTION_VALIDATOR" + keyValueSeparator + "ENCRYPTION_OK";

            mark = EncryptionTools.Encrypt(mark, encKey, true);
            writer.WriteLine(mark);
            writer.Close();
        }
Ejemplo n.º 2
0
        public static string GenerateToken(IDictionary <string, string> data, string encryptKey)
        {
            if (data.Keys.IsNullOrEmpty())
            {
                throw new ArgumentNullException("data");
            }

            var token = string.Join(
                ParameterDelimiter.ToString(),
                data.Select(item => $"{item.Key}{ValueDelimiter}{item.Value}"));
            var encryptedToken = EncryptionTools.Encrypt(token, encryptKey);

            return(HttpUtility.UrlEncode(encryptedToken));
        }
Ejemplo n.º 3
0
        public bool WriteEntry(string theKey, string theValue, ref string errorMessage)
        {
            ArrayList iniEntries = new ArrayList();
            Int32     count      = 0;
            string    tmp        = "";

            string[]      parts        = null;
            string        tmpKey       = "";
            string        tmpValue     = "";
            bool          valueWritten = false;
            StringBuilder theEntry     = new StringBuilder();

            // If ini file doesn't exist, create it.
            if (!File.Exists(iniPath))
            {
                try
                {
                    CreateFolders(GetFoldersFromPath(iniPath));
                    FileStream fs = new FileStream(iniPath, FileMode.Create);
                    fs.Close();
                }
                catch (Exception ex)
                {
                    errorMessage = "Could not create ini file: " + iniPath + ". (" + ex.Message + ")";
                    return(false);
                }
            }

            // Get the contents of the ini file, and
            // report an error reading from it, if any.
            if (!ReadAllEntries(ref iniEntries, ref tmp))
            {
                errorMessage = tmp;
                return(false);
            }

            // Delete the original ini file.
            if (!ClearINI(ref errorMessage))
            {
                return(false);
            }

            theEntry.Append(theKey);
            theEntry.Append(keyValueSeparator);
            theEntry.Append(theValue);

            try
            {
                StreamWriter writer = new StreamWriter(iniPath, useEncryption);

                if (iniEntries.Count < 1)
                {
                    tmp = theEntry.ToString();
                    if ((useEncryption))
                    {
                        tmp = EncryptionTools.Encrypt(tmp, encKey, true);
                    }

                    writer.WriteLine(tmp);
                    valueWritten = true;
                }
                else
                {
                    for (count = 0; count <= iniEntries.Count - 1; count++)
                    {
                        tmp      = Convert.ToString(iniEntries[count]);
                        parts    = Regex.Split(tmp, keyValueSeparator);
                        tmpKey   = parts[0];
                        tmpValue = parts[1];

                        // Modify ini file if the key already exists.
                        if (theKey.ToLower().Trim() == tmpKey.ToLower().Trim())
                        {
                            tmp = theEntry.ToString();
                            if ((useEncryption))
                            {
                                tmp = EncryptionTools.Encrypt(tmp, encKey, true);
                            }

                            writer.WriteLine(tmp);
                            valueWritten = true;
                        }
                        else
                        {
                            if ((useEncryption))
                            {
                                tmp = EncryptionTools.Encrypt(tmp, encKey, true);
                            }
                            writer.WriteLine(tmp);
                        }
                    }

                    // Add it to the ini file if the key didn't exist in it already
                    if (!valueWritten)
                    {
                        tmp = theEntry.ToString();
                        if ((useEncryption))
                        {
                            tmp = EncryptionTools.Encrypt(tmp, encKey, true);
                        }

                        writer.WriteLine(tmp);
                        valueWritten = true;
                    }
                }

                writer.Close();
            }
            catch (Exception ex)
            {
                errorMessage = "Error writing ini file.\n\n" + ex.Message;
                return(false);
            }

            return(valueWritten);
        }
Ejemplo n.º 4
0
        public bool RemoveEntry(string theKey, ref string errorMessage)
        {
            ArrayList iniEntries = new ArrayList();
            Int32     count      = 0;
            string    tmp        = "";

            string[] parts        = null;
            string   tmpKey       = "";
            string   tmpValue     = "";
            bool     entryRemoved = false;

            // Get the contents of the ini file, and
            // report an error reading from it, if any.
            if (!ReadAllEntries(ref iniEntries, ref tmp))
            {
                errorMessage = tmp;
                return(false);
            }

            // Delete the original ini file.
            try
            {
                File.Delete(iniPath);
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
                return(false);
            }

            // Wait for it to be deleted...
            while (File.Exists(iniPath))
            {
                PauseWithoutBlockingUI(10);
            }

            try
            {
                StreamWriter writer = new StreamWriter(iniPath, false);

                if (iniEntries.Count < 1)
                {
                    errorMessage = "Entry does not exist.";
                    return(false);
                }
                else
                {
                    for (count = 0; count <= iniEntries.Count - 1; count++)
                    {
                        tmp      = Convert.ToString(iniEntries[count]);
                        parts    = Regex.Split(tmp, keyValueSeparator);
                        tmpKey   = parts[0];
                        tmpValue = parts[1];

                        // If we find the entry,
                        if (theKey.ToLower().Trim() == tmpKey.ToLower().Trim())
                        {
                            // Leave it out.
                            entryRemoved = true;
                        }
                        else
                        {
                            if ((useEncryption))
                            {
                                tmp = EncryptionTools.Encrypt(tmp, encKey, true);
                            }
                            writer.WriteLine(tmp);
                        }
                    }
                }

                writer.Close();
            }
            catch (Exception ex)
            {
                errorMessage = "Error writing ini file.\n\n" + ex.Message;
                return(false);
            }

            if (!entryRemoved)
            {
                errorMessage = "Entry does not exist.";
            }
            return(entryRemoved);
        }