home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 8 Other / 08-Other.zip / cipher.zip / cipher.c next >
C/C++ Source or Header  |  1993-07-11  |  2KB  |  108 lines

  1. /*
  2.  * CIPHER.C
  3.  * David Stafford
  4.  *
  5.  * Slightly modified and compiled for OS/2 by David Eagen
  6.  * using Borland C++ for OS/2 (July 11, 1993)
  7.  */
  8.  
  9. #include<stdio.h>
  10. #include<stdlib.h>
  11.  
  12. #define MULTIPLIER 0x015A4E35L
  13. #define INCREMENT 1
  14.  
  15. long RandomSeed;
  16.  
  17. int GetRandomNumber(int Range)
  18. {
  19.     RandomSeed = MULTIPLIER*RandomSeed+INCREMENT;
  20.     return (RandomSeed % Range);
  21. }
  22.  
  23. /*
  24.  * Does the actual enciphering.
  25.  *
  26.  * In:InFile Input file
  27.  *    OutFile Output File
  28.  *
  29.  * Out: Nothing
  30.  */
  31.  
  32. void Cipher(FILE*InFile, FILE*OutFile)
  33. {
  34.     int Ch;
  35.  
  36.     while((Ch=getc(InFile))!=EOF)
  37.     {
  38.         fputc(Ch^GetRandomNumber(256), OutFile);
  39.     }
  40. }
  41.  
  42. /*
  43.  * Opens the input and output files.
  44.  *
  45.  * In:Key  the key used to encrypt/decrypt the file
  46.  *    InFileName Input file name
  47.  *    OutFileName Output file name
  48.  *
  49.  * Out: Nothing
  50.  */
  51.  
  52. void Supervisor(long Key, char *InFileName, char *OutFileName)
  53. {
  54.     FILE *InFile, *OutFile;
  55.  
  56.     if((InFile = fopen(InFileName, "rb"))!=NULL)
  57.     {
  58.         if((OutFile = fopen(OutFileName, "wb"))!=NULL)
  59.         {
  60.             RandomSeed = Key;
  61.  
  62.             Cipher(InFile, OutFile);
  63.  
  64.             fclose(OutFile);
  65.         }
  66.         else
  67.         {
  68.             printf("Can't open output file %s\n", OutFileName);
  69.         }
  70.         fclose(InFile);
  71.     }
  72.     else
  73.     {
  74.         printf("Can't open input file %s\n", InFileName);
  75.     }
  76. }
  77.  
  78. /*
  79.  * Entry point. Gets the command-line args.
  80.  * Provides usage information.
  81.  *
  82.  * IN:Argc Count of command line arguments
  83.  *    Argv Strings(commands)
  84.  *
  85.  * Out: Nothing
  86.  */
  87.  
  88. void main(int Argc, char *Argv[])
  89. {
  90.     if(Argc==4)
  91.     {
  92.         Supervisor(atol(Argv[1]), Argv[2], Argv[3]);
  93.     }
  94.     else
  95.     {
  96.         puts("Cipher - File encryption/decryption\n");
  97.         puts("Usage: cipher_key input_file output_file\n");
  98.         puts("       cipher_key is a string of integers and/or characters");
  99.         puts("       input_file & output_file are the file names\n");
  100.         puts("For example: \"CIPHER 5275 README.TXT README.CPH\"");
  101.         puts("    This would encrypt the file README.TXT and store it");
  102.         puts("    in README.CPH. You must use the key 5275 to decrypt it.");
  103.         puts("To decrypt a file, simply reverse the file names....");
  104.         puts("             \"CIPHER 5275 README.CPH README.TXT\"");
  105.     }
  106. }
  107.  
  108.