home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / cpm / bdsc-2 / crypt.cq / CRYPT.C
Encoding:
C/C++ Source or Header  |  1985-02-10  |  1.8 KB  |  92 lines

  1. /*
  2.  * crypt: a file encryption utility
  3.  *
  4.  * by Andrew Scott Beals
  5.  * 9178 Centerway Rd.
  6.  * Gaithersburg MD 20879
  7.  * (301) 926-0911
  8.  * last update->26 June 1982
  9.  *
  10.  */
  11.  
  12. #include "a:bdscio.h"
  13.  
  14. #ifndef FILE
  15. #define FILE struct _buf
  16. #endif
  17.  
  18. main(argc,argv)
  19. int    argc;
  20. char    *argv[]; {
  21.     char    rotor[128];
  22.     char    rotsiz;
  23.     int    byte;
  24.     char    rotptr;
  25.     char    filout;
  26.     FILE    input,output;
  27.  
  28.     if(argc == 2 && argv[1][0] == '-') {
  29.         printf("crypt is a program designed to provide\n");
  30.         printf("the user with a means to protect his data\n");
  31.         printf("from the perusal of others, even if they\n");
  32.         printf("can get their hands on his file\n");
  33.         exit(1);
  34.     }
  35.  
  36.     if(argc > 5 || argc < 2) {
  37.         printf("usage: crypt [ -K <key> ] <input file> [ <output file> ]\nfor more information, enter: crypt -");
  38.         exit(1);
  39.     }
  40.  
  41.     if(argv[1][0] == '-' && argv[1][1] == 'K') {
  42.         strcpy(rotor,argv[2]);
  43.         argc-=3;    /* make argc == # of files spec */
  44.         argv+=3;    /* argv now points to the first file name */
  45.     } else {
  46.         printf("key? ");
  47.         scanf("%s",rotor);
  48.         argc--;
  49.         argv++;
  50.     }
  51.  
  52.     if(fopen(argv[0],input) == ERROR) {
  53.         printf("can't open %s for input",argv[0]);
  54.         exit(1);
  55.     }
  56.  
  57.     if(argc == 2) {        /* open up the output file */
  58.         filout=1;
  59.         if(fcreat(argv[1],output) == ERROR) {
  60.             printf("can't open %s for output",argv[1]);
  61.             exit(1);
  62.         }
  63.     } else
  64.         filout=0;
  65.  
  66.     rotsiz=strlen(rotor);
  67.     if(!rotsiz) {
  68.         printf("funny guy...the key must be at least 1 character");
  69.         exit(1);
  70.     }
  71.  
  72.     rotptr=1;
  73.  
  74.     printf("%sWorking...",CLEARS);
  75.  
  76.     while((byte=getc(input)) != EOF) {
  77.         byte ^= rotor[rotptr++];
  78.         rotptr %= rotsiz;
  79.         if(filout)
  80.             putc(byte,output);
  81.         else
  82.             putchar(byte);
  83.     }
  84.  
  85.     if(filout) {
  86.         fflush(output);
  87.         fclose(output);
  88.     }
  89.  
  90.     exit(0);
  91. }
  92.