예제 #1
0
        private bool UpdateTablesEncryption(string dirPath)
        {
            DirectoryInfo di = new DirectoryInfo(dirPath);

            if (cbxCreateBackup.Checked)
            {
                int    iBackupCount = 1;
                string destDirTmp;
                do
                {
                    destDirTmp = $"{di.FullName}_bak{iBackupCount++}";
                    if (!Directory.Exists(destDirTmp))
                    {
                        Directory.CreateDirectory(destDirTmp);
                        di.CopyFilesRecursively(new DirectoryInfo(destDirTmp));
                        break;
                    }
                } while (Directory.Exists(destDirTmp));
            }

            // Slander.tbl gets encrypted separately from the rest of the tbls, so let's keep it as is, as we don't need to update it.
            var files = di.GetFiles("*.tbl", SearchOption.TopDirectoryOnly)
                        .Where(f => !f.Name.StartsWith("Slander", StringComparison.OrdinalIgnoreCase));

            foreach (var fi in files)
            {
                byte[] data = File.ReadAllBytes(fi.FullName);
                if (IsTableDecrypted(data))
                {
                    Log.Info($"Table {fi.Name} is not encrypted. Updating to new encryption.");
                }
                else
                {
                    FileSecurity.DecryptXOR(data, _previousKeys[0], _previousKeys[1], _previousKeys[2]);
                    if (!IsTableDecrypted(data)) // verify that we managed to decrypt it.
                    {
                        Log.Error($"Failed to decrypt {fi.Name}.");
                        return(false);
                    }
                }

                FileSecurity.EncryptXOR(data, tbxKey1.Key, tbxKey2.Key, tbxKey3.Key);
                File.WriteAllBytes(fi.FullName, data);
            }

            return(true);
        }