home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 103_01 / crypt.c < prev    next >
Text File  |  1985-03-10  |  7KB  |  252 lines

  1. /* last update 7 May 81
  2.  
  3. CRYPT program by kathy bacon and neal somos
  4.  
  5.     CRYPT can be used to "encrypt" a file for secrecy, using a 
  6. special keyword which the user specifies; later, the original file
  7. can be retrieved by re-encrypting the file with the same keyword.
  8. If you forget your keyword...well, sorry.
  9.  
  10.  
  11. CRYPT accepts command line arguments of three forms:
  12.  
  13.      INFILE
  14.      INFILE>OUTFILE
  15.      <KEYWORD
  16.  
  17. If no output file is specified, the encrypted form of INFILE will be
  18. written out as INFILE.CRP ( if the input file has an extension of 
  19. ".CRP", however, it will be written out as TEMP.$$$ ).
  20.     In the second case, the ">" means that the output file name
  21. will follow.
  22.     The key word itself can also be specified on the command line,
  23. which is useful if you want to use a submit file. Only one keyword per
  24. command line, however.
  25.     CRYPT will ask for the keyword if none is given; it will not
  26. be echoed to the console.
  27.  
  28.     :::::::::::::::::::::::::::::::::::::::::::
  29.  
  30. NOTE!!!!!  There should be NO SPACES between a ">" and the filenames;
  31.        nor between a "<" and its keyword.
  32.  
  33.     :::::::::::::::::::::::::::::::::::::::::::
  34.  
  35. a sample call might therefore be:
  36.  
  37. A>crypt  tomb.c  help.crp>help.c   <yippeezoop
  38.  
  39.  
  40. ************************************************************/
  41.  
  42. #include "bdscio.h"
  43.  
  44. int debug;
  45.  
  46. #define    KEYCHAR    '<'
  47. #define OUTCHAR '>'
  48. #define READ    0
  49. #define    ERROR_EXIT    { close(fd_in);    exit();    }
  50. #define NEWLINE    '\n'
  51. #define DEBUG     (debug != 0)
  52. #define    BLANK    ' '
  53. #define    BACKSPACE    '\b'
  54. #define CR    '\r'
  55. #define NUL    '\0'
  56.  
  57.  
  58. main(argc, argv)
  59. int argc;
  60. char **argv;
  61. {
  62. char filename[30], filecrypt[30], after[10];
  63. char key[100], t[100], argument[100];
  64. char *comarg;
  65. char cryptbuf [8*SECSIZ];
  66. int fd_in, fd_crypt;
  67. int i, j, k, l, len;
  68. int seed[3];
  69. int keylen, fine, nread;
  70.  
  71. debug = FALSE;
  72. if (argc == 1)    /* we'll change this later, so no snide remarks, neal */
  73.       { printf("\nthis is a cryptic program!! heeheeheehee");
  74.     exit();
  75.       }
  76. setmem (key, 100, NUL);
  77. for (i=1; i<argc; i++)        /* check for key */
  78.       { comarg = argv[i];
  79.     if (*comarg == KEYCHAR)    /* key found */
  80.           { strcpy (key, &comarg[1]);
  81.         break;
  82.           }
  83.     if (OK == strcmp (comarg, "-D"))    /* debug */
  84.           debug = TRUE;
  85.       }
  86.  
  87. if (i == argc)            /* no key found on command line */
  88.       {
  89.     while (1)    /* get 'key' for encrypting file */
  90.           { puts ("\nkey ?     (no echo)\n");
  91.         if (0 != (keylen =no_echo (key)))
  92.               { if DEBUG printf("\nthanks that was <%s>",key);
  93.             puts("\nplease retype for verification:\n");
  94.             if (0 != (l = no_echo (t)))
  95.                   { if (OK != strcmp (key,t))
  96.                       { puts("\nthey aren't the same,");
  97.                     puts(" so we'd better start over.\n");
  98.                     continue;    /* while (1) */
  99.                       }
  100.                 else puts("\nkey verified.");
  101.                 break;    /* from while (1) */
  102.                   }
  103.               }
  104.     
  105.         puts ("\nOK, be that way!");
  106.         
  107.         exit();
  108.     
  109.           }    /* end while (1) */
  110.       }
  111. argv++;
  112. while (--argc)
  113.       { strcpy (argument,*argv++);
  114.  
  115.     if DEBUG printf("\nnext argument is    |%s|", argument);
  116.     if (OK == strcmp (argument, "-D"))    /* debug */
  117.         continue;        /* while (--argc) */
  118.     if (argument[0] == KEYCHAR)        /* key */
  119.         continue;
  120.  
  121.     len = strlen (argument);
  122.     if (argument[0]==OUTCHAR || argument[len-1]== OUTCHAR)    /* ERROR!!*/
  123.           { puts("\n\nPROFOUND error:");
  124.             puts ("\nNO spaces allowed around output specifier > ");
  125.         printf("\n%s is illegal.", argument);
  126.         continue;
  127.           }
  128.  
  129.     setmem (filename, 30, NUL); setmem(filecrypt, 30, NUL);
  130.     if (ERROR != index (argument, ">"))
  131.           sscanf (argument, "%s>%s", filename, filecrypt);
  132.     else strcpy (filename, argument);
  133.  
  134.     printf("\nNow processing file <%s>", filename);
  135.     if (ERROR == (fd_in = open(filename, READ)))
  136.           { printf("\ncan not open <%s>", filename);
  137.         continue;        /* while (--argc) */
  138.           }
  139.  
  140.     if (filecrypt[0] == NUL)    /* outfile not specified */
  141.           {    sscanf (filename, "%s.%s", filecrypt, after);
  142.         if (OK == strcmp (after, "CRP"))
  143.             strcpy (filecrypt, "TEMP.$$$");
  144.         else strcat (filecrypt, ".CRP");
  145.           }
  146.     printf("\nencrypted file will be <%s>", filecrypt);
  147.     if (ERROR == (fd_crypt = creat ("TEMP.$$$")))
  148.           { printf("\ncan not create temporary file");
  149.         continue;    /* while (--argc) */
  150.           }
  151.  
  152.  
  153. /* set up seed for the number generator */
  154.     seed[0] = seed[1] = seed[2] = 0;
  155.     get_seed (key, seed);
  156.     nrand (-1, seed[0], seed[1], seed[2] );
  157.     
  158.     if DEBUG printf("\nfd_in=%d, fd_crypt=%d",fd_in,fd_crypt);
  159.     fine = FALSE;    /* musical end */
  160.     while (!fine)
  161.           { if (OK > (nread = read (fd_in, cryptbuf, 8)))
  162.               { printf("\nread returned %d for <%s>",
  163.                           nread,  filename );
  164.             if DEBUG printf("\nfd_in=%d",fd_in);
  165.             ERROR_EXIT
  166.               }
  167.         for (i=0; i<nread*SECSIZ; i++)
  168.             cryptbuf[i] ^= nrand(1);
  169.  
  170.         if (nread != write (fd_crypt, cryptbuf, nread))
  171.               { printf("\nerror writing to temporary file");
  172.             if DEBUG printf("\nfd_crypt=%d",fd_crypt);
  173.             ERROR_EXIT
  174.               }
  175.         if (nread == 0 || nread != 8)    fine = TRUE;
  176.           }
  177.     if (ERROR == close (fd_in))
  178.         printf("\nerror closing file <%s>", filename);
  179.     if (ERROR == close (fd_crypt))
  180.           { puts ("\nerror closing temporary file");
  181.         exit();
  182.           }
  183.     unlink (filecrypt);
  184.     rename ("TEMP.$$$", filecrypt);
  185.     printf("\n\nEncrypted form of <%s> is in <%s>",
  186.                                  filename,   filecrypt);
  187.  
  188.       }    /* end while (--argc) */
  189.  
  190. }    /* end main() */
  191.  
  192. /*************************************************************
  193.     gets input from keyboard char by char, blanking out
  194. as received. Assumes that "input" is big enough to handle input.
  195. Returns length of input string.
  196. ******************************************************************/
  197. int no_echo (input)
  198. char *input;
  199. {
  200. int c;
  201. char *cp;
  202.  
  203. cp = input;
  204. while (1)
  205.       { if (kbhit())
  206.           { if (NEWLINE == (*cp = getchar()))
  207.               { *cp = NUL;
  208.             break;
  209.               }
  210.         cp++;
  211.         putchar (BACKSPACE);
  212.         putchar (BLANK);
  213.           }
  214.       }
  215. return (strlen(input));
  216. }
  217.  
  218. /*************************************************************
  219.     get a seed for the ramnod number generator
  220. "seed" should be a 3-integer array
  221. *************************************************************/
  222. get_seed (key, seed)
  223. char *key;
  224. int *seed;
  225. {
  226. int *ptr;
  227. int i, length;
  228.  
  229. length = strlen (key);
  230. ptr = key;
  231. for (i=0; i< length/2; i++)
  232.     seed [i%3] +=    *ptr++;
  233. }
  234.  
  235. /*********************************************************************
  236.  returns index of t in s, ERROR if not found.  Thanx to Kernighan
  237.  and Ritchie, p. 67
  238. *********************************************************************/
  239. int index (s, t)
  240. char *s, *t;
  241. {
  242. int i, j, k;
  243.  
  244. for (i=0; s[i] != NUL; i++)
  245.       { for (j=i, k=0; t[k] != NUL && s[j]==t[k]; j++, k++)
  246.         ;
  247.     if (t[k] == NUL)
  248.         return (i);
  249.       }
  250. return (ERROR);
  251. }
  252.     { for (j=i, k=