sobinist

Rayman 2 Save Encryption Checker

May 14th, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace Rayman2SaveEncryption {
  9.   class Program {
  10.  
  11.     static bool breakTheMold;
  12.     static byte checkByte = 0;
  13.     static uint checkDWord = 0;
  14.     static uint importantSaveThing2 = 0;
  15.     static byte importantSaveThing = 87;
  16.  
  17.     static void Main(string[] args) {
  18.       if (args.Length > 0 || true) {
  19.         //FileStream stream = File.OpenRead(args[0]);
  20.         FileStream stream = File.OpenRead("test.sav");
  21.         read(stream);
  22.         Console.WriteLine("CheckByte now is "+checkByte);
  23.         Console.WriteLine("CheckDWord now is " + checkDWord);
  24.         Console.ReadKey();
  25.       }
  26.       else {
  27.         Console.WriteLine("Enter a filename as the first argument");
  28.         Console.ReadKey();
  29.       }
  30.     }
  31.  
  32.     static int saveDecryptMagic_1(FileStream File) {
  33.       int nextCharInFile; // esi@2
  34.       int result; // eax@4
  35.       byte v4; // ST00_1@5
  36.  
  37.       nextCharInFile = File.ReadByte();
  38.  
  39.       result = nextCharInFile;
  40.       if (nextCharInFile != -1) {
  41.         v4 = importantSaveThing++;
  42.         result = nextCharInFile ^ (byte)fuckUpTheByte(v4) ^ 0xB9;
  43.         checkByte -= (byte)result;
  44.         ++importantSaveThing2;
  45.       }
  46.       return result;
  47.     }
  48.  
  49.     static byte fuckUpTheByte(byte theByte) {
  50.       byte result; // al@1
  51.       byte v2; // dl@1
  52.       byte v3; // esi@1
  53.  
  54.       result = 0;
  55.       v2 = 0;
  56.       v3 = 7;
  57.       do {
  58.         result = (byte)(result | ((theByte >> v2++) & 1) << v3--);
  59.       }
  60.       while (v2 < 8u);
  61.       return result;
  62.     }
  63.  
  64.     static byte __SETS__(int x) {
  65.         return (byte)((x < 0) ? 1 : 0);
  66.     }
  67.  
  68.     static byte __OFSUB__(int x, int y) {
  69.       int y2 = y;
  70.       byte sx = __SETS__(x);
  71.       return (byte)((sx ^ __SETS__(y2)) & (sx ^ __SETS__(x - y2)));
  72.     }
  73.  
  74.     static void decreaseDWordAndCheck() { // Rayman2.exe sub_48D610
  75.       if (--checkDWord < 0) {
  76.         breakTheMold = true;
  77.       }
  78.     }
  79.  
  80.     static void read(FileStream stream) {
  81.       int cheese = -1;
  82.       int v3 = 0;
  83.       int v5 = 0;
  84.       int v6 = 0;
  85.       int v8 = 0;
  86.       int v9 = 0;
  87.       int v10 = 0;
  88.       int someVar;
  89.  
  90.       checkByte = getOneByteAndXorWith53(stream);
  91.       checkDWord = getFourBytesAndXorWith0x54555657(stream);
  92.  
  93.       Console.WriteLine("CheckByte was " + checkByte);
  94.       Console.WriteLine("CheckDWord was " + checkDWord);
  95.  
  96.       if (!breakTheMold) {
  97.         while (true) {
  98.           if (cheese == -1) {
  99.             someVar = saveDecryptMagic_1(stream);
  100.             if (someVar == -1) {
  101.               breakTheMold = true;
  102.             }
  103.             else {
  104.               v3 = saveDecryptMagic_1(stream);
  105.             }
  106.           }
  107.           else {
  108.             v5 = saveDecryptMagic_1(stream);
  109.             someVar = v5;
  110.             if (v5 == -1) {
  111.               v6 = 0;
  112.               do {
  113.                 v6++;
  114.                 decreaseDWordAndCheck();
  115.               } while (v6 < 8);
  116.               breakTheMold = true;
  117.             }
  118.             else {
  119.               v3 = saveDecryptMagic_1(stream);
  120.               if (v3 == -1) {
  121.                 for (int i=0;i<v5; ++i) {
  122.                     decreaseDWordAndCheck();
  123.                 }
  124.                 breakTheMold = true;
  125.               }
  126.               else {
  127.                 v8 = 0;
  128.                 do {
  129.                   v8++;
  130.                   decreaseDWordAndCheck();
  131.                 } while (v8 < 8);
  132.               }
  133.             }
  134.           }
  135.           if (breakTheMold) {
  136.             break;
  137.           }
  138.           v9 = 0;
  139.           do {
  140.             if (v9 > 0) {
  141.               v3 = saveDecryptMagic_1(stream);
  142.             }
  143.             v10 = v9 + 8;
  144.             if ((__OFSUB__(v9, v9 + 8) ^ 1)!=0) {
  145.               do {
  146.                 if ((((ulong)v3 >> 32) ^ (ulong)Math.Abs(v3) & 1) - ((ulong)v3 >> 32) == 1) {
  147.                   saveDecryptMagic_1(stream); // don't store as we don't give a shit
  148.                 }
  149.                
  150.                 ++v9;
  151.                 v3 /= 2;
  152.               }
  153.               while (v9 < v10);
  154.             }
  155.             v9 = v10;
  156.           }
  157.           while (v10 < 8);
  158.           if (breakTheMold) {
  159.             break;
  160.           }
  161.           cheese = someVar;
  162.         }
  163.       }
  164.     }
  165.  
  166.     private static uint getFourBytesAndXorWith0x54555657(FileStream stream) {
  167.       byte[] DstBuf = new byte[4];
  168.       stream.Read(DstBuf, 0, 4);
  169.       int value = BitConverter.ToInt32(DstBuf, 0);
  170.       return (uint)(value ^ 0x54555657);
  171.     }
  172.  
  173.     private static byte getOneByteAndXorWith53(FileStream stream) {
  174.       byte[] DstBuf = new byte[4];
  175.       byte value = (byte)stream.ReadByte();
  176.       return (byte)(value ^ 0x53);
  177.     }
  178.   }
  179. }
Add Comment
Please, Sign In to add comment