Advertisement
Guest User

RK RC4 descrambler adapted from rockbox source

a guest
May 19th, 2014
505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.71 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <fcntl.h>
  3.  
  4. /* scrambling/descrambling reverse engineered by AleMaxx */
  5. static void decode_page(char *inpg, char *outpg, const int size)
  6. {
  7.  
  8. char key[] = {
  9.         0x7C, 0x4E, 0x03, 0x04,
  10.         0x55, 0x05, 0x09, 0x07,
  11.         0x2D, 0x2C, 0x7B, 0x38,
  12.         0x17, 0x0D, 0x17, 0x11
  13. };
  14.         int i, i3, x, val, idx;
  15.  
  16.         char key1[0x100];
  17.         char key2[0x100];
  18.  
  19.         for (i=0; i<0x100; i++) {
  20.                 key1[i] = i;
  21.                 key2[i] = key[i&0xf];
  22.         }
  23.  
  24.         i3 = 0;
  25.         for (i=0; i<0x100; i++) {
  26.                 x = key1[i];
  27.                 i3 = key1[i] + i3;
  28.                 i3 += key2[i];
  29.                 i3 &= 0xff;
  30.                 key1[i] = key1[i3];
  31.                 key1[i3] = x;
  32.         }
  33.  
  34.         idx = 0;
  35.         for (i=0; i<size; i++) {
  36.                 x = key1[(i+1) & 0xff];
  37.                 val = x;
  38.                 idx = (x + idx) & 0xff;
  39.                 key1[(i+1) & 0xff] = key1[idx];
  40.                 key1[idx] = (x & 0xff);
  41.                 val = (key1[(i+1)&0xff] + x) & 0xff;
  42.                 val = key1[val];
  43.                 outpg[i] = val ^ inpg[i];
  44.         }
  45. }
  46.  
  47. int decode_file(int fdin,int fdout)
  48. {
  49.     int rlen,wlen;
  50.     char bu[0x200],buo[0x200];
  51.  
  52.     rlen=lseek(fdin,0,SEEK_END);
  53.     lseek(fdin,0,SEEK_SET);
  54.  
  55.     while(rlen){
  56.         if(rlen>0x200)
  57.             wlen=0x200;
  58.         else
  59.             wlen=rlen;
  60.         read(fdin,bu,wlen);
  61.         decode_page(bu,buo,wlen);
  62.         write(fdout,buo,wlen);
  63.         rlen-=wlen;
  64.     }
  65. }
  66.  
  67. int main(int argc,char *argv[])
  68. {
  69.     int fdin,fdout;
  70.  
  71.     if(argc<2)
  72.         return 1;
  73.  
  74.     fdin=open(argv[1],O_RDONLY);
  75.     fdout=open(argv[2],O_CREAT|O_WRONLY,00664);
  76.  
  77.     decode_file(fdin,fdout);
  78.  
  79.     close(fdin);
  80.     close(fdout);
  81.  
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement