home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / SNPD9404.ZIP / CRYPT.C < prev    next >
C/C++ Source or Header  |  1994-04-03  |  2KB  |  64 lines

  1. .I 37 61
  2. void crypt(unsigned char *buf)
  3. {
  4.       *buf ^= cryptext[crypt_ptr] ^ (cryptext[0] * crypt_ptr);
  5.       cryptext[crypt_ptr] += ((crypt_ptr < (crypt_length - 1)) ?
  6.             cryptext[crypt_ptr + 1] : cryptext[0]);
  7.       if (!cryptext[crypt_ptr])
  8.             cryptext[crypt_ptr] += 1;
  9.       if (++crypt_ptr >= crypt_length)
  10.             crypt_ptr = 0;
  11. }
  12.  
  13. /**** Encrypt/decrypt buffer ************************************/
  14. void bufcrypt(unsigned char *buf, long length)
  15. {
  16.       while (length--)
  17.             crypt(*buf++)
  18. }
  19.  
  20. #ifdef TEST
  21.  
  22. #include <stdio.h>
  23. #include <string.h>
  24.  
  25. int main(int argc, char *argv[])
  26. {
  27.       static char buf[16384];
  28.       size_t len, i;
  29.       FILE *in, *out;
  30.  
  31.       if (4 > argc)
  32.       {
  33.             puts("Usage: CRYPT password infile outfile");
  34.             return -1;
  35.       }
  36.       cryptext = argv[1];
  37.       crypt_length = strlen(cryptext);
  38.       if (NULL == (in = fopen(argv[2], "rb")))
  39.       {
  40.             printf("Can't open %s for input\n", argv[2]);
  41.             return -1;
  42.       }
  43.       if (NULL == (out = fopen(argv[3], "wb")))
  44.       {
  45.             printf("Can't open %s for output\n", argv[3]);
  46.             return -1;
  47.       }
  48.       do
  49.       {
  50.             if (0 != (len = fread(buf, 1, 16384, in)))
  51.             {
  52.                   for (i = 0; i < len; ++i)
  53.                         crypt(&buf[i]);
  54.                   fwrite(buf, 1, len, out);
  55.             }
  56.       } while (len);
  57.       fclose(in);
  58.       fclose(out);
  59.       return 0;
  60. }
  61.  
  62. #endif
  63. .D 38 10
  64.