home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 155_01 / crypt.c < prev    next >
Text File  |  1979-12-31  |  4KB  |  144 lines

  1. /*    
  2. HEADER:
  3. TITLE:        crypt;
  4. DATE:        04/18/85;
  5. DESCRIPTION:    
  6.    "This uses the simple encryption/decryption scheme of XORing each character 
  7. with those of a comparison string. The security in this is that any string of
  8. characters, known only by the user, can encode the text.  Furthermore to     
  9. retrieve the original text, the user simply applies the same key string.";     
  10.  
  11. KEYWORDS:    encryption,decryption,XOR;
  12. FILENAME:    crypt.c;
  13. WARNINGS:
  14.      "Do not encrypt a file with itself!  You will lose the   
  15. original file ( it will be all blank spaces due to the method used )."       
  16.  
  17. AUTHORS:    Jim Pisano;
  18. COMPILERS:    DeSmet;
  19. REFERENCES:    AUTHORS:    Kernighan,Plauger;
  20.         TITLE:        Software Tools;
  21.         CITATION:    "pg 49";        
  22.     ENDREF:
  23. */
  24.  
  25. /*   file name ... crypt.c
  26. *    program ..... crypt(), xor()
  27. *
  28. *   Adapted from Software Tools by Kernighan & Plauger, p.49.  
  29. *
  30. *   C implementaion by 
  31. *   Jim Pisano 
  32. *   April 4, 1985
  33. *
  34. *    This uses the simple encryption/decryption scheme of XORing each character
  35. * with those of a comparison string. The security in this is that any string of
  36. * characters, known only by the user, can encode the text.  Furthermore to 
  37. * retrieve the original text, the user simply applies the same key string.      
  38. *
  39. *   This implementation will accept the encoding key either directly from the
  40. * keyboard or from a file.  If the key is not a valid file name then it is used
  41. * as an enciphering key.  The encoded data are written to a temporary file.
  42. * The original file is then deleted, the temporary file is renamed to the 
  43. * original file eliminating the temporary file.
  44. *
  45. * **** CAUTION *****  Do not encrypt a file with itself!  You will lose the
  46. * original file ( it will be all blank spaces due to the method used ).       
  47. */
  48.  
  49. #include  <stdio.h>
  50.  
  51. main( argc, argv )
  52. int argc;
  53. char *argv[];
  54. {
  55.  
  56.     FILE *in_file, *in_crypt, *out_file, *fopen();
  57.     int i, key_len, in_char, out_char, err;
  58.     char *key;            /* encryption key */
  59. /*
  60. * Ensure that command line contains require inputs.
  61. */
  62.     if( argc < 2 )
  63.         pr_err_exit("No input file.");
  64.  
  65.     else if( argc < 3 )
  66.         pr_err_exit("No encryption code declared."); 
  67.  
  68.     in_file = fopen( argv[1], "r" );    /* open input file to encrypt */
  69.     if( in_file == ERR )
  70.         pr_err_exit("Can't open input file.");
  71.  
  72.     out_file = fopen( "temp.dat", "w" );    /* open temporary data file */
  73.     if( out_file == ERR )
  74.         pr_err_exit("Can't open temporary file.");
  75.  
  76. /* deal with encoding key */
  77.     i = 0;                /* init pointer to beginning of key */
  78.     in_crypt = fopen( argv[2], "r" );
  79. /* 
  80. * check if encoding key is a valid filename if so open it & get the key else
  81. * use the 3-rd command line arguement as a key
  82. */
  83.     if( (in_crypt != NULL) && (in_crypt != ERR) )
  84.     {
  85.         while( ( in_char = fgetc( in_crypt ) ) != EOF )
  86.             *(key + i++) = in_char;
  87.         fclose( in_crypt );
  88.     }
  89. /*
  90. * otherwise use the third command line arguement as a encryption key
  91. */
  92.     else     
  93.         key = argv[2];
  94.  
  95.     key_len = strlen( key );
  96.     i = 0;
  97. /*
  98. * encode input file using non-buffered I/O
  99. */
  100.     while( (in_char = fgetc( in_file ) ) != EOF )
  101.     {
  102. /*
  103. * wrap key phrase around if it's shorter than data
  104. */
  105.         if( i >= key_len )    
  106.             i = 0;        
  107.  
  108.         out_char = xor( in_char, *(key + i++)); 
  109.         err = fputc( out_char, out_file );
  110.  
  111.         if( err == ERR )
  112.             pr_err_exit("*** Error in writing to temporary file, check disk space.");
  113.     }
  114.     fclose( in_file );
  115.     fclose( out_file );
  116. /*
  117. * rename temporary file to original one & delete extraneous files.
  118. */
  119.     err = unlink( argv[1] );
  120.     if( err == ERR )
  121.         pr_err_exit("Error in delete input file.");
  122.  
  123.     err = rename( "temp.dat", argv[1] );
  124.     if( err == ERR )
  125.         pr_err_exit("Error in renaming file.");
  126. }
  127. /*
  128. *  Do bitwise exclusive or of bytes a and b
  129. */
  130. xor( a, b )
  131. char a, b;
  132. {
  133.     return( (a & ~b) | (~a & b) );
  134. }
  135. /* 
  136. * print error message and exit
  137. */
  138. pr_err_exit( mess )
  139. char *mess;
  140. {
  141.     printf( "\n%s\n", mess );
  142.     printf( "Aborting crypt...\n");
  143.     exit();
  144. }