home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 17 / CD_ASCQ_17_101194.iso / dos / prg / sphinx / examples / other / encrypt.c__ < prev    next >
Encoding:
Text File  |  1994-03-05  |  4.0 KB  |  162 lines

  1. /*
  2.     SPHINX Programming (c) 1994
  3.     SPHINX C-- Example Program
  4.     NAME:  ENCRYPT.C--
  5.     Description:  The encryption and decryption program.
  6.                   This program also has the option to emulate an older C
  7.                   version of the same program that had a bug that would
  8.                   slightly change the code.
  9.     Run File Size:  1497 bytes.
  10. */
  11.  
  12. ?parsecommandline  TRUE
  13.  
  14. ?include "FILE.H--"
  15. ?include "KEYCODES.H--"
  16. ?include "DOS.H--"
  17. ?include "WRITE.H--"
  18.  
  19.  
  20. word readhandle,writehandle;     /* file handles */
  21.  
  22. ?define MAXCODE 3000
  23. byte code[MAXCODE];
  24. byte buffer[MAXCODE];
  25.  
  26. word codelen=0,linelen;
  27. word i,level=0;
  28. byte dir,key;
  29.  
  30. main ()
  31. {
  32. WRITESTR("SPHINX Programming 1994 -- FILE ENCRYPTER -- Version 1.0\n");
  33. IF( PARAMCOUNT() < 2 )
  34.     {WRITESTR("USAGE> encrypt <INPUT FILENAME> <OUTPUT FILENAME> [OLD FLAG]\n");
  35.     EXIT(1);
  36.     }
  37.  
  38. WRITESTR("PRESS:  [0] for positive encrypt\n");
  39. WRITESTR("        [1] for negative encrypt\n");
  40. WRITESTR("        [anything else] to exit.\n");
  41. AL = GETCH();
  42. IF( AL == '0' )
  43.     dir = 0;
  44. ELSE IF( AL == '1' )
  45.     dir = 1;
  46. ELSE EXIT(2);
  47.  
  48. readhandle = open(PARAMSTR(0),F_READ);  /* open file for reading */
  49. IF(readhandle == 0)                    /* if file did not open */
  50.     {WRITESTR("Unable to open input file "); /* display error message */
  51.     WRITESTR(PARAMSTR(0));
  52.     WRITELN();
  53.     EXIT(3);
  54.     }
  55. writehandle = create(PARAMSTR(1),FA_NORMAL);  /* create file with normal attrib's */
  56. IF(writehandle == 0)                    /* if file did not open */
  57.     {WRITESTR("Unable to create output file.\n"); /* display error message */
  58.     WRITESTR(PARAMSTR(1));
  59.     WRITELN();
  60.     EXIT(4);
  61.     }
  62.  
  63. ES = DS;
  64. DI = #code;
  65. CX = MAXCODE;
  66. AL = 0;
  67. $ REPZ
  68. $ STOSB       /* initialize code to all zeros */
  69.  
  70. do {
  71.     WRITESTR("\nENTER CODE STRING LEVEL ");
  72.     WRITEWORD(level);
  73.     WRITESTR(" (<RETURN> to stop):\n");
  74.     i = 0;
  75.     do {
  76.         key = GETCH();
  77.         IF( key == ascii_backspace )
  78.             {IF( i > 0 )
  79.                 {i--;
  80.                 WRITESTR("\b \b");
  81.                 }
  82.             ELSE BEEP();
  83.             }
  84.         ELSE IF( key >= ' ' )
  85.             IF( key <= '~' )
  86.                 {IF( i < MAXCODE )
  87.                     {DI = i;
  88.                     buffer[DI] = key;
  89.                     i++;
  90.                     WRITE(key);
  91.                     }
  92.                 ELSE BEEP();
  93.                 }
  94.         } while( key != ascii_return );
  95.     IF( i > 0 )
  96.         {DI = 0;
  97.         do {code[DI] += buffer[DI];
  98.             DI++;
  99.             } while( DI < i );
  100.         IF( i > codelen )
  101.             codelen = i;
  102.         }
  103.     level++;
  104.     } while( i > 0 );
  105.  
  106. IF( codelen == 0 )
  107.     {WRITESTR("Error:  No code!\n");
  108.     EXIT(5);
  109.     }
  110.  
  111. WRITELN();
  112.  
  113. IF( PARAMCOUNT() >= 3 )
  114.     {WRITESTR("Modifying Code to emulate bug in old C encryption program.\n");
  115.     DI = codelen;
  116.     code[DI] = code[0];    
  117.     DI = 0;
  118.     IF( codelen > 1 )
  119.         {
  120.         do {code[DI] = code[DI+1];
  121.             DI++;
  122.             } while( DI+1 < codelen );
  123.         }
  124.     code[DI] = 0;
  125.     codelen++;
  126.     }
  127.  
  128. WRITESTR("Code Length: ");
  129. WRITEWORD(codelen);
  130. WRITELN();
  131. WRITESTR("\n*** CONVERTING ***");
  132.  
  133. linelen = read(readhandle,#buffer,codelen);
  134. IF( linelen > 0 )
  135.     {do {
  136.         DI = 0;
  137.         IF( dir == 0 )
  138.             {do {buffer[DI] += code[DI];
  139.                 DI++;
  140.                 } while( DI < linelen );
  141.             }
  142.         ELSE{
  143.             do {buffer[DI] -= code[DI];
  144.                 DI++;
  145.                 } while( DI < linelen );
  146.             }
  147.         IF( write(writehandle,#buffer,linelen) != linelen )
  148.             {WRITESTR("\nError writing to output file.\n");
  149.             close(writehandle);
  150.             EXIT(6);
  151.             }
  152.         IF( linelen == codelen )
  153.             linelen = read(readhandle,#buffer,codelen);
  154.         ELSE linelen = 0;
  155.         } while( linelen > 0 );
  156.     }
  157. close(writehandle);
  158. close(readhandle);
  159. WRITESTR("\ndone.");
  160. }
  161.  
  162. /* end of ENCRYPT.C-- */