summaryrefslogtreecommitdiff
path: root/main.c
blob: c065116adb3db4ab733adfa85956f62457104c09 (plain)
    1 #include <stdio.h>
    2 #include <string.h>
    3 
    4 #define MAXLEN 256
    5 
    6 int readfile(char* path, char* buf) {
    7   FILE* fd;
    8   fd = fopen(path, "r");
    9   if(!fd)
   10     return -1;
   11   fgets(buf, MAXLEN, fd);
   12   fclose(fd);
   13   return 0;
   14 }
   15 
   16 void applexor(char* msg, char* out) {
   17   int keys[] = {125, 137, 82, 35, 210, 188, 221, 234, 163, 185, 31};
   18   int i = 0;
   19 
   20   // Encrypt/decrypt (xor)
   21   for(i=0; msg[i] != '\0'; i++)
   22     out[i] = msg[i] ^ keys[i % 11];
   23 
   24   // If the previous char wasn't a null byte, we are likely encrypting not
   25   // decrypting, so add a null byte.
   26   if(out[i-1] != '\0') {
   27     out[i] = '\0' ^ keys[i % 11];
   28     i++;
   29   }
   30 
   31   // Append until we hit a multiple of 12
   32   for(; i%12 != 0; i++)
   33     out[i] = msg[i%strlen(msg)] ^ keys[i % 11];
   34   out[i] = '\0' ;
   35 }
   36 
   37 int main(int argc, char* argv[]) {
   38   char in[MAXLEN];
   39   char out[MAXLEN];
   40   if(argc == 1) {
   41     printf("Must provide path to file or a password to encrypt\n");
   42     return 1;
   43   }
   44 
   45   // If the file can't be opened, try to use the "path" as a password
   46   if(readfile(argv[1], in) == -1)
   47     strcpy(in, argv[1]);
   48 
   49   applexor(in, out);
   50 
   51   printf("%s\n", out);
   52   return 0;
   53 }

Generated by cgit