Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Necessary usings
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.IO;
- using System.IO.Compression;
- namespace GamesaveDecryption
- {
- public static class GamesaveDecryption
- {
- /// <summary>Returns the decrypted version of the gamesave after checking whether the gamesave is encrypted or not. Returns true if the gamesave is encrypted; otherwise false.</summary>
- /// <param name="decrypted">The string to return the decrypted gamesave.</param>
- /// <returns>Returns true if the gamesave is encrypted; otherwise false.</returns>
- public static bool TryDecryptGamesave(out string decrypted)
- {
- string path = GDLocalData + "\\CCGameManager.dat";
- string gamesave = "";
- bool isEncrypted = CheckIfGamesaveIsEncrypted();
- if (isEncrypted)
- gamesave = DecryptGamesave();
- else
- gamesave = Encoding.UTF8.GetString(File.ReadAllBytes(path));
- decrypted = gamesave;
- return isEncrypted;
- }
- public static bool CheckIfGamesaveIsEncrypted()
- {
- string path = GDLocalData + "\\CCGameManager.dat";
- string gamesave = Encoding.UTF8.GetString(File.ReadAllBytes(path));
- int checks = 0;
- string[] tests = { "<k>bgVolume</k>", "<k>sfxVolume</k>", "<k>playerUDID</k>", "<k>playerName</k>", "<k>playerUserID</k>" };
- for (int i = 0; i < tests.Length; i++)
- if (gamesave.Contains(tests[i]))
- checks++;
- return checks != tests.Length;
- }
- public static string DecryptGamesave()
- {
- string path = GDLocalData + "\\CCGameManager.dat"; // Get the path
- string gamesave = GetData(path); // Get the gamesave data
- return GDGamesaveDecrypt(gamesave);
- }
- /// <summary>Returns the decrypted version of the level data after checking whether the level data is encrypted or not. Returns true if the level data is encrypted; otherwise false.</summary>
- /// <param name="decrypted">The string to return the decrypted level data.</param>
- /// <returns>Returns true if the level data is encrypted; otherwise false.</returns>
- public static bool TryDecryptLevelData(out string decrypted)
- {
- string path = GDLocalData + "\\CCLocalLevels.dat";
- string levelData = "";
- bool isEncrypted = CheckIfLevelDataIsEncrypted();
- if (isEncrypted)
- levelData = DecryptLevelData();
- else
- levelData = Encoding.UTF8.GetString(File.ReadAllBytes(path));
- decrypted = levelData;
- return isEncrypted;
- }
- public static bool CheckIfLevelDataIsEncrypted()
- {
- string path = GDLocalData + "\\CCLocalLevels.dat";
- string levelData = Encoding.UTF8.GetString(File.ReadAllBytes(path));
- string test = "<?xml version=\"1.0\"?><plist version=\"1.0\" gjver=\"2.0\">";
- return !levelData.Contains(test);
- }
- public static string DecryptLevelData()
- {
- string path = GDLocalData + "\\CCLocalLevels.dat"; // Get the path
- string levelData = GetData(path); // Get the gamesave data
- return GDGamesaveDecrypt(levelData);
- }
- /// <summary>Returns the decrypted version of the level string after checking whether the level string is encrypted or not. Returns true if the level string is encrypted; otherwise false.</summary>
- /// <param name="decrypted">The string to return the decrypted level string.</param>
- /// <returns>Returns true if the level string is encrypted; otherwise false.</returns>
- public static bool TryDecryptLevelString(int index, out string decrypted)
- {
- string path = GDLocalData + "\\CCLocalLevels.dat";
- string levelString = "";
- bool isEncrypted = CheckIfLevelStringIsEncrypted(index);
- if (isEncrypted)
- levelString = DecryptLevelString(index);
- else
- levelString = GetLevelString(index);
- decrypted = levelString;
- return isEncrypted;
- }
- public static bool CheckIfLevelStringIsEncrypted(int index)
- {
- string levelString = GetLevelString(index);
- int checks = 0;
- string[] tests = { "kA13,", "kA15,", "kA16,", "kA14,", "kA6," };
- for (int i = 0; i < tests.Length; i++)
- if (levelString.Contains(tests[i]))
- checks++;
- return checks != tests.Length;
- }
- public static string DecryptLevelString(int index)
- {
- string levelString = GetLevelString(index);
- return GDLevelStringDecrypt(levelString);
- }
- public static int GetLevelCount()
- {
- return decryptedLevelData.FindAll("<k>k_").Length;
- }
- public static string GetKeyValue(int index, int key, string valueType)
- {
- string startKeyString = "<k>k" + key.ToString() + "</k><" + valueType + ">";
- string endKeyString = "</" + valueType + ">";
- int lvls = GetLevelCount();
- if (lvls > 0)
- {
- if (index <= lvls - 1)
- {
- int[] startIndices = new int[lvls];
- for (int i = 0; i < lvls; i++)
- startIndices[i] = decryptedLevelData.Find("<k>k_" + i.ToString() + "</k>");
- string[] lvlParameters = new string[lvls];
- int[] parameterStartIndices = new int[lvls];
- int[] parameterEndIndices = new int[lvls];
- int[] parameterLengths = new int[lvls];
- for (int i = 0; i < lvls; i++)
- {
- if (i < lvls - 1)
- {
- parameterStartIndices[i] = decryptedLevelData.Find(startKeyString, startIndices[i], startIndices[i + 1]) + startKeyString.Length;
- parameterEndIndices[i] = decryptedLevelData.Find(endKeyString, parameterStartIndices[i], startIndices[i + 1]);
- }
- else
- {
- parameterStartIndices[i] = decryptedLevelData.Find(startKeyString, startIndices[i], decryptedLevelData.Length - 1) + startKeyString.Length;
- parameterEndIndices[i] = decryptedLevelData.Find(endKeyString, parameterStartIndices[i], decryptedLevelData.Length - 1);
- }
- parameterLengths[i] = parameterEndIndices[i] - parameterStartIndices[i];
- }
- if (parameterStartIndices[index] == startKeyString.Length - 1)
- throw new KeyNotFoundException();
- else
- {
- for (int i = 0; i < lvls; i++)
- lvlParameters[i] = decryptedLevelData.Substring(parameterStartIndices[i], parameterLengths[i]);
- return lvlParameters[index];
- }
- }
- else
- throw new ArgumentOutOfRangeException();
- }
- else
- throw new ArgumentNullException();
- }
- public static string GetLevelString(int index)
- {
- return GetKeyValue(index, 4, "s");
- }
- public static string GetData(string path)
- {
- StreamReader sr = new StreamReader(path);
- string readfile = sr.ReadToEnd();
- sr.Close();
- return readfile;
- }
- public static byte[] Base64Decrypt(string encodedData)
- {
- while (encodedData.Length % 4 != 0)
- encodedData += "=";
- byte[] encodedDataAsBytes = Convert.FromBase64String(encodedData);
- return encodedDataAsBytes;
- }
- public static string Base64Encrypt(byte[] decryptedData)
- {
- return Convert.ToBase64String(decryptedData);
- }
- public static byte[] Decompress(byte[] data)
- {
- using (var compressedStream = new MemoryStream(data))
- using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
- using (var resultStream = new MemoryStream())
- {
- var buffer = new byte[4096];
- int read;
- while ((read = zipStream.Read(buffer, 0, buffer.Length)) > 0)
- resultStream.Write(buffer, 0, read);
- return resultStream.ToArray();
- }
- }
- public static byte[] Compress(byte[] data)
- {
- using (var decompressedStream = new MemoryStream(data))
- using (var zipStream = new GZipStream(decompressedStream, CompressionMode.Compress))
- using (var resultStream = new MemoryStream())
- {
- var buffer = new byte[4096];
- int read;
- while ((read = zipStream.Read(buffer, 0, buffer.Length)) > 0)
- resultStream.Write(buffer, 0, read);
- return resultStream.ToArray();
- }
- }
- public static string XORDecrypt(string text, int key)
- {
- byte[] result = new byte[text.Length];
- for (int c = 0; c < text.Length; c++)
- result[c] = (byte)((uint)text[c] ^ key);
- string dexored = Encoding.UTF8.GetString(result);
- return dexored;
- }
- public static string XOR11Decrypt(string text)
- {
- byte[] result = new byte[text.Length];
- for (int c = 0; c < text.Length - 1; c++)
- result[c] = (byte)((uint)text[c] ^ 11);
- string dexored = Encoding.UTF8.GetString(result);
- return dexored;
- }
- public static string GDGamesaveDecrypt(string data)
- {
- string xored = XOR11Decrypt(data); // Decrypt XOR ^ 11
- string replaced = xored.Replace('-', '+').Replace('_', '/').Replace("\0", string.Empty); // Replace characters
- byte[] gzipb64 = Decompress(Base64Decrypt(replaced)); // Decompress
- string result = Encoding.ASCII.GetString(gzipb64); // Change to string
- return result;
- }
- public static string GDLevelStringDecrypt(string ls)
- {
- string replaced = ls.Replace('-', '+').Replace('_', '/').Replace("\0", string.Empty); // Replace characters
- byte[] gzipb64 = Decompress(Base64Decrypt(replaced)); // Decompress
- string result = Encoding.ASCII.GetString(gzipb64); // Change to string
- return result;
- }
- }
- public static class Extensions
- {
- public static int Find(this string s, string match)
- {
- for (int i = 0; i <= s.Length - match.Length; i++)
- {
- string sub = s.Substring(i, match.Length);
- if (sub == match)
- return i;
- }
- return -1;
- }
- public static int Find(this string s, string match, int start, int end)
- {
- for (int i = start; i <= end - match.Length; i++)
- {
- string sub = s.Substring(i, match.Length);
- if (sub == match)
- return i;
- }
- return -1;
- }
- public static int[] FindAll(this string s, string match)
- {
- List<int> indices = new List<int>();
- for (int i = 0; i <= s.Length - match.Length; i++)
- {
- string sub = s.Substring(i, match.Length);
- if (sub == match)
- indices.Add(i);
- }
- return indices.ToArray();
- }
- public static int[] FindAll(this string s, string match, int start, int end)
- {
- List<int> indices = new List<int>();
- for (int i = start; i <= end - match.Length; i++)
- {
- string sub = s.Substring(i, match.Length);
- if (sub == match)
- indices.Add(i);
- }
- return indices.ToArray();
- }
- public static string Substring(this string s, string from, string to)
- {
- int startIndex = s.Find(from) + from.Length;
- int endIndex = s.Find(to);
- int length = endIndex - startIndex;
- return s.Substring(startIndex, length);
- }
- public static string Replace(this string originalString, string stringToReplaceWith, int startIndex, int length)
- {
- string result = originalString;
- result = result.Remove(startIndex, length);
- result = result.Insert(startIndex, stringToReplaceWith);
- return result;
- }
- public static string RemoveSpaces(this string s)
- {
- string result = s;
- bool spaces = true;
- while (spaces)
- {
- if (result[0] == ' ')
- result = result.Substring(1);
- else if (result[result.Length - 1] == ' ')
- result = result.Remove(result.Length - 1);
- else
- spaces = false;
- }
- return result;
- }
- public static List<int> BubbleSortList(this List<int> list)
- {
- int temp = 0;
- List<int> l = list;
- for (int i = 0; i < l.Count - 1; i++)
- for (int j = 0; j < l.Count - i - 1; j++)
- if (l[j] > l[j + 1])
- {
- temp = l[j + 1];
- l[j + 1] = l[j];
- l[j] = temp;
- }
- return l;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement