/*
 * 
 * XORk v 1.1.1, encrypts files using XOR. Copyright (c) 2001 Kalle R'is'nen.
 * 
 * It XOR's the file against a password, AND the password backwards,
 * if it was just XOR'd against the password, the password might be visible
 * in the encrypted file.
 * 
 *
 * Usage: xork file password
 * 
 * 
 */

#include <stdio.h>
#include <string.h>

#define MAXCHAR 256

int main(int argc, char **argv)
{
   FILE *in, *out;
   int i = 0, j, k;
   char *xrk;
   char c, outname[MAXCHAR], passwd[MAXCHAR], passwd2[MAXCHAR];

   if(argc > 2)
      {
      strcpy(outname, argv[1]);
      if(xrk = strstr(outname, ".xrk"))
         {
         xrk[0] = 0;
         strcat(outname, ".uxrk");
         }
      else
         {
         if(xrk = strstr(outname, ".uxrk"))
            xrk[0] = 0;
         strcat(outname, ".xrk");
         }
      strcpy(passwd, argv[2]);
      if(!((strlen(passwd) % 2) == 0))
    strcat(passwd, "x");
      for(j = 0, k = (strlen(passwd) - 1); k >= 0; k--, j++)
    passwd2[j] = passwd[k];  
    
      if((in = fopen(argv[1], "rb")) && (out = fopen(outname, "w")))
         {
         while((c = fgetc(in)) != EOF)
            {
            if(i == strlen(passwd))
               i = 0;   
            c ^= passwd[i];
       c ^= passwd2[i++];   
            fputc(c, out);
            }
         fclose(in);
         fclose(out);
         }
      else
         {
         printf("%s: File-error!!!\n",argv[0]);
         return 20;
         }
      }
   else
      {
      printf("Usage: %s file password\n",argv[0]);
      printf("Passwords maybe 256 characters long. They must be quoted, if they include spaces.\n"); 
      printf("Files ending with \'.xrk\' will be saved with \'.uxrk\' as ending, ");
      printf("and vice-versa.\n");
      return 20;
      }
   return 0;
}

