caitsith2

Anti RGSS decryptor algorithm

Jun 23rd, 2014
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.29 KB | None | 0 0
  1. // decryptor.cpp : Defines the entry point for the console application.
  2. //
  3. /*
  4. ** The encryption algorithm used by Anti RGSS Decryptor is pathetic. It leaked way too much info, that made it extremely trivial to
  5. ** black box reverse engineer. :)
  6. **
  7. ** For one, the worst info leaked, was in launcher.dat.  It had plaintext carraige return line feeds, and 2, its encryption
  8. ** was simply add string length plus 3, to each byte of the string.
  9. **
  10. ** Second, since this encryptor did NOT validate the contents of the files to be encrypted, I was able to
  11. ** stuff some controlled values into the "black box", and analyze the output.  This is pretty much an 8-bit block cipher, with no
  12. ** real key expansion whatsoever, other than that there are 3 sboxes, all of which are trivially computed.
  13. */
  14.  
  15.  
  16. #include "stdafx.h"
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20.  
  21. unsigned char sbox[3][256];
  22. unsigned char *password;
  23.  
  24. int decode_string(unsigned char *str, FILE *f, int max_len)
  25. {
  26.     int i,slen=0;
  27.     i=fgetc(f);
  28.     if(i == EOF)
  29.         return i;
  30.     str[slen++]=i;
  31.     str[slen++]=fgetc(f);
  32.     str[slen++]=fgetc(f);
  33.     while (((str[slen-2] != 0x0D) || (str[slen-1] != 0x0A)) && (slen < max_len))
  34.         str[slen++]=fgetc(f);
  35.     slen-=2;
  36.     str[slen]=0;
  37.     for(i=0;i<slen;i++)
  38.         str[i] -= ((slen + 3) & 0xFF);
  39.     return slen;
  40. }
  41.  
  42. int _tmain(int argc, _TCHAR* argv[])
  43. {
  44.     FILE *f, *g;
  45.     unsigned char *filedata;
  46.     char filename[260];
  47.     int flen=0;
  48.    
  49.     int psize=0;
  50.     int plen=0;
  51.  
  52.     int i, j;
  53.     unsigned char low, high;
  54.  
  55.     for(i=0;i<16;i++)
  56.     {
  57.         for(j=0;j<16;j++)
  58.         {
  59.             high = (i+((j<9)?0:1)) % 16;
  60.             high = (8 - (high & 0xC) + (high & 0x3)) << 4;
  61.  
  62.             low = (j-1)%16;
  63.             low = (3 + (low & 0xC) - (low & 0x3));
  64.             sbox[0][(i*16)+j]=high|low;
  65.  
  66.             low += 8;
  67.             low %= 16;
  68.             sbox[2][(i*16)+j]=high|low;
  69.  
  70.             if(low&1)
  71.                 low-=1;
  72.             else
  73.                 low+=1;
  74.             low %= 16;
  75.             sbox[1][(i*16)+j]=high|low;
  76.         }
  77.     }
  78.  
  79.    
  80.  
  81.     f = fopen("Launcher.dat","rb");
  82.     if(f==NULL)
  83.     {
  84.         printf("Failed to open Launcher.dat\n");
  85.         return 1;
  86.     }
  87.  
  88.     fseek(f,0,SEEK_END);
  89.     psize=ftell(f);
  90.     fseek(f,0,SEEK_SET);
  91.  
  92.     password=(unsigned char*)malloc(psize);
  93.     if(password == NULL)
  94.     {
  95.  
  96.         fclose(f);
  97.         return 1;
  98.     }
  99.  
  100.    
  101.     flen=decode_string((unsigned char*)filename,f,260);
  102.     plen=decode_string(password,f,psize);
  103.  
  104.     while(flen != EOF)
  105.     {
  106.         g=fopen(filename,"rb");
  107.         if(g == NULL)
  108.         {
  109.             fclose(f);
  110.             free(password);
  111.             return 1;
  112.         }
  113.  
  114.         fseek(g,0,SEEK_END);
  115.         j=ftell(g);
  116.         fseek(g,0,SEEK_SET);
  117.  
  118.         filedata=(unsigned char*)malloc(j);
  119.         if (filedata == NULL)
  120.         {
  121.             fclose(f);
  122.             fclose(g);
  123.             free(password);
  124.             return 1;
  125.         }
  126.  
  127.         fread(filedata,1,j,g);
  128.         fclose(g);
  129.         g=NULL;
  130.  
  131.         j-=5;
  132.         for(i=0;i<j;i++)
  133.         {
  134.             filedata[i] -= password[i%plen];
  135.             filedata[i] = sbox[i%3][filedata[i]+0x39];
  136.         }
  137.         g=fopen(filename,"wb");
  138.         if(g!=NULL)
  139.         {
  140.             fwrite(filedata,1,j,g);
  141.             fclose(g);
  142.             g=NULL;
  143.         }
  144.         free(filedata);
  145.         filedata=NULL;
  146.        
  147.         sprintf(filename,"Audio\\");
  148.         flen=decode_string((unsigned char*)&filename[6],f,254);
  149.     }
  150.     fclose(f);
  151.     f=NULL;
  152.  
  153.     //Now that we fully decrypted the game, there is no longer a need for launcher.dat or the launcher program.
  154.     unlink("Launcher.dat");
  155.    
  156.     f=fopen("ARD Launcher RC2.exe","rb");
  157.     if(f!=NULL)
  158.     {
  159.         fclose(f);
  160.         unlink("ARD Launcher RC2.exe");
  161.     }
  162.     return 0;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment