home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / crypt / diverse / encrypt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-09-10  |  3.8 KB  |  137 lines

  1. /****************************************************************************
  2.  * encrypt.c
  3.  *
  4.  *    Microsoft C  Vers 1.03            James Bumgardner 11-Sep-86
  5.  *                                              (818)-766-0594
  6.  *
  7.  * This program, and the associated header file are for encrypting text
  8.  * which is otherwise discernable through the use of a debugger or
  9.  * other dumping program.  Some possible uses are:
  10.  *
  11.  *    - Protecting Copyright and Author/Origin Messages
  12.  *    - Scrambling Passwords
  13.  *    - Preventing Game Players from cheating
  14.  *
  15.  * To use this program, you must #include the header file "decrypt.h" in
  16.  * your source code.  Use the decrypt() function on strings to be scrambled.
  17.  * For example:
  18.  *
  19.  *      puts("Copyright (c) 1986 James Bumgardner");
  20.  *
  21.  * would be changed to:
  22.  * 
  23.  *      puts(decrypt("Copyright (c) 1986 James Bumgardner"));
  24.  *
  25.  * Then make a text file which contains exact duplicates of each string
  26.  * to be encrypted.  Each string should be on a separate line.  The lines
  27.  * should be ordered as they appear in the object file.  There is
  28.  * currently an arbitrary limit of 256 characters per string.  Only
  29.  * ascii printing characters are allowed (e.g. 32-127).
  30.  *
  31.  * After you compile/link the file, use ENCRYPT.EXE to scramble the text.
  32.  * Note: Until Encrypt is run, the decrypted lines will be output as garbage
  33.  *       when your program is run.  Also, Encrypt should only be run once
  34.  *       per object file - decrypt can't decode multiple passes.
  35.  *
  36.  * Syntax:
  37.  * ENCRYPT <datafile> <objfile> [<outfile>]
  38.  *
  39.  *  <datafile> is the text file containing the strings to look for.
  40.  *  <objfile>  is the object file that will be encrypted.
  41.  *  <outfile>  is the output filename.
  42.  *
  43.  *************************************************************************/
  44.  
  45. #include <stdio.h>
  46. #include "decrypt.h"
  47.  
  48. #define TRUE    1
  49. #define FALSE    0
  50.  
  51. int _fmode = 0x0000;
  52.  
  53. FILE *datafile,*objfile,*outfile;
  54.  
  55. main(argc,argv)
  56. char *argv[];
  57. {
  58.     static int c,nchars = 0,nlines = 0;
  59.     static char cline[CRYPTMAX + 1],*cp;
  60.  
  61.     if (argc != 4)    {
  62.         printf("Syntax:\n\t ENCRYPT datafile objfile [outfile]\n\n");
  63.         clean_exit(0);
  64.     }
  65.     if ((datafile = fopen(argv[1],"r")) == NULL)    {
  66.         printf("Can't open datafile %s\n",argv[1]);
  67.         clean_exit(0);
  68.     }
  69.     _fmode = 0x8000;    /* open as binary files */
  70.     if ((objfile = fopen(argv[2],"r")) == NULL)    {
  71.         printf("Can't open objfile %s\n",argv[2]);
  72.         clean_exit(1);
  73.     }
  74.     if ((outfile = fopen(argv[3],"w")) == NULL)    {
  75.         printf("Can't open outfile %s\n",argv[3]);
  76.         clean_exit(2);
  77.     }
  78.     cprintf("Chars Read       Lines Encrypted\n");
  79.     if (cp = fgets(cline,CRYPTMAX,datafile)) {
  80.         cline[strlen(cline) - 1] = '\0';
  81.         if (*cp == '\0')    {
  82.             printf("Blank Line in Input File\n");
  83.             clean_exit(3);
  84.         }
  85.     }
  86.     while ((c = getc(objfile)) != EOF)    {
  87.         if ((nchars++ % 100) == 0)
  88.             cprintf("\r %8d                %3d",nchars,nlines);
  89.         if (cp)    {
  90.             if (c != *cp)    {
  91.                 if (cp != cline)    {
  92.                     fwrite(cline,1,(int) cp - (int) cline,
  93.                            outfile);
  94.                     cp = cline;
  95.                 }
  96.                 putc(c,outfile);
  97.             }
  98.             else    {
  99.  
  100.                 if (*cp == NULL)    {
  101.                     fwrite(encrypt(cline),1,strlen(cline)+1,
  102.                         outfile);
  103.                     if (cp = fgets(cline,CRYPTMAX,datafile)) {
  104.                         cline[strlen(cline) - 1] = '\0';
  105.                         if (*cp == '\0')    {
  106.                             printf("Blank Line in Input File\n");
  107.                             clean_exit(3);
  108.                         }
  109.                     }
  110.                     cprintf("\b\b\b%3d",++nlines);    
  111.                 }
  112.                 else    {
  113.                     ++cp;
  114.                 }
  115.             }
  116.         }
  117.         else    {
  118.             putc(c,outfile);
  119.         }
  120.     }
  121.     if (cp)
  122.         printf("\n\nCouldn't Encrypt: \n%s\n(And subsequent lines)\n",cline);
  123.  
  124.     printf("\n\n%d chars written to file %s\n",nchars,argv[3]);
  125.     clean_exit(3);
  126. }
  127.  
  128. clean_exit(n)
  129. {
  130.     switch (n)    {
  131.     case 3:    fclose(outfile);
  132.     case 2: fclose(objfile);
  133.     case 1: fclose(datafile);
  134.     }
  135.     exit();
  136. }    
  137.