Advertisement
xerpi

keyxorer

Aug 29th, 2014
519
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.94 KB | None | 0 0
  1. /*
  2.     keyxorer by xerpi
  3.     compile with:
  4.         gcc -O3 keyxorer.c -o keyxorer
  5.     usage:
  6.         keyxorer <input file> <key file>
  7.  
  8.     the output will be <input file>.out
  9.     *"big" input files and "small" keys*
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <malloc.h>
  16.  
  17. void print_usage();
  18.  
  19. int main(int argc, char *argv[])
  20. {
  21.     int ret_val = EXIT_SUCCESS;
  22.    
  23.     if (argc < 3) {
  24.         print_usage();
  25.         ret_val = EXIT_FAILURE;
  26.         goto exit_fail_1;
  27.     }
  28.    
  29.     FILE *fd_in, *fd_key, *fd_out;
  30.     if (!(fd_in = fopen(argv[1], "rb"))) {
  31.         printf("Error opening input file\n");
  32.         ret_val = EXIT_FAILURE;
  33.         goto exit_fail_1;
  34.     }
  35.     if (!(fd_key = fopen(argv[2], "rb"))) {
  36.         printf("Error opening key file\n");
  37.         ret_val = EXIT_FAILURE;
  38.         goto exit_fail_2;
  39.     }
  40.    
  41.     fseek(fd_in, 0, SEEK_END);
  42.     long int in_size = ftell(fd_in);
  43.     fseek(fd_in, 0, SEEK_SET);
  44.    
  45.     fseek(fd_key, 0, SEEK_END);
  46.     long int key_size = ftell(fd_key);
  47.     fseek(fd_key, 0, SEEK_SET);
  48.     unsigned char *key_buf  = malloc(key_size);
  49.     unsigned char *data_buf = malloc(key_size);
  50.     if (key_size != fread(key_buf, 1, key_size, fd_key)) {
  51.         printf("Cannot read key file\n");
  52.         ret_val = EXIT_FAILURE;
  53.         goto exit_fail_3;
  54.     }
  55.    
  56.     char *out_name = malloc(strlen(argv[1]) + strlen(".out") + 1);
  57.     sprintf(out_name, "%s.out", argv[1]);
  58.    
  59.     if (!(fd_out = fopen(out_name, "wb+"))) {
  60.         printf("Cannot create output file\n");
  61.         ret_val = EXIT_FAILURE;
  62.         goto exit_fail_4;
  63.     }
  64.    
  65.     #define BAR_LEN 50
  66.     long int temp_read = 0;
  67.     long int step_bytes = in_size/100;
  68.     long int percent = 1;
  69.     size_t bytes_read;
  70.     while ((bytes_read = fread(data_buf, 1, key_size, fd_in))) {
  71.         size_t i;
  72.         for (i = 0;  i < bytes_read; i++) {
  73.             data_buf[i] ^= key_buf[i];
  74.         }
  75.         fwrite(data_buf, 1, bytes_read, fd_out);
  76.        
  77.         //Progress bar
  78.         temp_read += bytes_read;
  79.         if (temp_read >= step_bytes) {
  80.             percent++;
  81.             temp_read = 0;
  82.             printf("%3li%% [", percent);
  83.             int j;
  84.             int bar_size = (BAR_LEN*percent)/100;
  85.             for (j = 0; j < BAR_LEN; j++) {
  86.                 if (j < bar_size) printf("=");
  87.                 else if (j == bar_size) printf(">");
  88.                 else printf(" ");
  89.             }
  90.             printf("]\r");
  91.             fflush(stdout);
  92.         }
  93.     }
  94.     fflush(fd_out);
  95.     fclose(fd_out);
  96.     printf("\nFinished!\n");
  97. exit_fail_4:
  98.     free(out_name);
  99. exit_fail_3:
  100.     free(data_buf);
  101.     free(key_buf);
  102.     fclose(fd_key);
  103. exit_fail_2:
  104.     fclose(fd_in);
  105. exit_fail_1:
  106.     return ret_val;
  107. }
  108.  
  109. void print_usage()
  110. {
  111.     printf("keyxorer by xerpi\n"
  112.            "usage: keyxorer <input file> <key file>\n"
  113.            "the output will be <input file>.out\n");
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement