Advertisement
AlFas

Gamesave decryption code

Apr 29th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.00 KB | None | 0 0
  1. // Necessary usings
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.IO;
  6. using System.IO.Compression;
  7.  
  8. namespace GamesaveDecryption
  9. {
  10.     public static class GamesaveDecryption
  11.     {
  12.         /// <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>
  13.         /// <param name="decrypted">The string to return the decrypted gamesave.</param>
  14.         /// <returns>Returns true if the gamesave is encrypted; otherwise false.</returns>
  15.         public static bool TryDecryptGamesave(out string decrypted)
  16.         {
  17.             string path = GDLocalData + "\\CCGameManager.dat";
  18.             string gamesave = "";
  19.             bool isEncrypted = CheckIfGamesaveIsEncrypted();
  20.             if (isEncrypted)
  21.                 gamesave = DecryptGamesave();
  22.             else
  23.                 gamesave = Encoding.UTF8.GetString(File.ReadAllBytes(path));
  24.             decrypted = gamesave;
  25.             return isEncrypted;
  26.         }
  27.         public static bool CheckIfGamesaveIsEncrypted()
  28.         {
  29.             string path = GDLocalData + "\\CCGameManager.dat";
  30.             string gamesave = Encoding.UTF8.GetString(File.ReadAllBytes(path));
  31.             int checks = 0;
  32.             string[] tests = { "<k>bgVolume</k>", "<k>sfxVolume</k>", "<k>playerUDID</k>", "<k>playerName</k>", "<k>playerUserID</k>" };
  33.             for (int i = 0; i < tests.Length; i++)
  34.                 if (gamesave.Contains(tests[i]))
  35.                     checks++;
  36.             return checks != tests.Length;
  37.         }
  38.         public static string DecryptGamesave()
  39.         {
  40.             string path = GDLocalData + "\\CCGameManager.dat"; // Get the path
  41.             string gamesave = GetData(path); // Get the gamesave data
  42.             return GDGamesaveDecrypt(gamesave);
  43.         }
  44.  
  45.         /// <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>
  46.         /// <param name="decrypted">The string to return the decrypted level data.</param>
  47.         /// <returns>Returns true if the level data is encrypted; otherwise false.</returns>
  48.         public static bool TryDecryptLevelData(out string decrypted)
  49.         {
  50.             string path = GDLocalData + "\\CCLocalLevels.dat";
  51.             string levelData = "";
  52.             bool isEncrypted = CheckIfLevelDataIsEncrypted();
  53.             if (isEncrypted)
  54.                 levelData = DecryptLevelData();
  55.             else
  56.                 levelData = Encoding.UTF8.GetString(File.ReadAllBytes(path));
  57.             decrypted = levelData;
  58.             return isEncrypted;
  59.         }
  60.         public static bool CheckIfLevelDataIsEncrypted()
  61.         {
  62.             string path = GDLocalData + "\\CCLocalLevels.dat";
  63.             string levelData = Encoding.UTF8.GetString(File.ReadAllBytes(path));
  64.             string test = "<?xml version=\"1.0\"?><plist version=\"1.0\" gjver=\"2.0\">";
  65.             return !levelData.Contains(test);
  66.         }
  67.         public static string DecryptLevelData()
  68.         {
  69.             string path = GDLocalData + "\\CCLocalLevels.dat"; // Get the path
  70.             string levelData = GetData(path); // Get the gamesave data
  71.             return GDGamesaveDecrypt(levelData);
  72.         }
  73.  
  74.         /// <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>
  75.         /// <param name="decrypted">The string to return the decrypted level string.</param>
  76.         /// <returns>Returns true if the level string is encrypted; otherwise false.</returns>
  77.         public static bool TryDecryptLevelString(int index, out string decrypted)
  78.         {
  79.             string path = GDLocalData + "\\CCLocalLevels.dat";
  80.             string levelString = "";
  81.             bool isEncrypted = CheckIfLevelStringIsEncrypted(index);
  82.             if (isEncrypted)
  83.                 levelString = DecryptLevelString(index);
  84.             else
  85.                 levelString = GetLevelString(index);
  86.             decrypted = levelString;
  87.             return isEncrypted;
  88.         }
  89.         public static bool CheckIfLevelStringIsEncrypted(int index)
  90.         {
  91.             string levelString = GetLevelString(index);
  92.             int checks = 0;
  93.             string[] tests = { "kA13,", "kA15,", "kA16,", "kA14,", "kA6," };
  94.             for (int i = 0; i < tests.Length; i++)
  95.                 if (levelString.Contains(tests[i]))
  96.                     checks++;
  97.             return checks != tests.Length;
  98.         }
  99.         public static string DecryptLevelString(int index)
  100.         {
  101.             string levelString = GetLevelString(index);
  102.             return GDLevelStringDecrypt(levelString);
  103.         }
  104.  
  105.         public static int GetLevelCount()
  106.         {
  107.             return decryptedLevelData.FindAll("<k>k_").Length;
  108.         }
  109.         public static string GetKeyValue(int index, int key, string valueType)
  110.         {
  111.             string startKeyString = "<k>k" + key.ToString() + "</k><" + valueType + ">";
  112.             string endKeyString = "</" + valueType + ">";
  113.             int lvls = GetLevelCount();
  114.             if (lvls > 0)
  115.             {
  116.                 if (index <= lvls - 1)
  117.                 {
  118.                     int[] startIndices = new int[lvls];
  119.                     for (int i = 0; i < lvls; i++)
  120.                         startIndices[i] = decryptedLevelData.Find("<k>k_" + i.ToString() + "</k>");
  121.                     string[] lvlParameters = new string[lvls];
  122.                     int[] parameterStartIndices = new int[lvls];
  123.                     int[] parameterEndIndices = new int[lvls];
  124.                     int[] parameterLengths = new int[lvls];
  125.                     for (int i = 0; i < lvls; i++)
  126.                     {
  127.                         if (i < lvls - 1)
  128.                         {
  129.                             parameterStartIndices[i] = decryptedLevelData.Find(startKeyString, startIndices[i], startIndices[i + 1]) + startKeyString.Length;
  130.                             parameterEndIndices[i] = decryptedLevelData.Find(endKeyString, parameterStartIndices[i], startIndices[i + 1]);
  131.                         }
  132.                         else
  133.                         {
  134.                             parameterStartIndices[i] = decryptedLevelData.Find(startKeyString, startIndices[i], decryptedLevelData.Length - 1) + startKeyString.Length;
  135.                             parameterEndIndices[i] = decryptedLevelData.Find(endKeyString, parameterStartIndices[i], decryptedLevelData.Length - 1);
  136.                         }
  137.                         parameterLengths[i] = parameterEndIndices[i] - parameterStartIndices[i];
  138.                     }
  139.                     if (parameterStartIndices[index] == startKeyString.Length - 1)
  140.                         throw new KeyNotFoundException();
  141.                     else
  142.                     {
  143.                         for (int i = 0; i < lvls; i++)
  144.                             lvlParameters[i] = decryptedLevelData.Substring(parameterStartIndices[i], parameterLengths[i]);
  145.                         return lvlParameters[index];
  146.                     }
  147.                 }
  148.                 else
  149.                     throw new ArgumentOutOfRangeException();
  150.             }
  151.             else
  152.                 throw new ArgumentNullException();
  153.         }
  154.         public static string GetLevelString(int index)
  155.         {
  156.             return GetKeyValue(index, 4, "s");
  157.         }
  158.  
  159.         public static string GetData(string path)
  160.         {
  161.             StreamReader sr = new StreamReader(path);
  162.             string readfile = sr.ReadToEnd();
  163.             sr.Close();
  164.             return readfile;
  165.         }
  166.         public static byte[] Base64Decrypt(string encodedData)
  167.         {
  168.             while (encodedData.Length % 4 != 0)
  169.                 encodedData += "=";
  170.             byte[] encodedDataAsBytes = Convert.FromBase64String(encodedData);
  171.             return encodedDataAsBytes;
  172.         }
  173.         public static string Base64Encrypt(byte[] decryptedData)
  174.         {
  175.             return Convert.ToBase64String(decryptedData);
  176.         }
  177.         public static byte[] Decompress(byte[] data)
  178.         {
  179.             using (var compressedStream = new MemoryStream(data))
  180.             using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
  181.             using (var resultStream = new MemoryStream())
  182.             {
  183.                 var buffer = new byte[4096];
  184.                 int read;
  185.  
  186.                 while ((read = zipStream.Read(buffer, 0, buffer.Length)) > 0)
  187.                     resultStream.Write(buffer, 0, read);
  188.  
  189.                 return resultStream.ToArray();
  190.             }
  191.         }
  192.         public static byte[] Compress(byte[] data)
  193.         {
  194.             using (var decompressedStream = new MemoryStream(data))
  195.             using (var zipStream = new GZipStream(decompressedStream, CompressionMode.Compress))
  196.             using (var resultStream = new MemoryStream())
  197.             {
  198.                 var buffer = new byte[4096];
  199.                 int read;
  200.  
  201.                 while ((read = zipStream.Read(buffer, 0, buffer.Length)) > 0)
  202.                     resultStream.Write(buffer, 0, read);
  203.  
  204.                 return resultStream.ToArray();
  205.             }
  206.         }
  207.         public static string XORDecrypt(string text, int key)
  208.         {
  209.             byte[] result = new byte[text.Length];
  210.             for (int c = 0; c < text.Length; c++)
  211.                 result[c] = (byte)((uint)text[c] ^ key);
  212.             string dexored = Encoding.UTF8.GetString(result);
  213.             return dexored;
  214.         }
  215.         public static string XOR11Decrypt(string text)
  216.         {
  217.             byte[] result = new byte[text.Length];
  218.             for (int c = 0; c < text.Length - 1; c++)
  219.                 result[c] = (byte)((uint)text[c] ^ 11);
  220.             string dexored = Encoding.UTF8.GetString(result);
  221.             return dexored;
  222.         }
  223.         public static string GDGamesaveDecrypt(string data)
  224.         {
  225.             string xored = XOR11Decrypt(data); // Decrypt XOR ^ 11
  226.             string replaced = xored.Replace('-', '+').Replace('_', '/').Replace("\0", string.Empty); // Replace characters
  227.             byte[] gzipb64 = Decompress(Base64Decrypt(replaced)); // Decompress
  228.             string result = Encoding.ASCII.GetString(gzipb64); // Change to string
  229.             return result;
  230.         }
  231.         public static string GDLevelStringDecrypt(string ls)
  232.         {
  233.             string replaced = ls.Replace('-', '+').Replace('_', '/').Replace("\0", string.Empty); // Replace characters
  234.             byte[] gzipb64 = Decompress(Base64Decrypt(replaced)); // Decompress
  235.             string result = Encoding.ASCII.GetString(gzipb64); // Change to string
  236.             return result;
  237.         }
  238.     }
  239.     public static class Extensions
  240.     {
  241.         public static int Find(this string s, string match)
  242.         {
  243.             for (int i = 0; i <= s.Length - match.Length; i++)
  244.             {
  245.                 string sub = s.Substring(i, match.Length);
  246.                 if (sub == match)
  247.                     return i;
  248.             }
  249.             return -1;
  250.         }
  251.         public static int Find(this string s, string match, int start, int end)
  252.         {
  253.             for (int i = start; i <= end - match.Length; i++)
  254.             {
  255.                 string sub = s.Substring(i, match.Length);
  256.                 if (sub == match)
  257.                     return i;
  258.             }
  259.             return -1;
  260.         }
  261.         public static int[] FindAll(this string s, string match)
  262.         {
  263.             List<int> indices = new List<int>();
  264.             for (int i = 0; i <= s.Length - match.Length; i++)
  265.             {
  266.                 string sub = s.Substring(i, match.Length);
  267.                 if (sub == match)
  268.                     indices.Add(i);
  269.             }
  270.             return indices.ToArray();
  271.         }
  272.         public static int[] FindAll(this string s, string match, int start, int end)
  273.         {
  274.             List<int> indices = new List<int>();
  275.             for (int i = start; i <= end - match.Length; i++)
  276.             {
  277.                 string sub = s.Substring(i, match.Length);
  278.                 if (sub == match)
  279.                     indices.Add(i);
  280.             }
  281.             return indices.ToArray();
  282.         }
  283.         public static string Substring(this string s, string from, string to)
  284.         {
  285.             int startIndex = s.Find(from) + from.Length;
  286.             int endIndex = s.Find(to);
  287.             int length = endIndex - startIndex;
  288.             return s.Substring(startIndex, length);
  289.         }
  290.         public static string Replace(this string originalString, string stringToReplaceWith, int startIndex, int length)
  291.         {
  292.             string result = originalString;
  293.             result = result.Remove(startIndex, length);
  294.             result = result.Insert(startIndex, stringToReplaceWith);
  295.             return result;
  296.         }
  297.         public static string RemoveSpaces(this string s)
  298.         {
  299.             string result = s;
  300.             bool spaces = true;
  301.             while (spaces)
  302.             {
  303.                 if (result[0] == ' ')
  304.                     result = result.Substring(1);
  305.                 else if (result[result.Length - 1] == ' ')
  306.                     result = result.Remove(result.Length - 1);
  307.                 else
  308.                     spaces = false;
  309.             }
  310.             return result;
  311.         }
  312.         public static List<int> BubbleSortList(this List<int> list)
  313.         {
  314.             int temp = 0;
  315.             List<int> l = list;
  316.             for (int i = 0; i < l.Count - 1; i++)
  317.                 for (int j = 0; j < l.Count - i - 1; j++)
  318.                     if (l[j] > l[j + 1])
  319.                     {
  320.                         temp = l[j + 1];
  321.                         l[j + 1] = l[j];
  322.                         l[j] = temp;
  323.                     }
  324.             return l;
  325.         }
  326.     }
  327. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement