Advertisement
AlFas

Decryption algorithms

Nov 21st, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.74 KB | None | 0 0
  1.         /// <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>
  2.         /// <param name="decrypted">The string to return the decrypted gamesave.</param>
  3.         /// <returns>Returns true if the gamesave is encrypted; otherwise false.</returns>
  4.         public static bool TryDecryptGamesave(out string decrypted)
  5.         {
  6.             string path = GDLocalData + "\\CCGameManager.dat";
  7.             string gamesave = "";
  8.             bool isEncrypted = CheckIfGamesaveIsEncrypted();
  9.             if (isEncrypted)
  10.                 gamesave = DecryptGamesave();
  11.             else
  12.                 gamesave = Encoding.UTF8.GetString(File.ReadAllBytes(path));
  13.             decrypted = gamesave;
  14.             doneDecryptingGamesave = true;
  15.             return isEncrypted;
  16.         }
  17.         public static bool CheckIfGamesaveIsEncrypted()
  18.         {
  19.             string path = GDLocalData + "\\CCGameManager.dat";
  20.             string gamesave = Encoding.UTF8.GetString(File.ReadAllBytes(path));
  21.             int checks = 0;
  22.             string[] tests = { "<k>bgVolume</k>", "<k>sfxVolume</k>", "<k>playerUDID</k>", "<k>playerName</k>", "<k>playerUserID</k>" };
  23.             for (int i = 0; i < tests.Length; i++)
  24.                 if (gamesave.Contains(tests[i]))
  25.                     checks++;
  26.             return checks != tests.Length;
  27.         }
  28.         public static string DecryptGamesave()
  29.         {
  30.             string path = GDLocalData + "\\CCGameManager.dat"; // Get the path
  31.             string gamesave = GetData(path); // Get the gamesave data
  32.             return GDGamesaveDecrypt(gamesave);
  33.         }
  34.  
  35.         /// <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>
  36.         /// <param name="decrypted">The string to return the decrypted level data.</param>
  37.         /// <returns>Returns true if the level data is encrypted; otherwise false.</returns>
  38.         public static bool TryDecryptLevelData(out string decrypted)
  39.         {
  40.             string path = GDLocalData + "\\CCLocalLevels.dat";
  41.             string levelData = "";
  42.             bool isEncrypted = CheckIfLevelDataIsEncrypted();
  43.             if (isEncrypted)
  44.                 levelData = DecryptLevelData();
  45.             else
  46.                 levelData = Encoding.UTF8.GetString(File.ReadAllBytes(path));
  47.             decrypted = levelData;
  48.             return isEncrypted;
  49.         }
  50.         public static bool CheckIfLevelDataIsEncrypted()
  51.         {
  52.             string path = GDLocalData + "\\CCLocalLevels.dat";
  53.             string levelData = Encoding.UTF8.GetString(File.ReadAllBytes(path));
  54.             string test = "<?xml version=\"1.0\"?><plist version=\"1.0\" gjver=\"2.0\">";
  55.             return !levelData.Contains(test);
  56.         }
  57.         public static string DecryptLevelData()
  58.         {
  59.             string path = GDLocalData + "\\CCLocalLevels.dat"; // Get the path
  60.             string levelData = GetData(path); // Get the gamesave data
  61.             return GDGamesaveDecrypt(levelData);
  62.         }
  63.  
  64.         public static string GetData(string path)
  65.         {
  66.             StreamReader sr = new StreamReader(path);
  67.             string readfile = sr.ReadToEnd();
  68.             sr.Close();
  69.             return readfile;
  70.         }
  71.         public static byte[] Base64Decrypt(string encodedData)
  72.         {
  73.             while (encodedData.Length % 4 != 0)
  74.                 encodedData += "=";
  75.             byte[] encodedDataAsBytes = Convert.FromBase64String(encodedData);
  76.             return encodedDataAsBytes;
  77.         }
  78.         public static string Base64Encrypt(byte[] decryptedData)
  79.         {
  80.             return Convert.ToBase64String(decryptedData);
  81.         }
  82.         public static byte[] Decompress(byte[] data)
  83.         {
  84.             using (var compressedStream = new MemoryStream(data))
  85.             using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress))
  86.             using (var resultStream = new MemoryStream())
  87.             {
  88.                 var buffer = new byte[4096];
  89.                 int read;
  90.  
  91.                 while ((read = zipStream.Read(buffer, 0, buffer.Length)) > 0)
  92.                     resultStream.Write(buffer, 0, read);
  93.  
  94.                 return resultStream.ToArray();
  95.             }
  96.         }
  97.         public static byte[] Compress(byte[] data)
  98.         {
  99.             using (var decompressedStream = new MemoryStream(data))
  100.             using (var zipStream = new GZipStream(decompressedStream, CompressionMode.Compress))
  101.             using (var resultStream = new MemoryStream())
  102.             {
  103.                 var buffer = new byte[4096];
  104.                 int read;
  105.  
  106.                 while ((read = zipStream.Read(buffer, 0, buffer.Length)) > 0)
  107.                     resultStream.Write(buffer, 0, read);
  108.  
  109.                 return resultStream.ToArray();
  110.             }
  111.         }
  112.         public static string XORDecrypt(string text, int key)
  113.         {
  114.             byte[] result = new byte[text.Length];
  115.             for (int c = 0; c < text.Length; c++)
  116.                 result[c] = (byte)((uint)text[c] ^ key);
  117.             string dexored = Encoding.UTF8.GetString(result);
  118.             return dexored;
  119.         }
  120.         public static string XOR11Decrypt(string text)
  121.         {
  122.             byte[] result = new byte[text.Length];
  123.             for (int c = 0; c < text.Length - 1; c++)
  124.                 result[c] = (byte)((uint)text[c] ^ 11);
  125.             string dexored = Encoding.UTF8.GetString(result);
  126.             return dexored;
  127.         }
  128.         public static string GDGamesaveDecrypt(string data)
  129.         {
  130.             string xored = XOR11Decrypt(data); // Decrypt XOR ^ 11
  131.             string replaced = xored.Replace('-', '+').Replace('_', '/').Replace("\0", string.Empty); // Replace characters
  132.             byte[] gzipb64 = Decompress(Base64Decrypt(replaced)); // Decompress
  133.             string result = Encoding.ASCII.GetString(gzipb64); // Change to string
  134.             return result;
  135.         }
  136.         public static string GDLevelStringDecrypt(string ls)
  137.         {
  138.             string replaced = ls.Replace('-', '+').Replace('_', '/').Replace("\0", string.Empty); // Replace characters
  139.             //string fixedBase64 = replaced.FixBase64String();
  140.             byte[] gzipb64 = Base64Decrypt(replaced);
  141.             if (replaced.StartsWith("H4sIAAAAAAAA"))
  142.                 gzipb64 = Decompress(gzipb64); // Decompress
  143.             else throw new ArgumentException("The level string is not valid.");
  144.             string result = Encoding.ASCII.GetString(gzipb64); // Change to string
  145.             return result;
  146.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement