Esempio n. 1
0
        /// <summary>
        /// function compares two files to check whether they are the same
        /// <para>Returns true if the files are the same</para>
        /// </summary>
        /// <param name="fileOne">First file to check</param>
        /// <param name="compareFile">File to compare with</param>
        /// <returns>returns true if the </returns>
        static public bool CompareFile(string fileOne, string compareFile)
        {
            bool areEqual = false;

            if (CheckFile.checkExtention(fileOne, compareFile))
            {
                var fileOneBytes = readFile(fileOne);
                var fileTwoBytes = readFile(compareFile);
                if (fileOneBytes.Length == fileTwoBytes.Length)
                {
                    int pos = 0;
                    if (fileOneBytes.Length == fileTwoBytes.Length)
                    {
                        bool equal = true;
                        while (pos < fileOneBytes.Length && fileTwoBytes[pos] == fileTwoBytes[pos] && equal == true)
                        {
                            equal = checkBits(fileOneBytes[pos], fileTwoBytes[pos]);
                            pos++;
                        }
                        if (pos == fileOneBytes.Length && pos == fileTwoBytes.Length)
                        {
                            areEqual = true;
                        }
                    }
                }
            }
            return(areEqual);
        }
        /// <summary>
        /// Function decrypts the given string
        /// <para> Returns a decrypted string</para>
        /// </summary>
        /// <param name="encryptedString"> the obj to decrypt</param>
        /// <param name="keys"> Obj to store key and seed</param>
        /// <returns>returns a decrypted string or the name of the decrypted file name</returns>
        public string Decrypt(string encryptedString, KeyHolder keys)
        {
            string decryptedObj = "";

            if (!CheckFile.checkHasExtention(encryptedString))
            {
                AesEncryption aes = new AesEncryption();
                aes.Key      = keys.Key;
                aes.Seed     = keys.Seed;
                decryptedObj = aes.Decrypt(encryptedString);
            }
            else
            {
                Decrypt(encryptedString, encryptedString, keys);
            }
            return(decryptedObj);
        }
        /// <summary>
        /// function encrypts the given  str
        /// <para/> function returns an encrypted string
        /// </summary>
        /// <param name="str"> str to encrypt</param>
        /// <param name="keys">place to store encryption keys and Seed</param>
        /// <param name="seed">user defined seed to run encryption on</param>
        /// <returns>returns an encrypted string ro  the name of the file where the file was encrypted</returns>
        public string EncryptStr(string str, ref KeyHolder keys, string seed = null, string key = null)
        {
            if (!CheckFile.checkHasExtention(str))
            {
                int           len = str.Length;
                AesEncryption Aes = new AesEncryption();

                if (seed == null && key == null)
                {
                    str = Aes.Encrypt(str);
                }
                else
                {
                    if (seed != null && key == null)
                    {
                        keys     = SetSeed(seed, ref Aes);
                        Aes.Seed = keys.Seed;
                        str      = Aes.Encrypt(str);
                    }
                    else if (seed == null && key != null)
                    {
                        keys    = SetKey(key, ref Aes);
                        Aes.Key = keys.Key;
                        str     = Aes.Encrypt(str);
                    }
                    else
                    {
                        keys     = SetKey(key, ref Aes);
                        Aes.Key  = keys.Key;
                        keys     = SetSeed(seed, ref Aes);
                        Aes.Seed = keys.Seed;
                        str      = Aes.Encrypt(str);
                    }
                }
                keys = new KeyHolder(Aes.Key, Aes.Seed);
                Aes.FlushKeys();
            }
            return(str);
        }