home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / misc / encrypt.lzh / Encrypt.c < prev    next >
C/C++ Source or Header  |  1990-03-07  |  8KB  |  248 lines

  1. /*-----------------------------Encrypt V1.00-----------------------------*/
  2. /*---------------------------by Dave Schreiber---------------------------*/
  3. /*                                                                       */
  4. /*Encrypt V1.00 is Copyright (c)1990 by Dave Schreiber. All Rights Reserved*/
  5. /*Encrypt must by freely distributed, except for costs associated with   */
  6. /*shipping, copying, media, labels, taxes, and other necessary costs.    */
  7. /*************************************************************************/
  8. /*Compiled under Lattice C V5.02.  To compile, use this command line:    */
  9. /*  1> lc -Lm Encrypt                                                    */
  10. /*************************************************************************/
  11. /*Encrypt is a file encryptor/decryptor.  It will take any file (binary, */
  12. /*text, IFF, whatever) and encrypts it using a user specified password.  */
  13. /*This renders the file unusable until it is decrypted with the same     */
  14. /*password.                                                              */
  15. /*************************************************************************/
  16. /*The algorithm goes something like this:                                */
  17. /*   Multiply the ASCII values of the user supplied password together    */
  18. /*   Use as a seed for the drand48() function (Lattice's pseudo-random   */
  19. /*     number generator)                                                 */
  20. /*   To encrypt, for each byte of the file:                              */
  21. /*      Read in the byte                                                 */
  22. /*      Add it to the value returned by drand48() (which is btw 0 & 255) */
  23. /*      Write the byte out                                               */
  24. /*   To decrypt, follow the same procedure, but subtract the value       */
  25. /*     returned by drand48() from the byte.                              */
  26. /*-----------------------------------------------------------------------*/
  27. /*-----------------------------------------------------------------------*/
  28.  
  29.  
  30. /*System #include files*/
  31. #include <math.h>
  32. #include <exec/types.h>
  33. #include <exec/exec.h>
  34. #include <libraries/dos.h>
  35. #include <libraries/dosextens.h>
  36. #include <ctype.h>
  37.  
  38. void InterpretCommandLine();
  39. void PutByte();
  40.  
  41. main(argc,argv)
  42. int argc;
  43. char *argv[];
  44. {
  45.    ULONG seedvalue;
  46.    double value;
  47.    UBYTE final;
  48.    char filename[200],outfn[201];
  49.    struct FileHandle *outfile,*infile;
  50.    BYTE buffer[2],encrypt,replace;
  51.    
  52.    if(argv[1][0]=='?')  /*If the user requests help*/
  53.    {
  54.       puts("Encrypt V1.00 (c)1990 by Dave Schreiber");
  55.       puts("");
  56.       puts("Usage:");
  57.       puts("  1> Encrypt [options] -p<passwd> <infile> [<outfile>]");
  58.       puts("Where the options are:");
  59.       puts("  -e  -- Encrypt <infile> and store as <outfile> (default)");
  60.       puts("  -d  -- Decrypt <infile> and store as <outfile>");
  61.       puts("  -r  -- Replace:  encrypt/decrypt <infile> and store");
  62.       puts("         as <infile> (<outfile> is not needed with this");
  63.       puts("         option)");
  64.       puts("");
  65.       puts("  -p<paswd> -- Use <passwd> as the password");
  66.       exit(0);
  67.    }
  68.    
  69.    /*Process the command line arguments*/
  70.    InterpretCommandLine(argc,argv,filename,outfn,&encrypt,&replace,
  71.                         &seedvalue);
  72.    
  73.    /*Open the input file*/
  74.    if((infile=(struct FileHandle *)Open(filename,MODE_OLDFILE))==NULL)
  75.    {
  76.       puts("Couldn't open the read file!");
  77.       exit(500);
  78.    }
  79.    
  80.    /*Open the output file*/
  81.    if((outfile=(struct FileHandle *)Open(outfn,MODE_NEWFILE))==NULL)
  82.    {
  83.       puts("Couldn't open the write file!");
  84.       Close(infile); /*Clean up*/
  85.       exit(600);
  86.    }
  87.  
  88.    if(encrypt) /*Encrypt...*/
  89.    {
  90.       printf("Encrypting...\n");
  91.       while(!GetByte(infile,buffer))
  92.       {
  93.          value=drand48();     /*Get a random number*/
  94.          final=(value*256);   /*Reduce it to >=0 and <=255*/
  95.          buffer[0]+=final;    /*Add it to the read byte*/
  96.  
  97.          PutByte(outfile,buffer,FALSE);   /*Write to disk*/
  98.       }
  99.    }
  100.    else  /*Decrypt...*/
  101.    {
  102.       printf("Decrypting...\n");
  103.       while(!GetByte(infile,buffer))
  104.       {
  105.          value=drand48();
  106.          final=(value*256);
  107.          buffer[0]-=final;
  108.    
  109.          PutByte(outfile,buffer,FALSE);
  110.       }
  111.    }
  112.    PutByte(outfile,buffer,TRUE); /*End of file...*/
  113.    
  114.    Close(outfile);   /*Close the files*/
  115.    Close(infile);
  116.    if(replace)    /*If the -r switch was used, delete the .tmp file*/
  117.       DeleteFile(filename);
  118.    
  119.    exit(0);
  120. }
  121.  
  122. /*These next two routines are buffers for the AmigaDOS Write() and     */
  123. /*Read() commands.  Instead of writing out each byte at a time, bytes  */
  124. /*are written out in packets of 128.  This can lead to tremendous      */
  125. /*speed increases (one program I wrote got a speedup of 400% by using  */
  126. /*GetByte alone).  Use to your heart's content...                      */
  127.  
  128. /*P.S.  The important thing with these routines is that I/O is buffered*/
  129. /*not the amount of buffer.  I tried increasing the buffer size to 8K  */
  130. /*and got no noticable speed increase...                               */
  131.  
  132. void PutByte(outfile,buffer,EOF)
  133. char *buffer;
  134. struct FileHandle *outfile;
  135. BYTE EOF;
  136. {
  137.    static char chip Buffer[128];
  138.    static curpos=0;  /*Next space for byte*/
  139.    
  140.    if(EOF)  /*If its the end of the file*/
  141.    {
  142.       Write(outfile,Buffer,curpos); /*Flush the buffer*/
  143.       return;
  144.    }
  145.    
  146.    Buffer[curpos++]=buffer[0]; /*Add a byte to the buffer*/
  147.    if(curpos==sizeof(Buffer))  /*If the buffer is full*/
  148.    {
  149.       curpos = 0;
  150.       Write(outfile,Buffer,sizeof(Buffer)); /*Write to disk*/
  151.    }
  152. }
  153.  
  154. GetByte(infile,buffer)
  155. char *buffer;
  156. struct FileHandle *infile;
  157. {
  158.    static char chip Buffer[128];
  159.    static curpos=0; /*Next byte to be returned*/
  160.    static end = 0;  /*Last byte in buffer*/
  161.    
  162.    if(curpos == end) /*If we've sent every byte*/
  163.    {                 /*Get another 8K chunk*/
  164.       curpos = 0;
  165.       end=Read(infile,Buffer,sizeof(Buffer));
  166.    }
  167.    
  168.    buffer[0]=Buffer[curpos++];
  169.    return(!end); /*Returns whether or not EOF*/
  170. }
  171.  
  172. /*Interpets the command line arguments given to Encrypt*/
  173. void InterpretCommandLine(argc,argv,in,out,encr,repl)
  174. int argc;
  175. char *argv[],*in,*out;
  176. BYTE *encr,*repl;
  177. {
  178.    ULONG seed;
  179.    BYTE seedhasvalue=FALSE;
  180.    BYTE c,i;
  181.    
  182.    *encr=TRUE;    /*Encrypt is the default*/
  183.    *repl=FALSE;   /*Default is not to replace the original file*/
  184.    in[0]=out[0]=NULL;
  185.    
  186.    for(c=1;(argv[c][0]=='-') ;c++)
  187.    {
  188.       switch(argv[c][1])
  189.       {
  190.          case 'e':      /*Encrypt*/
  191.          case 'E':
  192.             *encr=TRUE;
  193.             break;
  194.          case 'd':      /*Decrypt*/
  195.          case 'D':
  196.             *encr=FALSE;
  197.             break;
  198.          case 'p':      /*Password*/
  199.          case 'P':
  200.             if(argv[c][2]==NULL)
  201.                break; 
  202.                /*Multiply characters of the password together*/
  203.             for(i=2,seed=1;i < strlen(argv[c]);seed*=argv[c][i++]);
  204.             srand48(seed); /*Use the result and the random number seed*/
  205.             seedhasvalue=TRUE;   /*We've got the seed*/
  206.             break;
  207.          case 'r':
  208.          case 'R':         /*Replace un-encrypted version with*/
  209.             *repl=TRUE;    /*encrypted version*/
  210.             break;
  211.       }
  212.    }
  213.    
  214.    if(argc-c < 2 && !*repl)   /*-r not set and filename(s) not given*/
  215.    {
  216.       puts("Please specify the input and output filenames!");
  217.       exit(310);
  218.    }
  219.    
  220.    if(argc-c < 1 && *repl)    /*-r set and filename not given*/
  221.    {
  222.       puts("Please specify the input filename!");
  223.       exit(320);
  224.    }
  225.  
  226.    if(!seedhasvalue) /*If a password wasn't given*/
  227.    {
  228.       puts("Please specify a password!");
  229.       exit(100);
  230.    }
  231.                
  232.    strcpy(in,argv[c]);
  233.    if(!*repl)  /*If -r was used, use the filenames given*/
  234.       strcpy(out,argv[++c]);
  235.    else  /*Otherwise add a .tmp to the original version's filename*/
  236.    {     /*and use the originals version's old filename as the output*/
  237.       strcpy(out,in);      /*filename*/
  238.       strcat(in,".tmp");   /*The original, .tmp version, will be deleted*/
  239.       if(!Rename(out,in))  /*when the encrypted file has been written*/
  240.       {                    /*sucessfully*/
  241.          puts("Couldn't access the file!");
  242.          exit(200);
  243.       }
  244.    }
  245. }
  246.  
  247. /*End of Encrypt.c*/
  248.