Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2013
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 21.07 KB | None | 0 0
  1. /*
  2. GS enctypeX servers list decoder/encoder 0.1.3b
  3. by Luigi Auriemma
  4. e-mail: aluigi@autistici.org
  5. web:    aluigi.org
  6.  
  7. This is the algorithm used by ANY new and old game which contacts the Gamespy master server.
  8. It has been written for being used in gslist so there are no explanations or comments here,
  9. if you want to understand something take a look to gslist.c
  10.  
  11.     Copyright 2008-2012 Luigi Auriemma
  12.  
  13.     This program is free software; you can redistribute it and/or modify
  14.     it under the terms of the GNU General Public License as published by
  15.     the Free Software Foundation; either version 2 of the License, or
  16.     (at your option) any later version.
  17.  
  18.     This program is distributed in the hope that it will be useful,
  19.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.     GNU General Public License for more details.
  22.  
  23.     You should have received a copy of the GNU General Public License
  24.     along with this program; if not, write to the Free Software
  25.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  26.  
  27.     http://www.gnu.org/licenses/gpl-2.0.txt
  28. */
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <time.h>
  34. #include <ctype.h>
  35.  
  36.  
  37.  
  38. typedef struct {
  39.     unsigned char   encxkey[261];   // static key
  40.     int             offset;         // everything decrypted till now (total)
  41.     int             start;          // where starts the buffer (so how much big is the header), this is the only one you need to zero
  42. } enctypex_data_t;
  43.  
  44.  
  45.  
  46. int enctypex_func5(unsigned char *encxkey, int cnt, unsigned char *id, int idlen, int *n1, int *n2) {
  47.     int     i,
  48.             tmp,
  49.             mask = 1;
  50.  
  51.     if(!cnt) return(0);
  52.     if(cnt > 1) {
  53.         do {
  54.             mask = (mask << 1) + 1;
  55.         } while(mask < cnt);
  56.     }
  57.  
  58.     i = 0;
  59.     do {
  60.         *n1 = encxkey[*n1 & 0xff] + id[*n2];
  61.         (*n2)++;
  62.         if(*n2 >= idlen) {
  63.             *n2 = 0;
  64.             *n1 += idlen;
  65.         }
  66.         tmp = *n1 & mask;
  67.         if(++i > 11) tmp %= cnt;
  68.     } while(tmp > cnt);
  69.  
  70.     return(tmp);
  71. }
  72.  
  73.  
  74.  
  75. void enctypex_func4(unsigned char *encxkey, unsigned char *id, int idlen) {
  76.     int             i,
  77.                     n1 = 0,
  78.                     n2 = 0;
  79.     unsigned char   t1,
  80.                     t2;
  81.  
  82.     if(idlen < 1) return;
  83.  
  84.     for(i = 0; i < 256; i++) encxkey[i] = i;
  85.  
  86.     for(i = 255; i >= 0; i--) {
  87.         t1 = enctypex_func5(encxkey, i, id, idlen, &n1, &n2);
  88.         t2 = encxkey[i];
  89.         encxkey[i] = encxkey[t1];
  90.         encxkey[t1] = t2;
  91.     }
  92.  
  93.     encxkey[256] = encxkey[1];
  94.     encxkey[257] = encxkey[3];
  95.     encxkey[258] = encxkey[5];
  96.     encxkey[259] = encxkey[7];
  97.     encxkey[260] = encxkey[n1 & 0xff];
  98. }
  99.  
  100.  
  101.  
  102. int enctypex_func7(unsigned char *encxkey, unsigned char d) {
  103.     unsigned char   a,
  104.                     b,
  105.                     c;
  106.  
  107.     a = encxkey[256];
  108.     b = encxkey[257];
  109.     c = encxkey[a];
  110.     encxkey[256] = a + 1;
  111.     encxkey[257] = b + c;
  112.     a = encxkey[260];
  113.     b = encxkey[257];
  114.     b = encxkey[b];
  115.     c = encxkey[a];
  116.     encxkey[a] = b;
  117.     a = encxkey[259];
  118.     b = encxkey[257];
  119.     a = encxkey[a];
  120.     encxkey[b] = a;
  121.     a = encxkey[256];
  122.     b = encxkey[259];
  123.     a = encxkey[a];
  124.     encxkey[b] = a;
  125.     a = encxkey[256];
  126.     encxkey[a] = c;
  127.     b = encxkey[258];
  128.     a = encxkey[c];
  129.     c = encxkey[259];
  130.     b += a;
  131.     encxkey[258] = b;
  132.     a = b;
  133.     c = encxkey[c];
  134.     b = encxkey[257];
  135.     b = encxkey[b];
  136.     a = encxkey[a];
  137.     c += b;
  138.     b = encxkey[260];
  139.     b = encxkey[b];
  140.     c += b;
  141.     b = encxkey[c];
  142.     c = encxkey[256];
  143.     c = encxkey[c];
  144.     a += c;
  145.     c = encxkey[b];
  146.     b = encxkey[a];
  147.     encxkey[260] = d;
  148.     c ^= b ^ d;
  149.     encxkey[259] = c;
  150.     return(c);
  151. }
  152.  
  153.  
  154.  
  155. int enctypex_func7e(unsigned char *encxkey, unsigned char d) {
  156.     unsigned char   a,
  157.                     b,
  158.                     c;
  159.  
  160.     a = encxkey[256];
  161.     b = encxkey[257];
  162.     c = encxkey[a];
  163.     encxkey[256] = a + 1;
  164.     encxkey[257] = b + c;
  165.     a = encxkey[260];
  166.     b = encxkey[257];
  167.     b = encxkey[b];
  168.     c = encxkey[a];
  169.     encxkey[a] = b;
  170.     a = encxkey[259];
  171.     b = encxkey[257];
  172.     a = encxkey[a];
  173.     encxkey[b] = a;
  174.     a = encxkey[256];
  175.     b = encxkey[259];
  176.     a = encxkey[a];
  177.     encxkey[b] = a;
  178.     a = encxkey[256];
  179.     encxkey[a] = c;
  180.     b = encxkey[258];
  181.     a = encxkey[c];
  182.     c = encxkey[259];
  183.     b += a;
  184.     encxkey[258] = b;
  185.     a = b;
  186.     c = encxkey[c];
  187.     b = encxkey[257];
  188.     b = encxkey[b];
  189.     a = encxkey[a];
  190.     c += b;
  191.     b = encxkey[260];
  192.     b = encxkey[b];
  193.     c += b;
  194.     b = encxkey[c];
  195.     c = encxkey[256];
  196.     c = encxkey[c];
  197.     a += c;
  198.     c = encxkey[b];
  199.     b = encxkey[a];
  200.     c ^= b ^ d;
  201.     encxkey[260] = c;   // encrypt
  202.     encxkey[259] = d;   // encrypt
  203.     return(c);
  204. }
  205.  
  206.  
  207.  
  208. int enctypex_func6(unsigned char *encxkey, unsigned char *data, int len) {
  209.     int     i;
  210.  
  211.     for(i = 0; i < len; i++) {
  212.         data[i] = enctypex_func7(encxkey, data[i]);
  213.     }
  214.     return(len);
  215. }
  216.  
  217.  
  218.  
  219. int enctypex_func6e(unsigned char *encxkey, unsigned char *data, int len) {
  220.     int     i;
  221.  
  222.     for(i = 0; i < len; i++) {
  223.         data[i] = enctypex_func7e(encxkey, data[i]);
  224.     }
  225.     return(len);
  226. }
  227.  
  228.  
  229.  
  230. void enctypex_funcx(unsigned char *encxkey, unsigned char *key, unsigned char *encxvalidate, unsigned char *data, int datalen) {
  231.     int     i,
  232.             keylen;
  233.  
  234.     keylen = strlen(key);
  235.     for(i = 0; i < datalen; i++) {
  236.         encxvalidate[(key[i % keylen] * i) & 7] ^= encxvalidate[i & 7] ^ data[i];
  237.     }
  238.     enctypex_func4(encxkey, encxvalidate, 8);
  239. }
  240.  
  241.  
  242.  
  243. static int enctypex_data_cleaner_level = 2; // 0 = do nothing
  244.                                             // 1 = colors
  245.                                             // 2 = colors + strange chars
  246.                                             // 3 = colors + strange chars + sql
  247.  
  248.  
  249.  
  250. int enctypex_data_cleaner(unsigned char *dst, unsigned char *src, int max) {
  251.     static const unsigned char strange_chars[] = {
  252.                     ' ','E',' ',',','f',',','.','t',' ','^','%','S','<','E',' ','Z',
  253.                     ' ',' ','`','`','"','"','.','-','-','~','`','S','>','e',' ','Z',
  254.                     'Y','Y','i','c','e','o','Y','I','S','`','c','a','<','-','-','E',
  255.                     '-','`','+','2','3','`','u','P','-',',','1','`','>','%','%','%',
  256.                     '?','A','A','A','A','A','A','A','C','E','E','E','E','I','I','I',
  257.                     'I','D','N','O','O','O','O','O','x','0','U','U','U','U','Y','D',
  258.                     'B','a','a','a','a','a','a','e','c','e','e','e','e','i','i','i',
  259.                     'i','o','n','o','o','o','o','o','+','o','u','u','u','u','y','b',
  260.                     'y' };
  261.     unsigned char   c,
  262.                     *p;
  263.  
  264.     if(!dst) return(0);
  265.     if(dst != src) dst[0] = 0;  // the only change in 0.1.3a
  266.     if(!src) return(0);
  267.  
  268.     if(max < 0) max = strlen(src);
  269.  
  270.     for(p = dst; (c = *src) && (max > 0); src++, max--) {
  271.         if(c == '\\') {                     // avoids the backslash delimiter
  272.             *p++ = '/';
  273.             continue;
  274.         }
  275.  
  276.         if(enctypex_data_cleaner_level >= 1) {
  277.             if(c == '^') {                  // Quake 3 colors
  278.                 //if(src[1] == 'x') {         // ^x112233 (I don't remember the game which used this format)
  279.                     //src += 7;
  280.                     //max -= 7;
  281.                 //} else
  282.                 if(isdigit(src[1]) || islower(src[1])) { // ^0-^9, ^a-^z... a good compromise
  283.                     src++;
  284.                     max--;
  285.                 } else {
  286.                     *p++ = c;
  287.                 }
  288.                 continue;
  289.             }
  290.             if(c == 0x1b) {                 // Unreal colors
  291.                 src += 3;
  292.                 max -= 3;
  293.                 continue;
  294.             }
  295.             if(c < ' ') {                   // other colors
  296.                 continue;
  297.             }
  298.         }
  299.  
  300.         if(enctypex_data_cleaner_level >= 2) {
  301.             if(c >= 0x7f) c = strange_chars[c - 0x7f];
  302.         }
  303.  
  304.         if(enctypex_data_cleaner_level >= 3) {
  305.             switch(c) {                     // html/SQL injection (paranoid mode)
  306.                 case '\'':
  307.                 case '\"':
  308.                 case '&':
  309.                 case '^':
  310.                 case '?':
  311.                 case '{':
  312.                 case '}':
  313.                 case '(':
  314.                 case ')':
  315.                 case '[':
  316.                 case ']':
  317.                 case '-':
  318.                 case ';':
  319.                 case '~':
  320.                 case '|':
  321.                 case '$':
  322.                 case '!':
  323.                 case '<':
  324.                 case '>':
  325.                 case '*':
  326.                 case '%':
  327.                 case ',': c = '.';  break;
  328.                 default: break;
  329.             }
  330.         }
  331.  
  332.         if((c == '\r') || (c == '\n')) {    // no new line
  333.             continue;
  334.         }
  335.         *p++ = c;
  336.     }
  337.     *p = 0;
  338.     return(p - dst);
  339. }
  340.  
  341.  
  342.  
  343.     // function not related to the algorithm, I have created it only for a quick handling of the received data
  344.     // very quick explanation:
  345.     // - if you use out it will be considered as an output buffer where placing all the IP and ports of the servers in the classical format: 4 bytes for IP and 2 for the port
  346.     // - if you don't use out the function will return a non-zero value if you have received all the data from the master server
  347.     // - if you use infobuff it will be considered as an output buffer where placing all the informations of one server at time in the format "IP:port \parameter1\value1\...\parameterN\valueN"
  348.     // - infobuff_size is used to avoid to write more data than how much supported by infobuff
  349.     // - infobuff_offset instead is used for quickly handling the next servers for infobuff because, as just said, the function handles only one server at time
  350.     //   infobuff_offset is just the offset of the server to handle in our enctypex buffer, the function returns the offset to the next one or a value zero or -1 if there are no other hosts
  351.     // data and out can't be the same buffer because in some games like AA the gamespy master server returns
  352.     // 5 bytes for each IP/port and so there is the risk of overwriting the data to handle, that's why I use
  353.     // an output buffer which is at least "(datalen / 5) * 6" bytes long
  354. int enctypex_decoder_convert_to_ipport(unsigned char *data, int datalen, unsigned char *out, unsigned char *infobuff, int infobuff_size, int infobuff_offset) {
  355. #define enctypex_infobuff_check(X) \
  356.     if(infobuff) { \
  357.         if((int)(infobuff_size - infobuff_len) <= (int)(X)) { \
  358.             infobuff_size = 0; \
  359.         } else
  360.  
  361.     typedef struct {
  362.         unsigned char   type;
  363.         unsigned char   *name;
  364.     } par_t;
  365.  
  366.     int             i,
  367.                     len,
  368.                     pars    = 0,    // pars and vals are used for making the function
  369.                     vals    = 0,    // thread-safe when infobuff is not used
  370.                     infobuff_len = 0;
  371.     unsigned char   tmpip[6],
  372.                     port[2],
  373.                     t,
  374.                     *p,
  375.                     *o,
  376.                     *l;
  377.     static const int    use_parval = 1; // par and val are required, so this bool is useless
  378.     static unsigned char    // this function is not thread-safe if you use it for retrieving the extra data (infobuff)
  379.                     parz    = 0,
  380.                     valz    = 0,
  381.                     **val   = NULL;
  382.     static par_t    *par    = NULL; // par[255] and *val[255] was good too
  383.  
  384.     if(!data) return(0);
  385.     if(datalen < 6) return(0);  // covers the 6 bytes of IP:port
  386.     o = out;
  387.     p = data;
  388.     l = data + datalen;
  389.  
  390.     p += 4;         // your IP
  391.     port[0] = *p++; // the most used port
  392.     port[1] = *p++;
  393.     if((port[0] == 0xff) && (port[1] == 0xff)) {
  394.         return(-1); // error message from the server
  395.     }
  396.  
  397.     if(infobuff && infobuff_offset) {   // restore the data
  398.         p = data + infobuff_offset;
  399.     } else {
  400.         if(p < l) {
  401.             pars = *p++;
  402.             if(use_parval) {  // save the static data
  403.                 parz = pars;
  404.                 par  = realloc(par, sizeof(par_t) * parz);
  405.             }
  406.             for(i = 0; (i < pars) && (p < l); i++) {
  407.                 t = *p++;
  408.                 if(use_parval) {
  409.                     par[i].type = t;
  410.                     par[i].name = p;
  411.                 }
  412.                 p += strlen(p) + 1;
  413.             }
  414.         }
  415.         if(p < l) {
  416.             vals = *p++;
  417.             if(use_parval) {  // save the static data
  418.                 valz = vals;
  419.                 val  = realloc(val, sizeof(unsigned char *) * valz);
  420.             }
  421.             for(i = 0; (i < vals) && (p < l); i++) {
  422.                 if(use_parval) val[i] = p;
  423.                 p += strlen(p) + 1;
  424.             }
  425.         }
  426.     }
  427.  
  428.     if(use_parval) {
  429.         pars = parz;
  430.         vals = valz;
  431.     }
  432.     if(infobuff && (infobuff_size > 0)) {
  433.         infobuff[0] = 0;
  434.     }
  435.  
  436.     while(p < l) {
  437.         t = *p++;
  438.         if(!t && !memcmp(p, "\xff\xff\xff\xff", 4)) {
  439.             if(!out) o = out - 1;   // so the return is not 0 and means that we have reached the end
  440.             break;
  441.         }
  442.         len = 5;
  443.         if(t & 0x02) len = 9;
  444.         if(t & 0x08) len += 4;
  445.         if(t & 0x10) len += 2;
  446.         if(t & 0x20) len += 2;
  447.  
  448.         tmpip[0] = p[0];
  449.         tmpip[1] = p[1];
  450.         tmpip[2] = p[2];
  451.         tmpip[3] = p[3];
  452.         if((len < 6) || !(t & 0x10)) {
  453.             tmpip[4] = port[0];
  454.             tmpip[5] = port[1];
  455.         } else {
  456.             tmpip[4] = p[4];
  457.             tmpip[5] = p[5];
  458.         }
  459.  
  460.         if(out) {
  461.             memcpy(o, tmpip, 6);
  462.             o += 6;
  463.         }
  464.         enctypex_infobuff_check(22) {
  465.             infobuff_len = sprintf(infobuff,
  466.                 "%u.%u.%u.%u:%hu ",
  467.                 tmpip[0], tmpip[1], tmpip[2], tmpip[3],
  468.                 (unsigned short)((tmpip[4] << 8) | tmpip[5]));
  469.         }}
  470.  
  471.         p += len - 1;   // the value in len is no longer used from this point
  472.         if(t & 0x40) {
  473.             for(i = 0; (i < pars) && (p < l); i++) {
  474.                 enctypex_infobuff_check(1 + strlen(par[i].name) + 1) {
  475.                     infobuff[infobuff_len++] = '\\';
  476.                     infobuff_len += enctypex_data_cleaner(infobuff + infobuff_len, par[i].name, -1);
  477.                     infobuff[infobuff_len++] = '\\';
  478.                     infobuff[infobuff_len]   = 0;
  479.                 }}
  480.                 t = *p++;
  481.  
  482.                 if(use_parval) {
  483.                     if(!par[i].type) {  // string
  484.                         if(t == 0xff) { // inline string
  485.                             enctypex_infobuff_check(strlen(p)) {
  486.                                 infobuff_len += enctypex_data_cleaner(infobuff + infobuff_len, p, -1);
  487.                             }}
  488.                             p += strlen(p) + 1;
  489.                         } else {        // fixed string
  490.                             if(t < vals) {
  491.                                 enctypex_infobuff_check(strlen(val[t])) {
  492.                                     infobuff_len += enctypex_data_cleaner(infobuff + infobuff_len, val[t], -1);
  493.                                 }}
  494.                             }
  495.                         }
  496.                     } else {            // number (-128 to 127)
  497.                         enctypex_infobuff_check(5) {
  498.                             infobuff_len += sprintf(infobuff + infobuff_len, "%d", (signed char)t);
  499.                         }}
  500.                     }
  501.                 }
  502.             }
  503.         }
  504.         if(infobuff) {  // do NOT touch par/val, I use realloc
  505.             return(p - data);
  506.         }
  507.     }
  508.  
  509.     if((out == data) && ((o - out) > (p - data))) { // I need to remember this
  510.         fprintf(stderr, "\nError: input and output buffer are the same and there is not enough space\n");
  511.         exit(1);
  512.     }
  513.     if(infobuff) {      // do NOT touch par/val, I use realloc
  514.         parz = 0;
  515.         valz = 0;
  516.         return(-1);
  517.     }
  518.     return(o - out);
  519. }
  520.  
  521.  
  522.  
  523. int enctypex_decoder_rand_validate(unsigned char *validate) {
  524.     int     i,
  525.             rnd;
  526.  
  527.     rnd = ~time(NULL);
  528.     for(i = 0; i < 8; i++) {
  529.         do {
  530.             rnd = ((rnd * 0x343FD) + 0x269EC3) & 0x7f;
  531.         } while((rnd < 0x21) || (rnd >= 0x7f));
  532.         validate[i] = rnd;
  533.     }
  534.     validate[i] = 0;
  535.     return(i);
  536. }
  537.  
  538.  
  539.  
  540. unsigned char *enctypex_init(unsigned char *encxkey, unsigned char *key, unsigned char *validate, unsigned char *data, int *datalen, enctypex_data_t *enctypex_data) {
  541.     int             a,
  542.                     b;
  543.     unsigned char   encxvalidate[8];
  544.  
  545.     if(*datalen < 1) return(NULL);
  546.     a = (data[0] ^ 0xec) + 2;
  547.     if(*datalen < a) return(NULL);
  548.     b = data[a - 1] ^ 0xea;
  549.     if(*datalen < (a + b)) return(NULL);
  550.     memcpy(encxvalidate, validate, 8);
  551.     enctypex_funcx(encxkey, key, encxvalidate, data + a, b);
  552.     a += b;
  553.     if(!enctypex_data) {
  554.         data     += a;
  555.         *datalen -= a;  // datalen is untouched in stream mode!!!
  556.     } else {
  557.         enctypex_data->offset = a;
  558.         enctypex_data->start  = a;
  559.     }
  560.     return(data);
  561. }
  562.  
  563.  
  564.  
  565. unsigned char *enctypex_decoder(unsigned char *key, unsigned char *validate, unsigned char *data, int *datalen, enctypex_data_t *enctypex_data) {
  566.     unsigned char   encxkeyb[261],
  567.                     *encxkey;
  568.  
  569.     encxkey = enctypex_data ? enctypex_data->encxkey : encxkeyb;
  570.  
  571.     if(!enctypex_data || (enctypex_data && !enctypex_data->start)) {
  572.         data = enctypex_init(encxkey, key, validate, data, datalen, enctypex_data);
  573.         if(!data) return(NULL);
  574.     }
  575.     if(!enctypex_data) {
  576.         enctypex_func6(encxkey, data, *datalen);
  577.         return(data);
  578.     } else if(enctypex_data && enctypex_data->start) {
  579.         enctypex_data->offset += enctypex_func6(encxkey, data + enctypex_data->offset, *datalen - enctypex_data->offset);
  580.         return(data + enctypex_data->start);
  581.     }
  582.     return(NULL);
  583. }
  584.  
  585.  
  586.  
  587. // exactly as above but with enctypex_func6e instead of enctypex_func6
  588. unsigned char *enctypex_encoder(unsigned char *key, unsigned char *validate, unsigned char *data, int *datalen, enctypex_data_t *enctypex_data) {
  589.     unsigned char   encxkeyb[261],
  590.                     *encxkey;
  591.  
  592.     encxkey = enctypex_data ? enctypex_data->encxkey : encxkeyb;
  593.  
  594.     if(!enctypex_data || (enctypex_data && !enctypex_data->start)) {
  595.         data = enctypex_init(encxkey, key, validate, data, datalen, enctypex_data);
  596.         if(!data) return(NULL);
  597.     }
  598.     if(!enctypex_data) {
  599.         enctypex_func6e(encxkey, data, *datalen);
  600.         return(data);
  601.     } else if(enctypex_data && enctypex_data->start) {
  602.         enctypex_data->offset += enctypex_func6e(encxkey, data + enctypex_data->offset, *datalen - enctypex_data->offset);
  603.         return(data + enctypex_data->start);
  604.     }
  605.     return(NULL);
  606. }
  607.  
  608.  
  609.  
  610. unsigned char *enctypex_msname(unsigned char *gamename, unsigned char *retname) {
  611.     static unsigned char    msname[256];
  612.     unsigned    i,
  613.                 c,
  614.                 server_num;
  615.  
  616.     if(!gamename) return(NULL);
  617.  
  618.     server_num = 0;
  619.     for(i = 0; gamename[i]; i++) {
  620.         c = tolower(gamename[i]);
  621.         server_num = c - (server_num * 0x63306ce7);
  622.     }
  623.     server_num %= 20;
  624.  
  625.     if(retname) {
  626.         snprintf(retname, 256, "%s.ms%d.gamespy.com", gamename, server_num);
  627.         return(retname);
  628.     }
  629.     snprintf(msname, sizeof(msname), "%s.ms%d.gamespy.com", gamename, server_num);
  630.     return(msname);
  631. }
  632.  
  633.  
  634.  
  635. int enctypex_wrapper(unsigned char *key, unsigned char *validate, unsigned char *data, int size) {
  636.     int             i;
  637.     unsigned char   *p;
  638.  
  639.     if(!key || !validate || !data || (size < 0)) return(0);
  640.  
  641.     p = enctypex_decoder(key, validate, data, &size, NULL);
  642.     if(!p) return(-1);
  643.     for(i = 0; i < size; i++) {
  644.         data[i] = p[i];
  645.     }
  646.     return(size);
  647. }
  648.  
  649.  
  650.  
  651. // data must be enough big to include the 23 bytes header, remember it: data = realloc(data, size + 23);
  652. int enctypex_quick_encrypt(unsigned char *key, unsigned char *validate, unsigned char *data, int size) {
  653.     int             i,
  654.                     rnd,
  655.                     tmpsize,
  656.                     keylen,
  657.                     vallen;
  658.     unsigned char   tmp[23];
  659.  
  660.     if(!key || !validate || !data || (size < 0)) return(0);
  661.  
  662.     keylen = strlen(key);   // only for giving a certain randomness, so useless
  663.     vallen = strlen(validate);
  664.     rnd = ~time(NULL);
  665.     for(i = 0; i < sizeof(tmp); i++) {
  666.         rnd = (rnd * 0x343FD) + 0x269EC3;
  667.         tmp[i] = rnd ^ key[i % keylen] ^ validate[i % vallen];
  668.     }
  669.     tmp[0] = 0xeb;  // 7
  670.     tmp[1] = 0x00;
  671.     tmp[2] = 0x00;
  672.     tmp[8] = 0xe4;  // 14
  673.  
  674.     for(i = size - 1; i >= 0; i--) {
  675.         data[sizeof(tmp) + i] = data[i];
  676.     }
  677.     memcpy(data, tmp, sizeof(tmp));
  678.     size += sizeof(tmp);
  679.  
  680.     tmpsize = size;
  681.     enctypex_encoder(key, validate, data, &tmpsize, NULL);
  682.     return(size);
  683. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement