Advertisement
Arch1S

Decrypt SWTOR RC4 Packet

Jan 15th, 2012
1,625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.63 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2012 Arch1S
  3.  *
  4.  */
  5.  
  6. #include "SWTORCrypt.h"
  7. #include <openssl/bn.h>
  8. #include <Util.h>
  9.  
  10. RSA *SWTORCrypt::rsaKey;
  11. bool SWTORCrypt::bInitialised = false;
  12. #define STORE32L(x, y)                                                                     \
  13.      { (y)[3] = (unsigned char)(((x)>>24)&255); (y)[2] = (unsigned char)(((x)>>16)&255);   \
  14.        (y)[1] = (unsigned char)(((x)>>8)&255); (y)[0] = (unsigned char)((x)&255); }
  15. #define OUTPUT_BIGNUM(num, buf2, y, z)         \
  16. {                                              \
  17.       z = (unsigned long)BN_num_bytes(num);  \
  18.       STORE32L(z, buf2+y);                     \
  19.       y += 4;                                  \
  20.       (void)BN_bn2bin(num, buf2+y);   \
  21.       y += z;                                  \
  22. }
  23.  
  24. void SWTORCrypt::decryptSWTORRC4Packet(unsigned char *inArray, int inLen, const unsigned char *RC4Key, const int RC4KeyLen)
  25. {
  26.     int x, y, midpoint, pos;
  27.     unsigned char *s, tmp, *tmp_sym_sbox;
  28.     tmp_sym_sbox = (unsigned char*)malloc(RC4KeyLen * sizeof(unsigned char));
  29.     if(tmp_sym_sbox == NULL)
  30.     {
  31.         LOG_FATAL(logger,"decryptSWTORRC4Packet");
  32.     }
  33.     else
  34.     {
  35.         memcpy(tmp_sym_sbox,RC4Key,RC4KeyLen);
  36.         x = 0;
  37.         y = 0;
  38.         s = tmp_sym_sbox;
  39.         midpoint = inLen / 2;
  40.  
  41.         for (pos=midpoint; pos<inLen; pos++)
  42.         {
  43.             x = (x + 1) & 255;
  44.             y = (y + s[x]) & 255;
  45.             tmp = s[x];
  46.             s[x] = s[y];
  47.             s[y] = tmp;
  48.             tmp = (s[x] + s[y]) & 255;
  49.             inArray[pos] ^= s[tmp];
  50.             y = (y + inArray[pos]) & 255;  // this is not standard RC4 here
  51.         }
  52.         for (pos=0; pos<midpoint; pos++)
  53.         {
  54.             x = (x + 1) & 255;
  55.             y = (y + s[x]) & 255;
  56.             tmp = s[x];
  57.             s[x] = s[y];
  58.             s[y] = tmp;
  59.             tmp = (s[x] + s[y]) & 255;
  60.             inArray[pos] ^= s[tmp];
  61.             y = (y + inArray[pos]) & 255;  // this is not standard RC4 here
  62.         }
  63.     }
  64.    
  65.     free(tmp_sym_sbox);
  66. }
  67. void SWTORCrypt::encryptSWTORRC4Packet(unsigned char *inArray, int inLen, const unsigned char *RC4Key, const int RC4KeyLen)
  68. {
  69.     int x, y, midpoint, pos;
  70.     unsigned char *s, tmp, *tmp_sym_sbox;
  71.     tmp_sym_sbox = (unsigned char*)malloc(RC4KeyLen * sizeof(unsigned char));
  72.     if(tmp_sym_sbox == NULL)
  73.     {
  74.         LOG_FATAL(logger,"encryptWARRC4Packet");
  75.     }
  76.     else
  77.     {
  78.         memcpy(tmp_sym_sbox,RC4Key,RC4KeyLen);
  79.  
  80.         x = 0;
  81.         y = 0;
  82.         s = tmp_sym_sbox;
  83.         midpoint = inLen / 2;
  84.  
  85.         for (pos=midpoint; pos<inLen; pos++)
  86.         {
  87.             x = (x + 1) & 255;
  88.             y = (y + s[x]) & 255;
  89.             tmp = s[x];
  90.             s[x] = s[y];
  91.             s[y] = tmp;
  92.             tmp = (s[x] + s[y]) & 255;
  93.             y = (y + inArray[pos]) & 255;  // this is not standard RC4 here
  94.             inArray[pos] ^= s[tmp];
  95.         }
  96.         for (pos=0; pos<midpoint; pos++)
  97.         {
  98.             x = (x + 1) & 255;
  99.             y = (y + s[x]) & 255;
  100.             tmp = s[x];
  101.             s[x] = s[y];
  102.             s[y] = tmp;
  103.             tmp = (s[x] + s[y]) & 255;
  104.             y = (y + inArray[pos]) & 255;  // this is not standard RC4 here
  105.             inArray[pos] ^= s[tmp];
  106.         }
  107.     }
  108.     free(tmp_sym_sbox);
  109. }
  110.  
  111. void SWTORCrypt::generateRSAKey()
  112. {
  113.     const char rnd_seed[] = "Rocks!";
  114.     RAND_seed(rnd_seed, sizeof rnd_seed);
  115.  
  116.     rsaKey = RSA_generate_key(1536, 65537, NULL, NULL);
  117.     bInitialised = true;
  118. }
  119. void SWTORCrypt::exportRSAPublicKey(char *outKey, int *outLen)
  120. {
  121.     unsigned char buf2[5120];
  122.     unsigned long y, z;
  123.     STORE32L(0x91, buf2);
  124.     y=4;
  125.     buf2[y++] = 0x01;
  126.     OUTPUT_BIGNUM(rsaKey->n, buf2, y, z);
  127.     OUTPUT_BIGNUM(rsaKey->e, buf2, y, z);
  128.  
  129.     memcpy(outKey,buf2,y);
  130.     *outLen = y;
  131. }
  132. void SWTORCrypt::decryptSWTORRSAPacket(const unsigned char *inArray, int inLen, unsigned char *outArray, int *outLen)
  133. {
  134.     *outLen = RSA_private_decrypt(inLen,inArray, outArray,rsaKey,RSA_NO_PADDING);
  135. }
  136.  
  137. void SWTORCrypt::cleanUp()
  138. {
  139.     RSA_free(rsaKey);
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement