ABaDy1996

[Source] : encryptor challenge

Sep 7th, 2018
166
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <unistd.h>
  6.  
  7. int loccc(char *s, int n){
  8.     div_t qr;
  9.     int pos;
  10.  
  11.     if(n == 0) return 0;
  12.  
  13.     qr = div(n, 10);
  14.     pos = loccc(s, qr.quot);
  15.     s[pos] = qr.rem + '0';
  16.     return pos + 1;
  17. }
  18.  
  19. int crypt(char* filename){
  20.     FILE *fp;
  21.     FILE *fp2;
  22.     int i;
  23.     int sz;
  24.     unsigned char k[500];
  25.     int cc=2;
  26.     unsigned char c[500]="";
  27.     fp=fopen(filename, "r");
  28.     fread(k, 500, 1, fp);
  29.     fseek(fp, 0L, SEEK_END);
  30.     sz = ftell(fp);
  31.     fp2=fopen(filename, "w+");
  32.     for (i=0; i<sz; i++){
  33.         cc+=(cc^(i+3));
  34.         c[i] = (k[i]+i) ^ (cc+i);
  35.    }
  36.    fwrite(c, 1, strlen(c)/1, fp2);
  37.    fclose(fp);
  38.    fclose(fp2);
  39. }
  40.  
  41. char *str_replace(char *orig, char *rep, char *with) {
  42.     char *result;
  43.     char *ins;  
  44.     char *tmp;
  45.     int len_rep;
  46.     int len_with;
  47.     int len_front;
  48.     int count;    
  49.  
  50.     if (!orig || !rep)
  51.         return NULL;
  52.     len_rep = strlen(rep);
  53.     if (len_rep == 0)
  54.         return NULL;
  55.     if (!with)
  56.         with = "";
  57.     len_with = strlen(with);
  58.  
  59.     ins = orig;
  60.     for (count = 0; tmp = strstr(ins, rep); ++count) {
  61.         ins = tmp + len_rep;
  62.     }
  63.  
  64.     tmp = result = malloc(strlen(orig) + (len_with - len_rep) * count + 1);
  65.  
  66.     if (!result)
  67.         return NULL;
  68.  
  69.     while (count--) {
  70.         ins = strstr(orig, rep);
  71.         len_front = ins - orig;
  72.         tmp = strncpy(tmp, orig, len_front) + len_front;
  73.         tmp = strcpy(tmp, with) + len_with;
  74.         orig += len_front + len_rep;
  75.     }
  76.     strcpy(tmp, orig);
  77.     return result;
  78. }
  79. char** str_split(char* a_str, const char a_delim)
  80. {
  81.     char** result    = 0;
  82.     size_t count     = 0;
  83.     char* tmp        = a_str;
  84.     char* last_comma = 0;
  85.     char delim[2];
  86.     delim[0] = a_delim;
  87.     delim[1] = 0;
  88.  
  89.     while (*tmp)
  90.     {
  91.         if (a_delim == *tmp)
  92.         {
  93.             count++;
  94.             last_comma = tmp;
  95.         }
  96.         tmp++;
  97.     }
  98.  
  99.     count += last_comma < (a_str + strlen(a_str) - 1);
  100.  
  101.     count++;
  102.  
  103.     result = malloc(sizeof(char*) * count);
  104.  
  105.     if (result)
  106.     {
  107.         size_t idx  = 0;
  108.         char* token = strtok(a_str, delim);
  109.  
  110.         while (token)
  111.         {
  112.             assert(idx < count);
  113.             *(result + idx++) = strdup(token);
  114.             token = strtok(0, delim);
  115.         }
  116.         assert(idx == count - 1);
  117.         *(result + idx) = 0;
  118.     }
  119.  
  120.     return result;
  121. }
  122. char* ctyhh(char *output_buff, int num){
  123.     char *p = output_buff;
  124.     if(num < 0){
  125.         *p++ = '-';
  126.         num *= -1;
  127.     } else if(num == 0)
  128.         *p++ = '0';
  129.     p[loccc(p, num)]='\0';
  130.     return output_buff;
  131. }
  132. int main(int argc, char const *argv[])
  133. {
  134.     char *what=malloc(500);
  135. here:
  136.     printf("\nwhat you want to do ? ");
  137.     fgets(what, 500, stdin);
  138.     what[strlen (what) - 1] = '\0';
  139.     if(!strcmp("help",what)){
  140.         printf("\nyou can encrypt file by : encrypt filename\n");
  141.         goto here;
  142.     }else if(!strcmp("decrypt flag",what)){
  143.         printf("\nreally ? do u really think i'm fu**ing stupid ! :(\n");
  144.         goto here;
  145.     }else if(strstr(what, "encrypt")!=NULL){
  146.         what = str_replace(what,"encrypt ","");
  147.         if(access(what,F_OK)!=-1) {
  148.             crypt(what);
  149.             printf("\ndone.\n");
  150.         } else {
  151.             printf("\nFile not found !\n");
  152.         }
  153.         goto here;
  154.     }else if(strstr(what, "quit")!=NULL){
  155.         return 0;
  156.     }else{
  157.         printf("\nuse : help to help :)\nor quit to quit ;)\n");
  158.         goto here;
  159.     }
  160.     return 0;
  161. }
RAW Paste Data