Exemplo n.º 1
0
        public static string EncryptString(string s, string password)
        {
            RijndaelManaged rd   = new RijndaelManaged();
            string          @out = "";

            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

            byte[] key = md5.ComputeHash(Encoding.UTF8.GetBytes(StringObfuscation.DeObfuscate(password)));

            md5.Clear();
            rd.Key = key;
            rd.GenerateIV();

            byte[]       iv = rd.IV;
            MemoryStream ms = new MemoryStream();

            ms.Write(iv, 0, iv.Length);

            CryptoStream cs = new CryptoStream(ms, rd.CreateEncryptor(), CryptoStreamMode.Write);

            byte[] data = System.Text.Encoding.UTF8.GetBytes(s);

            cs.Write(data, 0, data.Length);
            cs.FlushFinalBlock();

            byte[] encdata = ms.ToArray();
            @out = Convert.ToBase64String(encdata);
            cs.Close();
            rd.Clear();

            return(@out);
        }
Exemplo n.º 2
0
        public static string DecryptString(string s, string password)
        {
            RijndaelManaged          rd = new RijndaelManaged();
            int                      rijndaelIvLength = 16;
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();

            byte[] key = md5.ComputeHash(Encoding.UTF8.GetBytes(StringObfuscation.DeObfuscate(password)));

            md5.Clear();

            byte[]       encdata = Convert.FromBase64String(s);
            MemoryStream ms      = new MemoryStream(encdata);

            byte[] iv = new byte[16];

            ms.Read(iv, 0, rijndaelIvLength);
            rd.IV  = iv;
            rd.Key = key;

            CryptoStream cs = new CryptoStream(ms, rd.CreateDecryptor(), CryptoStreamMode.Read);

            byte[] data = new byte[Convert.ToInt32(ms.Length - rijndaelIvLength) + 1];
            int    i    = cs.Read(data, 0, data.Length);

            cs.Close();
            rd.Clear();
            return(System.Text.Encoding.UTF8.GetString(data, 0, i));
        }
        private static bool FilesValid()
        {
            long measuredSize = 0;

            List <string> files = new List <string>();

            string[] paths      = { "Content", "maps", "Scripts" };
            string[] includeExt = { ".dat", ".poke", ".lua", ".trainer" };

            // TODO
            if (Runvalidation)
            {
                Logger.Log(Logger.LogTypes.Debug, "FileValidation.vb: WARNING! FILE VALIDATION IS RUNNING!");
                foreach (string subFolder in paths)
                {
                    foreach (string file in System.IO.Directory.GetFiles(GameController.GamePath + "\\" + subFolder, "*.*", SearchOption.AllDirectories))
                    {
                        if (file.Contains("\\Content\\Localization\\") == false)
                        {
                            string ext = System.IO.Path.GetExtension(file);
                            if (includeExt.Contains(ext.ToLower()))
                            {
                                files.Add(file.Remove(0, GameController.GamePath.Length + 1));
                            }
                        }
                    }
                }

                string s = "";
                foreach (string f in files)
                {
                    long   i    = new System.IO.FileInfo(GameController.GamePath + "\\" + f).Length;
                    string hash = GetMd5FromFile(GameController.GamePath + "\\" + f);

                    FileDictionary.Add((GameController.GamePath + "\\" + f).ToLower(), new ValidationStorage(i, hash));
                    measuredSize += i;

                    if (!string.IsNullOrEmpty(s))
                    {
                        s += ",";
                    }
                    s += f + ":" + hash;
                }

                System.IO.File.WriteAllText(GameController.GamePath + "\\meta", s);
                Logger.Log(Logger.LogTypes.Debug, "FileValidation.vb: Meta created! Expected Size: " + measuredSize + "|MetaHash: " + StringObfuscation.Obfuscate(GetMd5FromFile(GameController.GamePath + "\\meta")));

                // TODO
                //Core.GameInstance.Exit();
            }
            else
            {
                if (System.IO.File.Exists(GameController.GamePath + "\\meta") == true)
                {
                    if (GetMd5FromFile(GameController.GamePath + "\\meta") == StringObfuscation.DeObfuscate(Metahash))
                    {
                        files = System.IO.File.ReadAllText(GameController.GamePath + "\\meta").Split(Convert.ToChar(",")).ToList();
                        Logger.Debug("Meta loaded. Files to check: " + files.Count);
                    }
                    else
                    {
                        Logger.Log(Logger.LogTypes.Warning, "FileValidation.vb: Failed to load Meta (Hash incorrect)! File Validation will fail!");
                    }
                }
                else
                {
                    Logger.Log(Logger.LogTypes.Warning, "FileValidation.vb: Failed to load Meta (File not found)! File Validation will fail!");
                }

                foreach (string f in files)
                {
                    string fileName = f.Split(Convert.ToChar(":"))[0];
                    string fileHash = f.Split(Convert.ToChar(":"))[1];

                    if (System.IO.File.Exists(GameController.GamePath + "\\" + fileName))
                    {
                        long i = new System.IO.FileInfo(GameController.GamePath + "\\" + fileName).Length;
                        FileDictionary.Add((GameController.GamePath + "\\" + fileName).ToLower(), new ValidationStorage(i, fileHash));
                        measuredSize += i;
                    }
                }
            }

            if (measuredSize == Expectedsize)
            {
                return(true);
            }
            return(false);
        }